Profile JUnit in Eclipse Indigo using VisualVM

rnunes picture rnunes · Aug 6, 2012 · Viewed 15.2k times · Source

How can I profile a JUnit Test, preferentially with Eclipse integrated support? I'm trying to do it using VisualVM but apparently it can't be done.

I'm using Windows 7 x64, Eclipse Indigo, jdk 1.6, jre-j9 and JUnit 3 (I could use v.4)

Answer

Matthew Farwell picture Matthew Farwell · Aug 6, 2012

You can use JVisualVM (%JAVA_HOME%/bin/jvisualvm.exe), but to use it with JUnit, you need to change the run configuration slightly.

  1. Run the JUnit tests once, to create the run configuration
  2. Edit the Run Configuration (Run->Run Configurations...)
  3. In the Test tab check the box 'Keep JUnit running after test when debugging'
  4. Rerun the test (with Debug). This will run the tests, but importantly, leave the JVM around, so that you can attach to it using JVisualVM.

If you wish to profile a specific section of the test, then setting a breakpoint before and after will enable you to start / stop profiling.