PerformanceMonitorAspect { @Around(value = "@annotation(annot)" + " && execution(@org.spf4j.annotations.PerformanceMonitor * *(..))", argNames = "pjp,annot") public Object performanceMonitoredMethod(final ProceedingJoinPoint pjp, final PerformanceMonitor annot) throws Throwable { final long start = TimeSource.nanoTime(); Object result = pjp.proceed(); final long elapsedNanos = TimeSource.nanoTime() - start; final long elapsed = TimeUnit.NANOSECONDS.toMillis(elapsedNanos); MeasurementRecorderSource mrs = REC_SOURCES.getUnchecked(annot.recorderSource()); mrs.getRecorder(pjp.toLongString()).record(elapsed); final long warnThresholdMillis = annot.warnThresholdMillis(); if (elapsed > warnThresholdMillis) { final long errorThresholdMillis = annot.errorThresholdMillis(); if (elapsed > errorThresholdMillis) { LOG.error("Execution time {} ms for {} exceeds error threshold of {} ms, arguments {}", elapsed, pjp.toShortString(), errorThresholdMillis, pjp.getArgs()); } else { LOG.warn("Execution time {} ms for {} exceeds warning threshold of {} ms, arguments {}", elapsed, pjp.toShortString(), warnThresholdMillis, pjp.getArgs()); } } else { if (annot.defaultInfoLog()) { LOG.info("Execution time {} ms for {}, arguments {}", elapsed, pjp.toShortString(), pjp.getArgs()); } else { LOG.debug("Execution time {} ms for {}, arguments {}", elapsed, pjp.toShortString(), pjp.getArgs()); } } return result; }  @Around(value = "@annotation(annot)" + " && execution(@org.spf4j.annotations.PerformanceMonitor * *(..))", argNames = "pjp,annot") Object performanceMonitoredMethod(final ProceedingJoinPoint pjp, final PerformanceMonitor annot);  }
@Test public void testPerformanceMonitoredMethod() throws Exception { TestLoggers ls = TestLoggers.sys(); LogCollection<List<TestLogRecord>> collection = ls.collect("org.aspectj.weaver.bcel", org.spf4j.log.Level.ERROR, true, Collectors.toList()); Registry.export(this); LogAssert expect = ls.expect("org.spf4j.perf.aspects.PerformanceMonitorAspect", Level.WARN, LogMatchers.hasFormat("Execution time {} ms for {} exceeds warning threshold of {} ms, arguments {}")); for (int i = 0; i < 10; i++) { somethingTomeasure(i, "Test"); } expect.assertObservation(); List<TestLogRecord> errors = collection.get(); if (!errors.isEmpty()) { LOG.info("aspectj misbehaves:", errors); } }