2016-02-25 147 views
0

工作後,我的性能測試的流程需要火腿顯示器要經過下列步驟操作:創建火腿不序列化+反序列化

  1. 系列化
  2. 反序列化的停止
  3. 反序列化副本

但是下面的方案不起作用(步驟4沒有效果)。

您能否爲我提供解決方法?

證明與代碼

public class Main { 

    protected static void monitorSomething() throws InterruptedException, IOException, ClassNotFoundException{ 
     Monitor outerMonitor = MonitorFactory.start("outerMonitor"); 
     for (int i=1; i<=10; i++) { 
      Monitor inner = MonitorFactory.start("myInnerMonitor"); 
      Monitor serializedMonitorSource = MonitorFactory.start("serializedMonitor"); 
      Thread.sleep(100+i); 
      byte[] serialized = serialize(serializedMonitorSource); 
      Monitor serializedMonitorDestination = (Monitor)deserialize(serialized); 
      serializedMonitorDestination.stop(); 
      inner.stop(); 
     } 
     outerMonitor.stop(); 
     MonitorFactory.start("mySecondMonitor") 
         .stop(); 

    } 

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

    } 

    public static void main(String[] args) throws InterruptedException, IOException, ClassNotFoundException { 
     monitorSomething(); 
     printPerformanceMeasurements(); 
    } 

    private static byte[] serialize(Serializable serializable) throws IOException { 
     try (ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
      ObjectOutput out = new ObjectOutputStream(bos)) { 
      out.writeObject(serializable); 
      byte[] messageBytes = bos.toByteArray(); 
      return messageBytes; 
     } 

    } 

    private static Object deserialize(byte[] bytes) throws IOException, ClassNotFoundException{ 
     try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes); 
      ObjectInput in = new ObjectInputStream(bis)) { 
      return in.readObject(); 
     }   
    } 
} 

輸出:即無serializedMonitor的被測量

JAMon Label=myInnerMonitor, Units=ms.: (LastValue=114.0, Hits=10.0, Avg=114.2, Total=1142.0, Min=105.0, Max=136.0, Active=0.0, Avg Active=1.0, Max Active=1.0, First Access=Thu Feb 25 22:01:38 EET 2016, Last Access=Thu Feb 25 22:01:39 EET 2016) 
JAMon Label=outerMonitor, Units=ms.: (LastValue=1143.0, Hits=1.0, Avg=1143.0, Total=1143.0, Min=1143.0, Max=1143.0, Active=0.0, Avg Active=1.0, Max Active=1.0, First Access=Thu Feb 25 22:01:39 EET 2016, Last Access=Thu Feb 25 22:01:39 EET 2016) 
JAMon Label=mySecondMonitor, Units=ms.: (LastValue=0.0, Hits=1.0, Avg=0.0, Total=0.0, Min=0.0, Max=0.0, Active=0.0, Avg Active=1.0, Max Active=1.0, First Access=Thu Feb 25 22:01:39 EET 2016, Last Access=Thu Feb 25 22:01:39 EET 2016) 
JAMon Label=serializedMonitor, Units=ms.: (LastValue=0.0, Hits=0.0, Avg=0.0, Total=0.0, Min=1.7976931348623157E308, Max=-1.7976931348623157E308, Active=10.0, Avg Active=0.0, Max Active=0.0, First Access=Thu Jan 01 02:00:00 EET 1970, Last Access=Thu Jan 01 02:00:00 EET 1970) 
JAMon Label=com.jamonapi.Exceptions, Units=Exception: (LastValue=0.0, Hits=0.0, Avg=0.0, Total=0.0, Min=1.7976931348623157E308, Max=-1.7976931348623157E308, Active=0.0, Avg Active=0.0, Max Active=0.0, First Access=Thu Jan 01 02:00:00 EET 1970, Last Access=Thu Jan 01 02:00:00 EET 1970) 

注(活性= 10)

+0

因爲反序列化後沒有相對於當前火腿實例。反序列化後,你有一個序列化的所有東西的新實例。 –

+0

10倍的重播。如何解決這個問題? – joro

+0

解決方法是什麼?你不應該首先序列化這些東西。 –

回答

0

一種解決方法是序列化不整個Monitor對象,但只有開始的時刻。在測量點,您可以反序列化那一刻,並計算從現在開始的時間差。然後將計算的值添加到MonitorFactory統計池。

你得到時間執行統計(最小,最大,平均)。缺點是你失去了主動執行的統計信息。

示例代碼:

//   Monitor serializedMonitorSource = MonitorFactory.start("serializedMonitor"); 
     Date measuringStartOriginal = new Date(); 
     byte[] serialized = serialize(measuringStartOriginal); 
     Thread.sleep(100+i); 
//   Monitor serializedMonitorDestination = (Monitor)deserialize(serialized); 
     Date measuringStartDeserialized = (Date)deserialize(serialized); 
//   serializedMonitorDestination.stop(); 
     Date measuringEnd = (new Date()); 
     long timeDifference = measuringEnd.getTime() - measuringStartDeserialized.getTime(); 
     MonitorFactory.add("timeDifference", "ms", timeDifference); 

結果:

JAMon Label=timeDifference, Units=ms: (LastValue=110.0, Hits=10.0, Avg=106.3, Total=1063.0, Min=102.0, Max=110.0, Active=0.0, Avg Active=0.0, Max Active=0.0, First Access=Mon Feb 29 10:43:57 EET 2016, Last Access=Mon Feb 29 10:43:58 EET 2016)