This is a mirror of official site: http://jasper-net.blogspot.com/

Micro Benchmarking with Caliper

| Monday, February 7, 2011
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:

  1. Extend the class SimpleBenchmark
  2. Do any test setup/clean up in respective setUp or tearDown methods (Similar to JUnit setUp/tearDown)
  3. Write the methods that will execute the code to benchmark starting with the word "time" (Again similar to JUnit, just "time" instead of "test")
  4. Place the code you want to benchmark inside your timeSomeOperation methods
Getting and Running Caliper

To get started with Caliper
  1. Go here to get a read-only svn link to the source code
  2. 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

Posted via email from Jasper-net

0 comments: