使用 perf4J 时,此代码运行良好:
StopWatch stopWatch = new LoggingStopWatch();
stopWatch.stop("example1", "custom message text");
但是在使用@Profiled 时,如何以最少的代码输出或获取度量:
@Profiled(tag = "dynamicTag_{$0}")
public void boucle(int k)
{
// My code to profile
}
当使用@Profiled
注释将秒表添加到您的代码时,您通常会使用 AspectJ 加载时间编织 (LTW) 在加载/运行时检测您的代码,这会在带注释的方法周围添加秒表开始/停止和记录。
使用哪个日志库/API(log4j、slf4j、commons-logging、JUL)取决于org.perf4j.aop.ProfiledTimingAspect
您在aop.xml
配置 LTW 时配置的特定实例。例如这个配置:
<aspectj>
<!--
We only want to weave in the log4j TimingAspect into the @Profiled classes.
Note that Perf4J provides TimingAspects for the most popular Java logging
frameworks and facades: log4j, java.util.logging, Apache Commons Logging
and SLF4J. The TimingAspect you specify here will depend on which logging
framework you wish to use in your code.
-->
<aspects>
<aspect name="org.perf4j.log4j.aop.TimingAspect"/>
<!-- if SLF4J/logback use org.perf4j.slf4j.aop.TimingAspect instead -->
</aspects>
<weaver options="-verbose -showWeaveInfo">
<!--
Here is where we specify the classes to be woven. You can specify package
names like com.company.project.*
-->
<include within="ProfiledExample"/>
</weaver>
</aspectj>
... 使用org.perf4j.log4j.aop.TimingAspect
,因此秒表将记录到您配置的 Log4J 秒表记录器中。如果您想避免使用第三方库,您可以为 JUL JDK 记录器更改此设置。