An annotation for benchmarking.
This annotation allows execution time measurement without modifying existing code.
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.defaultHandler":
groovy -cp gbench-xx.xx.xx.jar -Dgbench.defaultHandler="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
| Key | Value | Meaning |
|---|---|---|
| gbench.defaultHandler | expression | the default benchmark handler. |
| gbench.measureCpuTime | true, false | enables measuring CPU time. the default value is "true". |
| gbench.verbose | true, false | enables verbose output. the default value is "false". |
| gbench.quiet | true, false | suppress output. the default value is "false". |
| Modifiers | Name | Description |
|---|---|---|
static interface |
Benchmark.BenchmarkHandler |
|
static class |
Benchmark.DefaultBenchmarkHandler |
@default DefaultBenchmarkHandler.class