surefire单元测试 并发 提速

本文详细介绍了Maven Surefire插件中的并发配置选项,包括parallel参数的不同取值及其含义,如methods、classes、suites等,并解释了如何通过forkCount和reuseForks参数控制JVM进程数及复用策略。

http://maven.apache.org/surefire/maven-surefire-plugin/examples/fork-options-and-parallel-execution.html

这是官方地址


parallel 这个参数可以说是很重要的参数,因为其控制并发执行的对象,基于它我们可以设置并发执行类,类中的方法或者测试集suits.

The most obviousone is by using the parallel parameter. The possible values dependon the test provider used. For JUnit 4.7 and onwards, this may be methodsclassesbothsuitessuitesAndClassessuitesAndMethodsclassesAndMethods or all. As aprerequisite in JUnit tests, the JUnit runner should extend org.junit.runners.ParentRunner. If no runner is specified through theannotation @org.junit.runner.RunWith, the prerequisite is accomplished.

As ofmaven-surefire-plugin:2.16, the value "both" isdeprecated but it still can be used and behaves same as classesAndMethods.

这是官方上的原文,语音也很简单,就是说明parallel可以有哪些属性值和含义,methodsclassessuites,以及两两的组合和all.  

surefire 2.16版本是对于并发配置而言是一个变更的节点。2.16以上版本属性和属性名上发生了很多变化。或者可以说是里程碑。

几个例子:

As an example with an unlimited number of threads, there is maximum ofthree concurrent threads to execute suites: parallel=alluseUnlimitedThreads=truethreadCountSuites=3.  并发,不限制线程数,但是suits线程数是3.

这里又提到useUnlimitedThreads 和threadCountSuites 属性。

那么必然也有threadCountClasses属性  和  threadCountMethods 属性


In the second example, the number of concurrent methods is not strictlylimited: parallel=classesAndMethodsthreadCount=8threadCountClasses=3. Here thenumber of parallel methods is varying from 5 to 7. Accordingly parallel=all, but the sumof threadCountSuites and threadCountClasses must notexceed certain (threadCount - 1). Other combinations are possiblewith unspecified thread-count leaf. Make sure that the leaf islast from the order suites-classes-methods in parallel.

这个例子宽泛定义了method线程数的区间。


In the third example the thread-counts represent a ratio, e.g. for parallel=allthreadCount=16threadCountSuites=2threadCountClasses=3threadCountMethods=5. Thus theconcurrent suites will be 20%, concurrent classes 30%, and concurrent methods50%.

线程数,百分比的定义。



The parameters parallelTestsTimeoutInSeconds and parallelTestsTimeoutForcedInSeconds are usedto specify an optional timeout in parallel execution. If the timeout iselapsed, the plugin prints the summary log with ERROR lines: "Thesetests were executed in prior to the shutdown operation", and "Thesetests are incomplete" if the running Threads were interrupted.

这是parallelTestsTimeoutInSeconds parallelTestsTimeoutForcedInSeconds    说明。大概意思:

参数parallelTestsTimeoutInSeconds和parallelTestsTimeoutForcedInSeconds用于在并行执行中指定可选超时。如果超时过期,插件将使用错误行打印摘要日志:“这些测试在关闭操作之前执行”,如果正在运行的线程中断,则“这些测试不完整”。


forkCount 进程级别限制:

The parameter forkCount definesthe maximum number of JVM processes that maven-surefire-plugin will spawn concurrently toexecute the tests. It supports the same syntax as -T  in maven-core:if you terminate the value with a 'C', that value will be multiplied with thenumber of available CPU cores in your system. For example forkCount=2.5C on aQuad-Core system will result in forking up to ten concurrent JVM processes thatexecute tests.

The parameter reuseForks is used todefine whether to terminate the spawned process after one test class and tocreate a new process for the next test in line (reuseForks=false), or whether toreuse the processes to execute the next tests (reuseForks=true).

这里提及到 forkCount  和 reuseForks 两个重要的参数;forkcount 控制jvm进程数,reuseForks 设置进程数的可增长性。

CombiningforkCount and parallel

The modes forkCount=0 and forkCount=1/reuseForks=true canbe combined freely with the available settings for parallel.

As reuseForks=false creates a new JVM process for each test class, using parallel=classes wouldhave no effect. You can still use parallel=methods, though.

When using reuseForks=true anda forkCount valuelarger than one, test classes are handed over to the forked process one-by-one.Thus, parallel=classes wouldnot change anything. However, you can use parallel=methods: classes are executed in forkCount concurrent processes, each of the processes can thenuse threadCount threadsto execute the methods of one class in parallel.

forkCount = 0和forkCount = 1 /reuseForks = true的模式可以与可用的并行设置自由组合。

由于reuseForks = false为每个测试类创建一个新的JVM进程,使用parallel = classes将不起作用。你仍然可以使用parallel = methods。

当使用reuseForks = true和大于1的forkCount值时,测试类将逐个移交给并发出来的进程。因此,parallel = classes不会改变任何东西。但是,可以使用parallel =methods,parellel限制了类不能并发,但在forkCount并发进程中可以执行类,然后每个进程可以使用threadCount线程并行执行一个类的方法。   这句话说明

了parallel只能限制在jvm内部是并发执行方法或者类,说白是限制线程,而对进程没有约束。parallel和forkcount分别是线程和进程级别。


Migratingthe Deprecated forkMode Parameter to forkCount and reuseForks

surefire versions prior 2.14 used theparameter forkMode to configure forking. Although that parameter is stillsupported for backward compatibility, users are strongly encouraged to migratetheir configuration and use forkCount and reuseForks instead.

The migration is quite simple, giventhe following mapping:

将已弃用的forkMode参数迁移到forkCount和reuseForks

surefire版本之前2.14使用参数forkMode配置分岔。尽管该参数仍然支持向后兼容性,但强烈建议用户迁移其配置,并使用forkCount和reuseForks。

Old Setting

New Setting

forkMode=once (default)

forkCount=1(default), 

reuseForks=true (default)

forkMode=always

forkCount=1 (default), reuseForks=false

forkMode=never

forkCount=0

forkMode=perthreadthreadCount=N

forkCount=N, (reuseForks=false,

 if you did not had that one set)



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值