Which Profiler?
Jamie asks:
"Ted,What are you guys using for a Profiler? If you have any advice on a profiler
or, more importantly, common areas a profiler flags, I'd love to hear them."
Well we use:
- JProfiler - for exploratory work (has the problem of dilation)
- com.tedogrady.profiler - for precise timings (basically println)
- The toplink profiler - for understanding the cache and gross database numbers
- tkprof /explain plan- for understanding how queries are working in the database
- math and common sense - for figuring out how to solve the problem
- Establish a target time.
- Make sure you have a test showing the slowness. Automate it.
- Develop Causal Theory
- Test Causal Theory
- Develop Potential Solution
- Experiment with Potential Solution
- If 4 doesn't help, go back to 3
- Implement Solution
It's lying to you. Remember it slows down your application, but not the database. So, hot spots and the relative timings it tells you are lies where concerned with the database. For example, the current piece I am profiling is spending half the time in the database dealing with foreign keys. The profiler told me my problem was in navigating some hierarchy.
I use Jprofiler for the following features:
- Execution stack trace - shows the order in which things were called
- Number of Invocations of a method - really useful (do quick math to see if the numbers make sense)
- Percentage time in a method - also really useful
- Hot spots - (rank methods by amount of CPU time/calls)
- Memory profiler - which objects are sucking back the memory
I tried the eclipse one about 6 months ago (TpTp) but it choked on simple stuff. Haven't tried it since.
com.tedogrady.profiler implements println with extra special fancy do-dad stuff. It allows you to verify what your profiler is doing. It lies to you a lot less frequently than JProfiler. It's about 40 lines of source code that I got tired of writing on every project, so its now open source. It is in with my toplink in memory stuff, or just email me if you want a copy. Basically, it has a really small overhead to verify what you learned in JProfiler, or if Jprofiler is really lying to you then you can use it as a more painful exploration tool.To profile a suspicious method you can do the following:
Profiler.start("Profiling myMethod()");You can put as many starts and stops (each with unique tags) in your code as you want. It'll tell you nice things like the average time, cumulative time, invocation order, blah blah blah. Many of you are thinking map. You'd be right.
this.myMethod()
Profiler.stop("Profiling myMethod()");
Profiler.dump();
TopLink probably comes with a profiler because of all the people that complain that toplink is slow. Very smart of them to give you the tools to see where the slowness/idiocy is. The tool usually tells me again how stupid I really am. As an idiot, I frequently forget I'm an idiot. It's part of the condition.
The command that lets you show sql in toplink is also very useful. Dario has been using it to fine tune the SQL we're throwing at Oracle. It lets you configure all those wacky options in toplink and see what they do to the SQL (just what does batch write or batch read do? Gee, they make things a lot faster).
tkprof, explain plan, and oracle analyzer are useful when you have to think about the database. They tell you what the database is doing, like how long a query is taking, or the strategy that the optimizer is using to evluate your query. (Sadly, you do need to understand how oracle implements things if you want to tune it). asktom.oracle.com has all your answers pre-packaged.
I haven't gone looking for Oracle analyzer in Oracle 10g, but I've used it in 9i. Basically, it is a way for you to ask the Oracle optimizer what will work best. Rather than you analyzing (guessing) what the best index are for a query, oracle will recommend some. 90% of them are optimal, and some recommendations slow things down. Remember that whole test thingamy.
math and common sense are the best tools of all. This would be why you went to school. When you look at the number of invocations, and the time, does it make sense to you? What do the numbers mean? A scrap of paper, a pencil, and some simple multiplication and division can tell you a lot more about what your program is doing than any profiler.
Just as a side note, I learned most my techniques sitting across the hall from Steve Northover at OTI when he was writing the profiler for OTI/Smalltalk. Thanks Steve.