前面分析了Visualizer,今天看下他对应的testelement-ResultCollector
ResultCollector实现了SampleListener, Clearable, Serializable,TestStateListener, Remoteable, NoThreadClone这几个接口
一个一个的看下
SampleListener接口 notification on events occuring during the sampling process
Clearable接口 同上
Serializable接口 可序列化
TestStateListener接口 测试开始的过程中实现了这个接口的类可以被回调
Remoteable接口 以后分析
NoThreadClone接口 标记接口,标记了这个接口的testelement,在多个test run之间不会被clone
重点看实现了TestStateListener接口的方法和实现了SampleListener接口的方法
testStarted testEnded sampleOccurred
1 @Override 2 public void testStarted(String host) { 3 synchronized(LOCK){ 4 if (instanceCount == 0) { // Only add the hook once 5 shutdownHook = new Thread(new ShutdownHook()); 6 Runtime.getRuntime().addShutdownHook(shutdownHook); 7 } 8 instanceCount++; 9 try { 10 initializeFileOutput(); 11 if (getVisualizer() != null) { 12 this.isStats = getVisualizer().isStats(); 13 } 14 } catch (Exception e) { 15 log.error("", e); 16 } 17 } 18 inTest = true; 19 20 if(summariser != null) { 21 summariser.testStarted(host); 22 } 23 }
1 @Override 2 public void testEnded(String host) { 3 synchronized(LOCK){ 4 instanceCount--; 5 if (instanceCount <= 0) { 6 // No need for the hook now 7 Runtime.getRuntime().removeShutdownHook(shutdownHook); 8 finalizeFileOutput(); 9 inTest = false; 10 } 11 } 12 13 if(summariser != null) { 14 summariser.testEnded(host); 15 } 16 }
1 @Override 2 public void sampleOccurred(SampleEvent event) { 3 SampleResult result = event.getResult(); 4 5 if (isSampleWanted(result.isSuccessful())) { 6 sendToVisualizer(result); 7 if (out != null && !isResultMarked(result) && !this.isStats) { 8 SampleSaveConfiguration config = getSaveConfig(); 9 result.setSaveConfig(config); 10 try { 11 if (config.saveAsXml()) { 12 SaveService.saveSampleResult(event, out); 13 } else { // !saveAsXml 14 String savee = CSVSaveService.resultToDelimitedString(event); 15 out.println(savee); 16 } 17 } catch (Exception err) { 18 log.error("Error trying to record a sample", err); // should throw exception back to caller 19 } 20 } 21 } 22 23 if(summariser != null) { 24 summariser.sampleOccurred(event); 25 } 26 }
其中涉及到的SaveService,CSVSaveService都是帮助保存文件的辅助类