2017-02-26 131 views
2

我正在使用以下代码从java应用程序运行JMeter加载测试。从java代码运行JMeter加载测试后获取汇总值

StandardJMeterEngine jmeter = new StandardJMeterEngine(); 

JMeterUtils.setJMeterHome(jmeterHome); 
JMeterUtils.loadJMeterProperties(jmeterProperties); 
JMeterUtils.initLogging(); 
JMeterUtils.initLocale(); 

HTTPSampler httpSampler = new HTTPSampler(); 
httpSampler.setDomain(host); 
httpSampler.setPort(port); 
httpSampler.setPath(path); 
httpSampler.setMethod("GET"); 
httpSampler.setName("load test"); 

LoopController loopController = new LoopController(); 
loopController.setLoops(1); 
loopController.setFirst(true); 
loopController.initialize(); 

ThreadGroup threadGroup = new ThreadGroup(); 
threadGroup.setName("Sample Thread Group"); 
threadGroup.setNumThreads(userCount); 
threadGroup.setRampUp(1); 
threadGroup.setSamplerController(loopController); 

HashTree testPlanTree = new HashTree(); 
TestPlan testPlan = new TestPlan("load test"); 
testPlanTree.add(testPlan); 
HashTree threadGroupHashTree = testPlanTree.add(testPlan, threadGroup); 
threadGroupHashTree.add(httpSampler); 

jmeter.configure(testPlanTree); 
jmeter.run(); 

运行此脚本后,我想要获取聚合摘要值,如平均等待时间。我知道总结报告可以登录到一个csv文件,然后我可以通过再次读取该文件来计算平均延迟。但我只想知道是否有任何方法可以在不写入和读取csv文件的情况下获得该聚合值?

我已经在这里提到以下文章和几个相关的问题。

https://www.blazemeter.com/blog/5-ways-launch-jmeter-test-without-using-jmeter-gui http://uttesh.blogspot.com/2015/04/jmeter-load-testing-by-code-jmeter-api.html

回答

1

一种方法是创建的是定制类像ResultCollector(对我来说,这是JmeterOutListener.java),并从那里收集的值,因为你有访问它们。你可以在那里用sampleOccurred方法做任何你想做的事情。

您可以对ResultCollector类进行解码并进一步了解它的功能。

JMeterTestFromCode.java

public class JMeterTestFromCode { 

    public static void main(String[] args) throws Exception{ 

     String jmeterHome1 = "/rezsystem/apache-jmeter-2.11/apache-jmeter-2.11"; 
     File jmeterHome=new File(jmeterHome1); 
     String slash = System.getProperty("file.separator"); 

     if (jmeterHome.exists()) { 
      File jmeterProperties = new File(jmeterHome.getPath() + slash + "bin" + slash + "jmeter.properties"); 
      if (jmeterProperties.exists()) { 
       //JMeter Engine 
       StandardJMeterEngine jmeter = new StandardJMeterEngine(); 

       //JMeter initialization (properties, log levels, locale, etc) 
       JMeterUtils.setJMeterHome(jmeterHome.getPath()); 
       JMeterUtils.loadJMeterProperties(jmeterProperties.getPath()); 
       JMeterUtils.initLogging();// you can comment this line out to see extra log messages of i.e. DEBUG level 
       JMeterUtils.initLocale(); 

       // JMeter Test Plan, basically JOrphan HashTree 
       HashTree testPlanTree = new HashTree();  

       // HTTP Sampler 
       HTTPSampler httpSampler = new HTTPSampler(); 
       httpSampler.setDomain("www.google.com"); 
       httpSampler.setPort(80); 
       httpSampler.setPath("/"); 
       httpSampler.setMethod("GET"); 

       // Loop Controller 
       TestElement loopCtrl = new LoopController(); 
       ((LoopController)loopCtrl).setLoops(1); 
       ((LoopController)loopCtrl).addTestElement(httpSampler); 
       ((LoopController)loopCtrl).setFirst(true); 

       // Thread Group 
       SetupThreadGroup threadGroup = new SetupThreadGroup(); 
       threadGroup.setNumThreads(1); 
       threadGroup.setRampUp(1); 
       threadGroup.setSamplerController((LoopController)loopCtrl); 

       // Test plan 
       TestPlan testPlan = new TestPlan("MY TEST PLAN"); 

       testPlanTree.add("testPlan", testPlan); 
       testPlanTree.add("loopCtrl", loopCtrl); 
       testPlanTree.add("threadGroup", threadGroup); 
       testPlanTree.add("httpSampler", httpSampler);  

       JmeterOutListener jmeterOutListener = new JmeterOutListener(); 
       testPlanTree.add(testPlanTree.getArray()[0], jmeterOutListener); 

       // Run Test Plan 
       jmeter.configure(testPlanTree); 
       jmeter.run(); 

       System.exit(0); 

      } 
     } 

     System.err.println("jmeter.home property is not set or pointing to incorrect location"); 
     System.exit(1);  

    } 
} 

JmeterOutListener.java

public class JmeterOutListener extends AbstractListenerElement implements SampleListener,Clearable,Serializable,TestListener,Remoteable,NoThreadClone{ 

    public JmeterOutListener() { 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
    public void testEnded() { 
     // TODO Auto-generated method stub 
    } 

    @Override 
    public void testEnded(String arg0) { 
     // TODO Auto-generated method stub 
    } 

    @Override 
    public void testStarted() { 
     // TODO Auto-generated method stub 
    } 

    @Override 
    public void testStarted(String arg0) { 
     // TODO Auto-generated method stub 
    } 

    @Override 
    public void testIterationStart(LoopIterationEvent arg0) { 
     // TODO Auto-generated method stub 
    } 

    @Override 
    public void clearData() { 
     // TODO Auto-generated method stub 
    } 

    @Override 
    public void sampleOccurred(SampleEvent event) { 

     SampleResult sample = event.getResult(); 

     System.out.println("sampleOccurred().sample.getTimeStamp() : " + sample.getTimeStamp()); 
     System.out.println("sampleOccurred().sample.getTime() : " + sample.getTime()); 
     System.out.println("sampleOccurred().sample.getSampleLabel() : " + sample.getSampleLabel()); 
     System.out.println("sampleOccurred().sample.getResponseCode() : " + sample.getResponseCode()); 
     System.out.println("sampleOccurred().sample.getResponseMessage() : " + sample.getResponseMessage()); 
     System.out.println("sampleOccurred().sample.getThreadName() : " + sample.getThreadName()); 
     System.out.println("sampleOccurred().sample.isSuccessful() : " + sample.isSuccessful()); 

     String    message    = null; 
     AssertionResult[] results    = sample.getAssertionResults(); 
     if (results != null) 
     { 
      for (int i = 0; i < results.length; ++i) { 
       message = results[i].getFailureMessage(); 
       System.out.println("sampleOccurred().message : " + message); 
       if (message != null) { 
        break; 
       } 
      } 
     } 

     System.out.println("sampleOccurred().sample.getBytes() : " + sample.getBytes()); 
     System.out.println("sampleOccurred().sample.getGroupThreads() : " + sample.getGroupThreads()); 
     System.out.println("sampleOccurred().sample.getAllThreads() : " + sample.getAllThreads()); 
     System.out.println("sampleOccurred().sample.getURL() : " + sample.getURL()); 
     System.out.println("sampleOccurred().sample.getLatency() : " + sample.getLatency()); 
     System.out.println("sampleOccurred().sample.getDataEncodingWithDefault() : " + sample.getDataEncodingWithDefault()); 
     System.out.println("sampleOccurred().sample.getSampleCount() : " + sample.getSampleCount()); 
     System.out.println("sampleOccurred().sample.getErrorCount() : " + sample.getErrorCount()); 

    } 

    @Override 
    public void sampleStarted(SampleEvent arg0) { 
     // TODO Auto-generated method stub 
    } 

    @Override 
    public void sampleStopped(SampleEvent arg0) { 
     // TODO Auto-generated method stub 
    } 

}