我想將有關sampleResult的信息保存到MySQL中。所以我寫我自己的名爲性能記錄器的偵聽器。它可以在我的Windows上正常工作。但是當我將相同的JMeter Script移到Linux中時,它失敗了。以下是關於我的自定義偵聽器的一些代碼。我的自定義JMeter偵聽器無法在Linux中工作
LoggerVisualizer.class
public class LoggerVisualizer extends AbstractVisualizer {
private PerformanceResultCollector collector;
public LoggerVisualizer() {
super();
initConfiguration();
}
private void initConfiguration() {
// some code
}
@Override
public void clearData() {
//some codes
}
public String getStaticLabel() {
return "Performance Logger";
}
public String getLabelResource(){
return "Performance Logger";
}
@Override
public void add(SampleResult sampleResult) {
}
public TestElement createTestElement() {
if (collector == null) {
collector = new PerformanceResultCollector();
}
modifyTestElement(collector);
return collector;
}
}
PerformanceResultCollector.class
public class PerformanceResultCollector extends ResultCollector implements Serializable {
CustomCollector collector;
public PerformanceResultCollector() {
log.info("construct performance collector")
collector = new CustomCollector();
}
@Override
public void sampleOccurred(SampleEvent e) {
collector.addSample(e.getResult());
}
public void cleanUp(){
CustomCollector.cleanUp();
}
}
在我的Windows服務器,我添加了監聽器,它工作得很好。我可以看到MySQL服務器中的數據。但是,當我移動腳本到Linux,並用命令
nohup ./jmeter -n -t /opt/tests/test.jmx -l /opt/log/perf_result.csv -Jjmeter.save.saveservice.output_format=csv -Jjmeter.save.saveservice.hostname=true -Jjmeter.save.saveservice.print_field_names=false -Jjmeter.save.saveservice.url=true -Jjmeter.save.saveservice.thread_counts=true -Jjmeter.save.saveservice.timestamp_format="yyyy-MM-dd HH:mm:ss" > /opt/log/jmeter_script_run.out 2> /opt/log/jmeter_script_run.err < /dev/null
運行它,我得到的錯誤jmeter.log這樣的:
2015/08/08 04:36:28 INFO - com.test.jmeter.logger.PerformanceResultCollector: construct performance collector
2015/08/08 04:36:28 ERROR - jmeter.save.SaveService: Conversion error com.thoughtworks.xstream.converters.ConversionException: java.lang.NullPointerException : java.lang.NullPointerException
---- Debugging information ----
message : java.lang.NullPointerException
cause-exception : java.lang.RuntimeException
cause-message : java.lang.NullPointerException
class : com.test.jmeter.logger.PerformanceResultCollector
required-type : com.test.jmeter.logger.PerformanceResultCollector
converter-type : org.apache.jmeter.save.converters.TestElementConverter
path : /jmeterTestPlan/hashTree/hashTree/hashTree/com.test.jmeter.logger.PerformanceResultCollector
line number : 379
class[1] : org.apache.jorphan.collections.ListedHashTree
converter-type[1] : org.apache.jmeter.save.converters.HashTreeConverter
------------------------------- : java.lang.NullPointerException : java.lang.NullPointerException
---- Debugging information ----
message : java.lang.NullPointerException
cause-exception : java.lang.RuntimeException
cause-message : java.lang.NullPointerException
class : com.test.jmeter.logger.PerformanceResultCollector
required-type : com.test.jmeter.logger.PerformanceResultCollector
converter-type : org.apache.jmeter.save.converters.TestElementConverter
path : /jmeterTestPlan/hashTree/hashTree/hashTree/com.test.jmeter.logger.PerformanceResultCollector
line number : 379
class[1] : org.apache.jorphan.collections.ListedHashTree
converter-type[1] : org.apache.jmeter.save.converters.HashTreeConverter
在日誌中,我發現它已經alreay來到PerformanceResultCollector的構造函數中。但爲什麼它拋出NulPointerException。我確認Linux上的jmeter的lib和Windows上的一樣。任何人都可以告訴我聽衆有什麼問題嗎?非常感謝
後,我已經修改了我的代碼很多次,我終於知道如何解決它,甚至通過我不知道原因。在PerformanceResultCollector.class中,如果我構造了惰性CustomCollector,它將起作用。
public class PerformanceResultCollector extends ResultCollector implements Serializable {
CustomCollector collector;
public PerformanceResultCollector() {
log.info("construct performance collector")
}
@Override
public void sampleOccurred(SampleEvent e) {
if (null == collector) {
collector = new CustomCollector();
}
collector.addSample(e.getResult());
}
public void cleanUp(){
CustomCollector.cleanUp();
}
}
也許JMeter以非gui和gui模式以不同的方式解析jmx腳本。
你能在379行顯示PerformanceResultCollector的代碼嗎? –
不確定它是否有幫助,但需要注意的兩件事是文件路徑分隔符和行結束符。 – cjcroix
@UBIK LOAD PACK PerformanceResultCollector.class只有54行。 379行可能在SaveService.class – user2256235