Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Profile JUnit in Eclipse Indigo using VisualVM

Tags:

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)

like image 743
rnunes Avatar asked Aug 06 '12 10:08

rnunes


People also ask

What is profiler in VisualVM?

Java VisualVM enables you to take profiler snapshots to capture the results of a profiling session. A profiler snapshot captures the results at the moment the snapshot is taken. To take a snapshot, click the Take Snapshot of Collected Results button in the toolbar while a profiling session is in progress.

How do I use VisualVM in STS?

IntelliJ IDEA To set up VisualVM in Idea, first download the VisualVM Launcher jar. Open up the plugins settings page and install it by selecting 'Install plugin from disk'. Relaunch Idea and go to the new VisualVM Launcher settings page. Set the path to your jvisualvm.


2 Answers

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.

like image 137
Matthew Farwell Avatar answered Oct 07 '22 19:10

Matthew Farwell


JProfiler has an eclipse plugin which supports JUnit run configurations. See here for a screen cast on the eclipse IDE integration.

To keep the JVM alive after the test finishes (so that you can analyzed the results), edit the profiling settings and select "Keep VM alive" on the "Miscellaneous" tab.

Disclaimer: My company develops JProfiler.

like image 40
Ingo Kegel Avatar answered Oct 07 '22 19:10

Ingo Kegel