问题
I am running my junit tests via ant and they are running substantially slower than via the IDE. My ant call is:
<junit fork="yes" forkmode="once" printsummary="off">
<classpath refid="test.classpath"/>
<formatter type="brief" usefile="false"/>
<batchtest todir="${test.results.dir}/xml">
<formatter type="xml"/>
<fileset dir="src" includes="**/*Test.java" />
</batchtest>
</junit>
The same test that runs in near instantaneously in my IDE (0.067s) takes 4.632s when run through Ant. In the past, I've been able to speed up test problems like this by using the junit fork parameter but this doesn't seem to be helping in this case. What properties or parameters can I look at to speed up these tests?
More info:
I am using the reported time from the IDE vs. the time that the junit task outputs. This is not the sum total time reported at the end of the ant run.
So, bizarrely, this problem has resolved itself. What could have caused this problem? The system runs on a local disk so that is not the problem.
回答1:
Here's a blind guess: try increasing the maximum heap size available to the forked VM by using a nested <jvmarg>
tag to set the -Xmx
option.
回答2:
I'm guessing it's because your antscript is outputing results to XML files, whereas the IDE is keeping those in memory. It takes longer to write a file than to not write a file.
todir="${test.results.dir}/xml"
That's the part of the <batchtest> call that tells it to stick the results into that directory. It looks like leaving it off just tells it to stick the results in the "current directory", whatever that is. At first glance I didn't see anything to turn it all the way off.
回答3:
Difficult to tell with that information. First thing I would do is look at the test results and determine if all the individual tests are running uniformly slower or if it can be narrowed down to a certain subset of test cases.
(The zero'th thing I would do is make sure that my ant task is using the same JVM as Eclipse and that the classpath dependencies and imported JARs are really and truly identical)
回答4:
Maybe you are seeing that because Eclipse do incremental compiling and Ant don't. Can you confirm that this time is wasted only in the test target?
回答5:
For the record, I found my problem. We have been using a code obfuscator for this project, and the string encryption portion of that obfuscator was set to "maximum". This slowed down any operation where strings were present.
Turning down the string encryption to a faster mode fixed the problem.
回答6:
Try setting fork, forkmode and threads to these values:
<junit fork="yes" forkmode="perTest" printsummary="off" threads="4">
<classpath refid="test.classpath"/>
<formatter type="brief" usefile="false"/>
<batchtest todir="${test.results.dir}/xml">
<formatter type="xml"/>
<fileset dir="src" includes="**/*Test.java" />
</batchtest>
</junit>
Also see https://ant.apache.org/manual/Tasks/junit.html
回答7:
For me, adding forkmode="once"
for the <junit>
element and adding usefile="false"
for the <formatter>
element makes the tests run much faster. Also remove the formatters that you don't need.
来源:https://stackoverflow.com/questions/123127/ant-junit-tests-are-running-much-slower-via-ant-than-via-ide-what-to-look-at