Simple Benchmark Aspect
16/07/10 19:22
The Aspect itself:
public aspect BenchmarkAspect issingleton() {
private Map _cumulativeBenchmarks = new HashMap() ;
public pointcut benchmarked(): call(@Benchmark * *(..)) ;
Object around():benchmarked(){
MethodSignature ms = (MethodSignature)thisJoinPointStaticPart.getSignature() ;
Method m = ms.getMethod() ;
Benchmark benchAnn = m.getAnnotation(Benchmark.class) ;
String benchKey = benchAnn.name() ;
Long curBenchValue = _cumulativeBenchmarks.get(benchKey) ;
if (curBenchValue == null)
curBenchValue = new Long(0L) ;
final long start = System.nanoTime() ;
Object result = proceed() ;
final long end = System.nanoTime() ;
curBenchValue += (end - start) ;
_cumulativeBenchmarks.put(benchKey, curBenchValue) ;
return result ;
}
public void resetBenchmarks() {
_cumulativeBenchmarks = new HashMap() ;
}
public Map getBenchmarks() {
return _cumulativeBenchmarks ;
}
}
The annotation to mark methods to be benchmarked:
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface Benchmark {
String name() ;
}
Example of annotated method:
@Benchmarg(name="mybenchmark1")
public void method1()
{
...
}
The cumul of benchmarks being done based on the name given in the annotation, you may sum time spend in several different methods under the same key.
Example of out to output results:
for(Map.Entry e : BenchmarkAspect.aspectOf().getBenchmarks().entrySet() ) {
System.out.println(e.getKey() + "=" + (float)((e.getValue() / 1000) / 1000) / 1000.0f + " secs") ;
}
public aspect BenchmarkAspect issingleton() {
private Map
public pointcut benchmarked(): call(@Benchmark * *(..)) ;
Object around():benchmarked(){
MethodSignature ms = (MethodSignature)thisJoinPointStaticPart.getSignature() ;
Method m = ms.getMethod() ;
Benchmark benchAnn = m.getAnnotation(Benchmark.class) ;
String benchKey = benchAnn.name() ;
Long curBenchValue = _cumulativeBenchmarks.get(benchKey) ;
if (curBenchValue == null)
curBenchValue = new Long(0L) ;
final long start = System.nanoTime() ;
Object result = proceed() ;
final long end = System.nanoTime() ;
curBenchValue += (end - start) ;
_cumulativeBenchmarks.put(benchKey, curBenchValue) ;
return result ;
}
public void resetBenchmarks() {
_cumulativeBenchmarks = new HashMap
}
public Map
return _cumulativeBenchmarks ;
}
}
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface Benchmark {
String name() ;
}
Example of annotated method:
@Benchmarg(name="mybenchmark1")
public void method1()
{
...
}
The cumul of benchmarks being done based on the name given in the annotation, you may sum time spend in several different methods under the same key.
Example of out to output results:
for(Map.Entry
System.out.println(e.getKey() + "=" + (float)((e.getValue() / 1000) / 1000) / 1000.0f + " secs") ;
}