perf4j使用一

          如果大家使用的是maven工程,那么現在pom文件中加入perf4j的依賴。

          <dependency>

          <groupId>org.perf4j</groupId>

          <artifactId>perf4j</artifactId>

          <version>0.9.16</version>

          <scope>compile</scope>

          </dependency>

          如果用的是普通工程,那么直接下載jar包放入lib目錄下即可。

           

           

          例子:

          package com.baowu.per4j;

           

          import org.perf4j.LoggingStopWatch;

          import org.perf4j.StopWatch;

           

          public class Example1 {

           

              public static void main(String[] args) throws InterruptedException{

                 method1();

                 method2();

                 method3();

              }

             

              /**

               * 監控一處代碼示例

               * @throws InterruptedException

               */

              private static void method1() throws InterruptedException{

                 //創建一個監控對象,這里使用LoggingStopWatch,它是把結果直接輸出到控制臺。我們也可以

                 //使用StopWatch的其他子類,比如:Log4JStopWatchCommonsLogStopWatch。不過這些子類需

                 //要工程使用日志框架

                 StopWatch stopWatch = new LoggingStopWatch("codeBlock1");

           

                 //這里就是一些需要監控的代碼,我們命名為codeBlock1

                 //使用線程休眠是為了模擬代碼執行時間

                 Thread.sleep((long)(Math.random() * 1000L));

           

                 //停止計算代碼性能

                 stopWatch.stop();

              }

             

              /**

               * 一個方法多出代碼監控

               * @throws InterruptedException

               */

              private static void method2() throws InterruptedException{

                 StopWatch stopWatch = new LoggingStopWatch();

                 Thread.sleep((long)(Math.random() * 1000L));

                 stopWatch.lap("codeBlock3");

                 Thread.sleep((long)(Math.random() * 1000L));

                 stopWatch.lap("codeBlock4");

                 Thread.sleep((long)(Math.random() * 1000L));

                 stopWatch.lap("codeBlock5");

                 Thread.sleep((long)(Math.random() * 1000L));

                 stopWatch.stop("codeBlock6");

              }

             

              /**

               * stop方法可以加入一些說明信息

               */

              private static void method3(){

                 StopWatch stopWatch = new LoggingStopWatch();

                 try {

                     // the code block being timed - this is just a dummy example

                     long sleepTime = (long)(Math.random() * 1000L);

                     Thread.sleep(sleepTime);

                     if (sleepTime > 500L) {

                         throw new Exception("Throwing exception");

                     }

                     stopWatch.stop("codeBlock2.success", "Sleep time was < 500 ms");

                 } catch (Exception e) {

                     stopWatch.stop("codeBlock2.failure", "Exception was: " + e);

                 }

              }

          }

           

          運行結果:

          start[1334457619937] time[355] tag[codeBlock1]

          start[1334457620296] time[152] tag[codeBlock3]

          start[1334457620453] time[138] tag[codeBlock4]

          start[1334457620593] time[598] tag[codeBlock5]

          start[1334457621187] time[700] tag[codeBlock6]

          start[1334457621890] time[619] tag[codeBlock2.failure] message[Exception was: java.lang.Exception: Throwing exception]

          posted on 2012-04-15 10:44 yangpingyu 閱讀(2990) 評論(0)  編輯  收藏 所屬分類: java基礎

          <2012年4月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導航

          統計

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          收藏夾

          linux

          產品交互

          分析,設計,架構

          安全

          技術牛人

          數據庫

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 福建省| 永修县| 宝鸡市| 大石桥市| 伊金霍洛旗| 宜兰市| 信丰县| 高平市| 资阳市| 运城市| 阜宁县| 兴仁县| 渭源县| 华安县| 孝昌县| 长丰县| 江津市| 阿勒泰市| 平山县| 根河市| 杭锦旗| 敦化市| 涡阳县| 松阳县| 思茅市| 永兴县| 综艺| 南雄市| 上虞市| 通海县| 云霄县| 长顺县| 海盐县| 封丘县| 墨江| 通榆县| 德化县| 安庆市| 临沂市| 鄄城县| 蒲江县|