0

是否可以使用相同的 JAMon Monitor 类来监控不同的处理步骤?例如,在代码示例中,我想测量“Point1”和“Point2”的执行时间。但是,该示例仅返回有关第二步的统计信息。当然,我可以创建几个 Monitor 类型的对象。但也许有更清洁的方法?

Monitor mon = null;     
for(int i =0; i < 1000; i++){
    //Part1
    mon = MonitorFactory.start("Point 1");
    Thread.sleep(2);
    mon.stop();     
    
    //Part2
    mon = MonitorFactory.start("Point 2");
    mon.stop();
}
    
System.out.println(mon.toString());

输出:

JAMon 标签=Point 2, Units=ms.: (LastValue=0.0, Hits=1000.0, Avg=0.001, Total=1.0, Min=0.0, Max=1.0, Active=0.0, Avg Active=1.0, Max Active=1.0,首次访问=2015 年 6 月 17 日星期三 10:40:44 CEST,最后访问=2015 年 6 月 17 日星期三 10:40:46 CEST)

期望的输出:

JAMon 标签=Point 1, Units=ms.: (LastValue=0.0, Hits=1000.0, Avg=0.001, Total=1.0, Min=0.0, Max=1.0, Active=0.0, Avg Active=1.0, Max Active=1.0,首次访问=2015 年 6 月 17 日星期三 10:40:44 CEST,最后访问=2015 年 6 月 17 日星期三 10:40:46 CEST)

JAMon 标签=Point 2, Units=ms.: (LastValue=0.0, Hits=1000.0, Avg=0.001, Total=1.0, Min=0.0, Max=1.0, Active=0.0, Avg Active=1.0, Max Active=1.0,首次访问=2015 年 6 月 17 日星期三 10:40:44 CEST,最后访问=2015 年 6 月 17 日星期三 10:40:46 CEST)

4

2 回答 2

0

您可以将监视器 toString() 调用更改为显式 getMonitor(...) 调用。

Monitor mon = null;   

for(int i =0; i < 1000; i++){
  //Part1
  mon = MonitorFactory.start("Point 1");
  Thread.sleep(2);
  mon.stop();     

  //Part2
  mon = MonitorFactory.start("Point 2");
  mon.stop();
}

System.out.println(MonitorFactory.getMonitor("Point 1").toString());
System.out.println(MonitorFactory.getMonitor("Point 2").toString());

或者您可以创建 2 个监视器。

Monitor mon1 = null;   
Monitor mon2 = null;     

for(int i =0; i < 1000; i++){
  //Part1
  mon1 = MonitorFactory.start("Point 1");
  Thread.sleep(2);
  mon1.stop();     

  //Part2
  mon2 = MonitorFactory.start("Point 2");
  mon2.stop();
}

System.out.println(mon1.toString());
System.out.println(mon2.toString());
于 2015-07-16T07:30:25.157 回答
0

有比其他答案更好的方法:

for( Object monitor: MonitorFactory.getMap().values() ){
    System.out.println( monitor ); 
}

这将打印到目前为止所有使用过的监视器。在所有线程中。正是你想要的。

您可以在此处查看已完成的代码示例和输出。

于 2015-11-27T15:02:26.590 回答