Profiling line by line Java

Viewed 1364

I'm trying to profile my code to detect bottleneck. I have searched some profiler, but I never find what I was looking for.

I used a lot python in the past, and there was this software : line_profiler who gives this kind of return :

0         Line     Hits  Time  Per Hit   % Time  Line Contents

11                                           @profile
12                                           def compute_prior(folder):
13                                               """
14                                               Given a folder, we compute the prior of neg and pos
15                                               folder = "./movie-reviews-en/train/"
16                                               """
17                                               # we compute the number of positive reviews
18         3         1719    573.0     52.9      number_positive = len([f for f in listdir(folder + "pos/")])
19                                               # then the negative
20         3         1512    504.0     46.6      number_negative = len([f for f in listdir(folder + "neg/")])
21                                               # we add it and we have the total
22         3            6      2.0      0.2      total = number_positive + number_negative
23                                               # we devide to have the probabilites
24         3            6      2.0      0.2      number_positive /= total
25         3            1      0.3      0.0      number_negative /= total
26                                               # we return this three numbers
27         3            3      1.0      0.1      return [number_positive, number_negative, total]

Is there something similare in the Java world ?

Thanks for the reply.

p.s: I already know yourkit, jprofiler, visualwm, but I want something inside the code.

2 Answers

I've been working with performance analysis for quite some time and am pretty sure, you will not find such a tool. I don't think, it is feasible to build such a tool in the first place.

There are a lot of imponderabilities involved in this one, for example, that the hotspot-VM will de- and recompile code upon learning probable execution-paths or execution-frequencies. So, the execution-time for one line of code might significantly vary over time.

Also, your monitoring solution will slow down your application and worse: it will change the relative execution-times of your code. This basically means you might spot a hotspot in a place where no such hotspot would exist without your monitoring.

You are free to build such a tool yourself, that is, measuring execution-times via System.nanoTime(), but you will certainly discover that this is no path to follow.

My suggestion would be to stick with the default profilers you already named until you pinpoint the source of your sorrow and then switch to some manual technique or use refactorings to extract parts of the methods in which the hotspot is contained to get a clearer understanding, which part of the code is responsible for undesired behaviour.

In case you want to build micro-benchmarks: simply forget it. Micro-benchmarks almost never show any reliable or transferable data even if you get them right and chances are very high, you do not get them right at all.

You may find useful VTune Amplifier which is a profiler supporting Java code analysis. You will be able to see the hotspots in your Java code and the performance metrics distributed through your java source file. What's really valuable is that VTune Amplifier shows the accurate stacks for pure Java code and Java/C++ mixed mode code.

Related