From time to time I think all developers have done some form of benchmarking. I recently discovered Caliper which is according to the site - "Caliper is Google's open-source framework for writing, running and viewing the results of Java Microbenchmarks". I am aware that micro-benchmarking can be misleading depending on who is writing the tests, but sometimes they are very helpful in getting a feel for how your code is running.
Background
Micro benchmarks are dead simple to do. Get the current time in milliseconds, execute your code, then get the time in milliseconds again and subtract the difference. So why use a tool like Caliper? To me Caliper is great because it has a very familiar JUnit type of structure and feel to it. Instead of trying to describe what Caliper does, it's probably easiest to look at a simple example I put together. I decided to benchmark implementations of Heap Sort,Merge Sort,Quick Sort and the sort method of the java.lang.Arrays class. Not a terribly original idea I admit, but it was quick to do and it makes the point. NOTE: Package statement and imports left out intentionally for brevity
Getting Started
Here are the basic steps to writing benchmarks in Caliper:
To get started with Caliper
Read more: Near infinity
Background
Micro benchmarks are dead simple to do. Get the current time in milliseconds, execute your code, then get the time in milliseconds again and subtract the difference. So why use a tool like Caliper? To me Caliper is great because it has a very familiar JUnit type of structure and feel to it. Instead of trying to describe what Caliper does, it's probably easiest to look at a simple example I put together. I decided to benchmark implementations of Heap Sort,Merge Sort,Quick Sort and the sort method of the java.lang.Arrays class. Not a terribly original idea I admit, but it was quick to do and it makes the point. NOTE: Package statement and imports left out intentionally for brevity
Getting Started
Here are the basic steps to writing benchmarks in Caliper:
- Extend the class SimpleBenchmark
- Do any test setup/clean up in respective setUp or tearDown methods (Similar to JUnit setUp/tearDown)
- Write the methods that will execute the code to benchmark starting with the word "time" (Again similar to JUnit, just "time" instead of "test")
- Place the code you want to benchmark inside your timeSomeOperation methods
To get started with Caliper
- Go here to get a read-only svn link to the source code
- Check out the code then cd into <CALIPER_INSTALL_DIR> and run ant (obviously you need ant installed and on your path)
Read more: Near infinity