Perf4J 的目标是通过易于集成(和扩展)的开源软件包提供这些常用的 Java 性能监控功能。包括:
- 简洁的 stop watch计时机制;
- 提供命令行工具,从原始的日志文件中生成汇总的统计数据和性能图表;
- 定制的 log4j appender,可以在运行时应用中生成数据和图表,计划在以后的版本中支持 java.util.logging 和 logback
- 能够以 JMX 属性的形式发布性能数据,在数据超过指定阈值时发送通知;
- 提供 @Profiled 注解和一套自定义机制,允许在与 AOP 框架(如 AspectJ 或者 Spring AOP)集成时巧妙地计时。
一句话:Pef4j 之于 System.currentTimeMillis 就像 log4j 之于 System.out.println()
使用方法
- 引入 Maven 依赖
org.perf4j
perf4j
0.9.16
- 添加监控代码
public class Worker {
public void run() {
StopWatch stopWatch = new LoggingStopWatch("worker.run");
try {
int interval = ThreadLocalRandom.current().nextInt(1000);
Thread.sleep(interval);
stopWatch.stop("example1", "current message text: " + interval);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
- 执行,输出日志
start[1488182180833] time[302] tag[example1] message[current message text: 302]
start[1488182181135] time[329] tag[example1] message[current message text: 330]
start[1488182181465] time[385] tag[example1] message[current message text: 385]
start[1488182181850] time[675] tag[example1] message[current message text: 676]
start[1488182182526] time[437] tag[example1] message[current message text: 436]
start[1488182182963] time[706] tag[example1] message[current message text: 707]
start[1488182183670] time[674] tag[example1] message[current message text: 675]
start[1488182184345] time[28] tag[example1] message[current message text: 28]
start[1488182184373] time[376] tag[example1] message[current message text: 377]
start[1488182184750] time[817] tag[example1] message[current message text: 817]
start[1488182185567] time[667] tag[example1] message[current message text: 668]
start[1488182186235] time[411] tag[example1] message[current message text: 411]
start[1488182186646] time[43] tag[example1] message[current message text: 44]
start[1488182186690] time[30] tag[example1] message[current message text: 31]
start[1488182186721] time[958] tag[example1] message[current message text: 959]
start[1488182187680] time[767] tag[example1] message[current message text: 768]
start[1488182188448] time[234] tag[example1] message[current message text: 235]
start[1488182188683] time[822] tag[example1] message[current message text: 822]
start[1488182189505] time[856] tag[example1] message[current message text: 857]
start[1488182190362] time[270] tag[example1] message[current message text: 271]
start[1488182190633] time[709] tag[example1] message[current message text: 709]
start[1488182191342] time[704] tag[example1] message[current message text: 705]
start[1488182192047] time[816] tag[example1] message[current message text: 817]
start[1488182192864] time[218] tag[example1] message[current message text: 219]
start[1488182193083] time[542] tag[example1] message[current message text: 543]
start[1488182193626] time[327] tag[example1] message[current message text: 328]
start[1488182193954] time[435] tag[example1] message[current message text: 436]
start[1488182194390] time[277] tag[example1] message[current message text: 278]
start[1488182194668] time[298] tag[example1] message[current message text: 299]
start[1488182194967] time[577] tag[example1] message[current message text: 578]
start[1488182195545] time[35] tag[example1] message[current message text: 35]
start[1488182195580] time[584] tag[example1] message[current message text: 585]
start[1488182196165] time[558] tag[example1] message[current message text: 559]
start[1488182196724] time[854] tag[example1] message[current message text: 854]
start[1488182197578] time[447] tag[example1] message[current message text: 448]
start[1488182198026] time[800] tag[example1] message[current message text: 801]
start[1488182198827] time[40] tag[example1] message[current message text: 41]
start[1488182198868] time[342] tag[example1] message[current message text: 342]
start[1488182199210] time[734] tag[example1] message[current message text: 735]
start[1488182199945] time[306] tag[example1] message[current message text: 306]
start[1488182200251] time[409] tag[example1] message[current message text: 410]
start[1488182200661] time[298] tag[example1] message[current message text: 298]
start[1488182200959] time[507] tag[example1] message[current message text: 508]
start[1488182201467] time[997] tag[example1] message[current message text: 998]
start[1488182202465] time[848] tag[example1] message[current message text: 849]
start[1488182203314] time[769] tag[example1] message[current message text: 770]
start[1488182204084] time[423] tag[example1] message[current message text: 424]
start[1488182204508] time[755] tag[example1] message[current message text: 756]
start[1488182205264] time[271] tag[example1] message[current message text: 271]
start[1488182205535] time[214] tag[example1] message[current message text: 215]
start[1488182205750] time[394] tag[example1] message[current message text: 395]
start[1488182206145] time[544] tag[example1] message[current message text: 544]
start[1488182206689] time[486] tag[example1] message[current message text: 487]
start[1488182207176] time[249] tag[example1] message[current message text: 250]
start[1488182207426] time[661] tag[example1] message[current message text: 662]
start[1488182208088] time[889] tag[example1] message[current message text: 890]
start[1488182208978] time[351] tag[example1] message[current message text: 352]
start[1488182209330] time[530] tag[example1] message[current message text: 530]
start[1488182209860] time[816] tag[example1] message[current message text: 817]
start[1488182210677] time[302] tag[example1] message[current message text: 303]
start[1488182210980] time[513] tag[example1] message[current message text: 513]
start[1488182211493] time[307] tag[example1] message[current message text: 308]
start[1488182211803] time[400] tag[example1] message[current message text: 401]
start[1488182212204] time[116] tag[example1] message[current message text: 117]
start[1488182212321] time[397] tag[example1] message[current message text: 398]
start[1488182212719] time[19] tag[example1] message[current message text: 19]
start[1488182212738] time[88] tag[example1] message[current message text: 88]
start[1488182212826] time[660] tag[example1] message[current message text: 661]
start[1488182213487] time[293] tag[example1] message[current message text: 294]
start[1488182213781] time[60] tag[example1] message[current message text: 61]
start[1488182213842] time[806] tag[example1] message[current message text: 807]
start[1488182214649] time[192] tag[example1] message[current message text: 193]
start[1488182214842] time[160] tag[example1] message[current message text: 161]
start[1488182215003] time[665] tag[example1] message[current message text: 666]
start[1488182215669] time[141] tag[example1] message[current message text: 142]
start[1488182215811] time[595] tag[example1] message[current message text: 595]
start[1488182216406] time[790] tag[example1] message[current message text: 791]
start[1488182217197] time[665] tag[example1] message[current message text: 665]
start[1488182217862] time[957] tag[example1] message[current message text: 958]
start[1488182218820] time[303] tag[example1] message[current message text: 304]
start[1488182219124] time[818] tag[example1] message[current message text: 819]
start[1488182219943] time[649] tag[example1] message[current message text: 650]
start[1488182220593] time[371] tag[example1] message[current message text: 372]
start[1488182220965] time[173] tag[example1] message[current message text: 173]
start[1488182221138] time[300] tag[example1] message[current message text: 301]
start[1488182221439] time[504] tag[example1] message[current message text: 505]
start[1488182221944] time[519] tag[example1] message[current message text: 520]
start[1488182222464] time[217] tag[example1] message[current message text: 218]
start[1488182222682] time[875] tag[example1] message[current message text: 876]
start[1488182223558] time[431] tag[example1] message[current message text: 431]
start[1488182223989] time[517] tag[example1] message[current message text: 518]
start[1488182224507] time[460] tag[example1] message[current message text: 461]
start[1488182224968] time[731] tag[example1] message[current message text: 731]
start[1488182225699] time[550] tag[example1] message[current message text: 551]
start[1488182226250] time[151] tag[example1] message[current message text: 152]
start[1488182226402] time[157] tag[example1] message[current message text: 158]
start[1488182226560] time[382] tag[example1] message[current message text: 382]
start[1488182226942] time[482] tag[example1] message[current message text: 483]
start[1488182227425] time[263] tag[example1] message[current message text: 264]
start[1488182227689] time[936] tag[example1] message[current message text: 937]
- 性能日志分析
找到 pef4j 的 jar 包,输入如下命令:
java -jar perf4j-0.9.16.jar -g perf_out.html perf_log.txt -t 5000
上述命令会生成一个 perf_out.html 的网页,如下:
分析工具的使用帮助通过如下命令查看
java -jar perf4j-0.9.16.jar --help