如何在多模块Maven构建中列出最慢的JUnit测试
如何列出多模块Maven构建中最慢的JUnit测试?
How can I list the slowest JUnit tests in a multi-module Maven build?
这应该包含在所有模块中.
This should be accross all modules.
Hudson/Jenkins解决方案也可以.
A Hudson/Jenkins solution could also do.
免责声明:我对我的bash解决方案表示真正的歉意,尽管它可以正常工作并且适合一行:-).如果您不耐烦,请移至底部.
Disclaimer: I truly apologize for my bash solution, although it works and fits in one line :-). If you are impatient, go to the bottom.
首先,我们需要找到maven-surefire-plugin
生成的所有TEST-*.xml
文件.在项目的根目录中的mvn test
之后运行此命令,以发现所有子模块中的测试结果:
First we need to find all TEST-*.xml
files produced by maven-surefire-plugin
. Run this after mvn test
in the root directory of your project to discover test results in all submodules:
$ find . -iname "TEST-*.xml"
幸运的是,这些文件的格式非常简单,只需一个grep
,我们便拥有了所需的内容:
Fortunately the format of these files is pretty straightforward, a simple grep
and we have what we need:
$ grep -h "<testcase" `find . -iname "TEST-*.xml"`
现在,一些sed
魔术可以提取调用时间,测试用例类和方法名称:
Now some sed
magic to extract invocation time, test case class and method name:
$ sed 's/<testcase time="\(.*\)" classname="\(.*\)" name="\(.*\)".*/\1\t\2.\3/'
剩下的只是对结果进行排序并显示运行时间最长的测试了:
There's nothing more left just to sort the result and display longest running tests:
$ sort -rn | head
承诺的单线:
$ grep -h "<testcase" `find . -iname "TEST-*.xml"` | sed 's/<testcase time="\(.*\)" classname="\(.*\)" name="\(.*\)".*/\1\t\2.\3/' | sort -rn | head
令人惊讶的是,结果看起来很合理(以Activiti 5.1多模块代码库为例):
Amazingly, the results look reasonable (Activiti 5.1 multi-module code-base taken as an example):
3.029 org.activiti.examples.variables.jpa.JPAVariableTest.testStoreJPAEntityAsVariable
2.904 org.activiti.engine.test.forms.FormsTest.testTaskFormPropertyDefaultsAndFormRendering
1.594 org.activiti.engine.test.api.mgmt.ManagementServiceTest.testGetJobExceptionStacktrace
1.114 org.activiti.examples.variables.jpa.JPAVariableTest.testUpdateJPAEntityValues
1.006 org.activiti.engine.test.db.EngineRebootProcessDefinitionCacheTest.testStartProcessInstanceByIdAfterReboot
0 org.activiti.engine.test.pvm.PvmVariablesTest.testVariables
0 org.activiti.engine.test.pvm.PvmScopeWaitStateTest.testWaitStateScope
0 org.activiti.engine.test.pvm.PvmScopesAndConcurrencyTest.testConcurrentPathsGoingIntoScope
0 org.activiti.engine.test.pvm.PvmEventTest.testNestedActivitiesEventsOnTransitionEvents
0 org.activiti.engine.test.pvm.PvmEventTest.testEmbeddedSubProcessEvents