tags:

views:

341

answers:

3

I am studying More programming pearls (Addison-Wesley, 1988) by Bentley J. and am stumped by the fact that I am unable to find any tool/ profiler for Java that can tell me the number of times each statement in executed in an actual flow (reference: Prime Number Finding algorithm optimization in Chapter 1). I tried using profilers, tried instrumentation API but may be I am looking at the wrong place. Can you guys please pull out some magic trick/tool and point me in the right direction or is it that we just can't do this in Java since each statement may not form exactly one machine instruction and keeping a count per statement may not be possible OR its just a method's CPU time/call count that we can look at and work on it.

+2  A: 

I'm pretty sure some of the various code coverage tools such as emma and clover record the number of times each statement is executed because it's displayed in the reports.

I'm guessing that they actually rewrite the class files during there compile phrase to insert some tracking code, not sure if this is suitable for you or not.

EDIT: As recomended by unknown Cobertura can be used to record statement execution counts

  • Emma open source, but doesn't seem to actually record statement counts
  • Clover commercial coverage tool certainly does
  • Cobertura Newer open source tool than emma and does record statement counts
Gareth Davis
Sorry but Emma did not work for me and Clover is paid so I will just pass. About Emma it came out with the coverage stats and not with lines execution counts. Anyways thanks again and I will try and incorporate emma going forward in all my play projects. I was missing something like that for sure.
thedeveloperdude
Cobertura coverage reports is, I believe, free and includes execution count.
Buhb
Cobertura... I knew I'd forgotten one
Gareth Davis
Yes finally Cobertura did it. I am having some problems getting it to generate HTML reports but the XML report shows what I want.e.g.<line number="16" hits="47575558" branch="true" condition-coverage="100% (2/2)">Thanks [ unknown (google) ]Moderators, how do I accept this comment as an answer?
thedeveloperdude
I was going to say that the unknown dude needs to add an answer for you to accept, will update my answer with unknown's recommendation.
Gareth Davis
A: 

You may want to know the invocation count of statements, but that has only a pretty indirect relationship to what should be fixed to get better performance. This is the language-agnostic method I use, and Jon Bentley has recognized it as pretty good.

Mike Dunlavey
A: 

JXInsight has this capability via custom resource meters (counters) and probes tracking.

http://williamlouth.wordpress.com/2009/05/04/execution-profiling-counting-kpis/

William Louth
Thanks for pointing out JXInsight. I have never heard of it previously but will try and check this one out to.
thedeveloperdude
I should also point out that this is not at all possible with a sample based profiling solution which is largely ineffective nowadays because of the nature of call stacks today in enterprise Java applications - they are devoid of context (url, sql, ejb query, message queue, component name). Sampling is also pretty poor are finding hotspots - methods with high inherent (exclusive) cost.
William Louth
Mike Dunlavey