性能诊断
性能诊断是当RT升高时,能将事先所有对性能有影响的埋点按调用堆栈输出。性能埋点必须在事先埋好在代码中。
try { Profiler.enter(); // do something... }finally { Profiler.release(); }
性能埋点以Profiler.enter();
作为开始Profiler.release();
作为结束,大家可能比较容易忘记的是在finally中的release,切记切记,否则会得到错误的统计结果。
Example
调用代码 ``` private void sleep(long time) { try { Thread.sleep(time); } catch (InterruptedException e) { e.printStackTrace(); } }
private String trimName(String name) { try { Profiler.enter(); sleep(200); return name.trim(); }finally { Profiler.release(); } }
private String getPrefix() { try { Profiler.enter(); sleep(300); return getSayWords(); }finally { Profiler.release(); } }
private String getSayWords() { try { Profiler.enter(); sleep(400); return "hello:"; }finally { Profiler.release(); } } ```
输出样式
2012-07-15 12:25:59 [excavator-profiler]-[WARN] 0 - profilter:SayHelloService$sayTo `---1 [1,407ms (503ms)] - com.googlecode.excavator.demo.service.impl.SayHelloServiceImpl$sayTo +---504 [703ms (302ms), 50%] - com.googlecode.excavator.demo.service.impl.SayHelloServiceImpl$getPrefix | `---806 [401ms, 57%] - com.googlecode.excavator.demo.service.impl.SayHelloServiceImpl$getSayWords `---1,207 [201ms, 14%] - com.googlecode.excavator.demo.service.impl.SayHelloServiceImpl$trimName
开启参数 ```
profiler开启
excavator.profiler.enable=true
profiler告警阀值(选填,默认值为100,单位为ms)
excavator.profiler.limit=100 ```
这里需要解释的是
excavator.profiler.limit
参数,这个值的意思是当以下表达式满足时,才进行告警打印性能堆栈信息。timeout - cost <= ${excavator.profiler.limit}
理解过来的意思就是,当本次执行的剩余时间在告警阀值之内时,比如超时设置为1000ms,但本次调用已经执行到了950ms,虽然不会超时,但是已经很接近超时的限制了,可以认为有可能已经存在性能问题,所以此时将性能堆栈打印出来。