surefire单元测试 并发 提速
http://maven.apache.org/surefire/maven-surefire-plugin/examples/fork-options-and-parallel-execution.html
這是官方地址
parallel?這個參數(shù)可以說是很重要的參數(shù),因為其控制并發(fā)執(zhí)行的對象,基于它我們可以設(shè)置并發(fā)執(zhí)行類,類中的方法或者測試集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?methods,?classes,?both,?suites,?suitesAndClasses,?suitesAndMethods,?classesAndMethods?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可以有哪些屬性值和含義,methods,?classes,?suites,以及兩兩的組合和all. ?surefire 2.16版本是對于并發(fā)配置而言是一個變更的節(jié)點。2.16以上版本屬性和屬性名上發(fā)生了很多變化。或者可以說是里程碑。
幾個例子:
As an example with an unlimited number of threads, there is maximum ofthree concurrent threads to execute suites:?parallel=all,?useUnlimitedThreads=true,?threadCountSuites=3. ?并發(fā),不限制線程數(shù),但是suits線程數(shù)是3.
這里又提到useUnlimitedThreads 和threadCountSuites 屬性。
那么必然也有threadCountClasses屬性 ?和 ?threadCountMethods 屬性
In the second example, the number of concurrent methods is not strictlylimited:?parallel=classesAndMethods,?threadCount=8,?threadCountClasses=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線程數(shù)的區(qū)間。
In the third example the thread-counts represent a ratio, e.g. for?parallel=all,?threadCount=16,?threadCountSuites=2,?threadCountClasses=3,?threadCountMethods=5. Thus theconcurrent suites will be 20%, concurrent classes 30%, and concurrent methods50%.
線程數(shù),百分比的定義。
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? ? 說明。大概意思:參數(shù)parallelTestsTimeoutInSeconds和parallelTestsTimeoutForcedInSeconds用于在并行執(zhí)行中指定可選超時。如果超時過期,插件將使用錯誤行打印摘要日志:“這些測試在關(guān)閉操作之前執(zhí)行”,如果正在運行的線程中斷,則“這些測試不完整”。
forkCount 進(jìn)程級別限制:
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 兩個重要的參數(shù);forkcount 控制jvm進(jìn)程數(shù),reuseForks 設(shè)置進(jìn)程數(shù)的可增長性。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的模式可以與可用的并行設(shè)置自由組合。
由于reuseForks = false為每個測試類創(chuàng)建一個新的JVM進(jìn)程,使用parallel = classes將不起作用。你仍然可以使用parallel = methods。
當(dāng)使用reuseForks = true和大于1的forkCount值時,測試類將逐個移交給并發(fā)出來的進(jìn)程。因此,parallel = classes不會改變?nèi)魏螙|西。但是,可以使用parallel =methods,parellel限制了類不能并發(fā),但在forkCount并發(fā)進(jìn)程中可以執(zhí)行類,然后每個進(jìn)程可以使用threadCount線程并行執(zhí)行一個類的方法。? ?這句話說明
了parallel只能限制在jvm內(nèi)部是并發(fā)執(zhí)行方法或者類,說白是限制線程,而對進(jìn)程沒有約束。parallel和forkcount分別是線程和進(jìn)程級別。
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參數(shù)遷移到forkCount和reuseForks
surefire版本之前2.14使用參數(shù)forkMode配置分岔。盡管該參數(shù)仍然支持向后兼容性,但強(qiáng)烈建議用戶遷移其配置,并使用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=perthread,?threadCount=N | forkCount=N, (reuseForks=false, ?if you did not had that one set) |
總結(jié)
以上是生活随笔為你收集整理的surefire单元测试 并发 提速的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JVM调优之jstack找出最耗cpu的
- 下一篇: 中班美术教案《玩具乐园》反思