Groovy Documentation

groovy.gbench
[Java] Annotation Type Benchmark

java.lang.Object
  groovy.gbench.Benchmark

@Retention(RetentionPolicy.SOURCE)
@Target({ElementType.METHOD, ElementType.TYPE})
@GroovyASTTransformationClass("gbench.BenchmarkASTTransformation")
public @interface Benchmark

An annotation to benchmark methods.

This annotation allows you to benchmark methods without modifying their existing code. It can be added to methods or classes.


 class Klass {
     @Benchmark
     def foo() {
     }
     @Benchmark
     def bar() {
     }
 }
 
 @Benchmark
 class Klass {
     def foo() {
     }
     def bar() {
     }
 }
 
The ouputs of both examples will be:

 Klass    java.lang.Object foo()    user:xxx system:xxx cpu:xxx real:xxx
 Klass    java.lang.Object bar()    user:xxx system:xxx cpu:xxx real:xxx
 
The handling of benchmark results can be customized by using handler classes that implement BenchmarkHandler interface. Handler classes must have two methods, handle() and getInstance():

 class MyHandler implements Benchmark.BenchmarkHandler {
     static def instance = new MyHandler()
         void handle(klass method, time) {
         println("${method} of ${klass}: ${(time.real/1000000) as long} ms")
 	   }
     static MyHandler getInstance() {
         instance
     } 
 }
 
 @Benchmark(MyHandler.class)
 def foo() {
 }
 
Since Groovy 1.8, closures can be used instead of handler classes. With closures, you just need to assign closures that handle benchmark results:

 @Benchmark({println("${method} of ${class}: ${(time.real/1000000) as long} ms")})
 def foo() {
 }
 
also the default handling operation can be replaced with a system property, "gbench.defaulthandle":

 groovy -cp gbench-xx.xx.xx.jar -Dgbench.defaulthandle="println(method + ' of ' + klass + ': ' + ((time.real/1000000) as long) + ' ms')" Foo.groovy
 
Then the ouputs of both examples will be:

 java.lang.Object foo() of Foo: xxx ms
 
System Properties
KeyValueMeaning
"gbench.defaulthandle"expressionReplaces the default benchmark handling.
"gbench.cputime""on","off"Enables measuring CPU time. The default value is "on".

Authors:
Nagai Masato


Nested Class Summary
static interface Benchmark.BenchmarkHandler

 
Method Summary
 
Methods inherited from class Object
getClass, hashCode, equals, toString, notify, notifyAll, wait, wait, wait
 

Groovy Documentation