blob: ad0202ad0e45eded0c3e012f8b2251620d918709 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
package test;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
public class Main {
public static void main(String[] args) {
new Main().foo();
}
@PerformenceMonitor(expected=1000)
public void foo() {
}
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@interface PerformenceMonitor {
public int expected();
}
@Aspect
class Monitor {
@Pointcut("execution(@PerformenceMonitor * *(..)) && @annotation(monitoringAnnot)")
public void monitored(PerformenceMonitor monitoringAnnot) {}
@Around("monitored(monitoringAnnot)")
public Object flagExpectationMismatch(ProceedingJoinPoint pjp, PerformenceMonitor monitoringAnnot) {
long start = System.nanoTime();
Object ret = pjp.proceed();
long end = System.nanoTime();
if(end - start > monitoringAnnot.expected()) {
System.out.println("Method " + pjp.getSignature().toShortString() + " took longer than expected\n\t"
+ "Max expected = " + monitoringAnnot.expected() + ", actual = " + (end-start));
}
return ret;
}
}
|