0

I have some component tests that are running relatively slow (a few seconds each) and am trying to figure out which functions are eating the most time. Is there a way I can have that displayed to me in eclipse when I run a JUnit test, maybe similar to the coverage data?

I have already tried using VisualVM for that, but the problem I have there is that the test process is only available while the test is running, so the best I can do there is put a breakpoint right at the beginning of the test, then debug the test, attach the VisualVM profiler, and the unpause the test, which sadly means that whatever few seconds I need to unpause the test bloats up the statistic.

It feels like there should be some way to say "start test and profile method execution time", but I have been looking at a few different options now (such as the Java Monitor plugin for eclipse) but none of them seems to offer that option?

Is there a way to do that? Or is it really the best solution to pause each test and then manually attach a profiler?

And maybe related to this, if I have a test case suite, how do I attach the profiler before any tests are run?

Kira Resari
  • 1,718
  • 4
  • 19
  • 50
  • I've always used JProfiler with Eclipse and never ran into a problem of attaching the profiler, having to hit the correct instance and so on. You might want to give it a try over VisualVM. – Ralf Kleberhoff Feb 24 '21 at 08:48
  • @RalfKleberhoff : Seems interesting, but how do I get it to work in Eclipse? I installed and integrated it, started eclipse with -clean, enabled the profile-action, but for some reason there is no available action if I right-click on a test and say `Profile as...`, and I can't figure out how to add a profiling configuration either. I would expect there to be a `Profile as... > JUnit Test` or maybe `Profile as... > JProfiler` to be available, but there isn't. – Kira Resari Feb 26 '21 at 06:11
  • Sorry, I don't recall the steps I had to take with Eclipse. In my Eclipse, there is `Profile as... > JUnit Test` in the context menu, and then it starts JProfiler. In Window / Preferences, have you set the JProfiler executable .EXE location? – Ralf Kleberhoff Feb 26 '21 at 11:12
  • @RalfKleberhoff : For me the only option under "Profile as..." is "Profile on Server", which doesn't seem to do anything, and adding a new Profiling configuration also seems to only add new Server stuff. What do you mean with "JProfiler executable .EXE location"? I've looked through the `Window.Preferences`, but I can't find a setting for this. Do I need to have an additional plugin installed? When installing JProfiler I did integrate it with Eclipse, but come to think of it I can't see any indication that that integration worked. – Kira Resari Mar 01 '21 at 06:13
  • Sorry, then I can't help any more, maybe some JProfiler support forum can answer that. – Ralf Kleberhoff Mar 01 '21 at 10:14

1 Answers1

-1
long startTime = System.nanoTime();
//your code to measure the time of
long estimatedTime = System.nanoTime() - startTime;

you can just print out estimatedTime and the part of your code that takes longest is the one that prints out the biggest number

here are some more information on System.nanoTime() and System.currentTimeMillis() and the source