在 Java 应用程序监视器 (JAMon) 中使用多个标签

Using multiple labels in Java Application Monitor (JAMon)

是否可以使用同一个 JAMon Monitor class 来监控不同的处理步骤?例如,在代码示例中,我想测量“Point1”和“Point2”的执行时间。但是,例子returns只统计了第二步。当然,我可以创建多个 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 Label=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, First Access=Wed Jun 17 10:40:44 CEST 2015, Last Access=Wed Jun 17 10:40:46 CEST 2015)

期望的输出:

JAMon Label=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, First Access=Wed Jun 17 10:40:44 CEST 2015, Last Access=Wed Jun 17 10:40:46 CEST 2015)

JAMon Label=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, First Access=Wed Jun 17 10:40:44 CEST 2015, Last Access=Wed Jun 17 10:40:46 CEST 2015)

您可以将监视器 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());

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

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

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

您可以查看完整的代码示例和输出 here