[英]How to identify slow unit tests when using maven-surefire-plugin in parallel mode?
With a view to managing / reducing our build times, I want to identify which unit tests are taking the most time - in a parallel test environment using the maven-surefire-plugin
. 为了管理/缩短构建时间,我想确定哪些单元测试花费的时间最多 - 在使用
maven-surefire-plugin
的并行测试环境中。
We are using JUnit
(4.10) for unit tests. 我们使用
JUnit
(4.10)进行单元测试。 We use maven
(2.2.1 - some plugins we use don't yet support maven 3) as our primary build tool, and the maven-surefire-plugin
(2.19) to run unit tests. 我们使用
maven
(2.2.1 - 我们使用的一些插件还不支持maven 3)作为我们的主要构建工具,使用maven-surefire-plugin
(2.19)来运行单元测试。
We are using the maven-surefire-plugin
in parallel mode , where both the individual methods are run in parallel and the unit test classes are run in parallel - this is very important, as it significantly reduces build unit test time. 我们在并行模式下使用
maven-surefire-plugin
,其中各个方法并行运行,单元测试类并行运行 - 这非常重要,因为它显着减少了构建单元的测试时间。 The maven-surefire-plugin
is configured in the .pom
as follows: maven-surefire-plugin
.pom
maven-surefire-plugin
在.pom
配置如下:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<configuration>
<argLine>-Xmx2G -XX:MaxPermSize=1G -XX:-UseSplitVerifier</argLine>
<failIfNoTests>false</failIfNoTests>
<parallel>classesAndMethods</parallel>
<useUnlimitedThreads>true</useUnlimitedThreads>
</configuration>
</plugin>
However, one of the implications of this is that in the console output, the time elapsed for each JUnit test class is the aggregate time for all the methods in the class. 但是,其中一个含义是在控制台输出中,每个JUnit测试类所用的时间是该类中所有方法的总时间。
For example, if a test class had 10 unit test methods, each of which took 1 second to run, then the test class would take about 1 second to run (each method being run in parallel), but the output would be something like: 例如,如果一个测试类有10个单元测试方法,每个测试方法需要1秒才能运行,那么测试类需要大约1秒才能运行(每个方法并行运行),但输出结果如下:
Running com.package.QuickParallelTest Tests run: 10, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 10.0 sec - in com.package.QuickParallelTest
运行com.package.QuickParallelTest测试运行:10,失败:0,错误:0,跳过:0,已过去时间:10.0秒 - 在com.package.QuickParallelTest中
This makes it difficult to distinguish in the console output from another test class with 10 unit test methods, of which 9 run almost instantly and 1 takes almost 10 seconds to run. 这使得很难在控制台输出与另一个测试类中区分10个单元测试方法,其中9个几乎立即运行,1个运行几乎需要10秒。 In this case, the test class would take about 10 seconds to run (because of the one slow test method), but the
maven-surefire-plugin
console output would be effectively the same: 在这种情况下,测试类需要大约10秒才能运行(因为一个慢速测试方法),但是
maven-surefire-plugin
控制台输出实际上是相同的:
Running com.package.SlowParallelTest Tests run: 10, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 10.0 sec - in com.package.SlowParallelTest
运行com.package.SlowParallelTest测试运行:10,失败:0,错误:0,跳过:0,已过去时间:10.0秒 - 在com.package.SlowParallelTest
Ideally, I would like the time elapsed to indicate how long the test class took to run (in parallel), not the aggregate time taken to run the methods separately (as if single-threaded). 理想情况下,我希望花时间来指示测试类运行多长时间(并行),而不是单独运行方法所花费的总时间(就像单线程一样)。
So, my question(s) is/are: 所以,我的问题是:
maven-surefire-plugin
? maven-surefire-plugin
已知的“错误”(或“功能”)吗? (I've checked the SureFire JIRA , but couldn't find anything like this.) EDIT: 编辑:
I've tried playing with some additional configuration settings. 我试过玩一些额外的配置设置。 Curiously, adding the the following to the configuration in the
.pom
seems to change the time elapsed in the console output to be the time taken in running the test class - however, this is (in my mind) counter-intuitive, since these settings are the default settings : 奇怪的是,将以下内容添加到
.pom
的配置似乎将控制台输出中经过的时间更改为运行测试类所花费的时间 - 但是,这(在我看来)这是违反直觉的,因为这些设置是默认设置 :
<configuration>
...
<forkCount>1</forkCount>
<reuseForks>true</reuseForks>
</configuration>
Adding to the Maven Surefire Plugin configuration the reportFormat entry and setting its value to plain
(instead of the default brief
) would give you elapsed time per method. 添加到Maven Surefire插件配置的reportFormat条目并将其值设置为
plain
(而不是默认的brief
)将为每个方法提供经过的时间。
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<configuration>
<argLine>-Xmx2G -XX:MaxPermSize=1G -XX:-UseSplitVerifier</argLine>
<failIfNoTests>false</failIfNoTests>
<parallel>classesAndMethods</parallel>
<useUnlimitedThreads>true</useUnlimitedThreads>
<reportFormat>plain</reportFormat>
</configuration>
</plugin>
</plugins>
</build>
Output with default reportFormat ( brief
): 使用默认reportFormat输出(
brief
):
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.sample.mocking.InternalServiceTestCase
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.241 sec - in com.sample.mocking.InternalServiceTestCase
Output with plain
value: 具有
plain
价值的输出:
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.sample.mocking.InternalServiceTestCase
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.187 sec - in com.sample.mocking.InternalServiceTestCase
test(com.sample.mocking.InternalServiceTestCase) Time elapsed: 0.005 sec
mockTest(com.sample.mocking.InternalServiceTestCase) Time elapsed: 0.17 sec
mockTestFailureTollerance(com.sample.mocking.InternalServiceTestCase) Time elapsed: 0.007 sec
mockProcessfile(com.sample.mocking.InternalServiceTestCase) Time elapsed: 0.003 sec
This option may give you further details on tests and execution time. 此选项可为您提供有关测试和执行时间的更多详细信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.