接口自动化中的性能(jemter api)

做的事情:

在接口自动化回归测试加入封装好的jemter api做接口性能测试,完成测试数据记录
输出测试报告和数据库中,形成迭代版本性能走势趋势,找出新增功能在迭代中性能的表现


注意点:
由于是每日构建,性能测试时也要保持机器的硬件和网络的稳定性。
如果每次测试机器不一样那么性能数据受环境影响较大,无法直接看出是不是更新代码导致的。

Jmeter api
<dependency>
<groupId>kg.apc</groupId>
<artifactId>jmeter-plugins</artifactId>
<version>1.0.0</version>
</dependency>

 

部分主要设置

        // Engine
        StandardJMeterEngine jm = new StandardJMeterEngine();
        // jmeter.properties
        JMeterUtils.loadJMeterProperties("D://jmeter//bin//jmeter.properties");
        JMeterUtils.setJMeterHome("D://jmeter");
        JMeterUtils.setLocale(new Locale("ignoreResources"));
        // HTTP Sampler 接口信息
        HTTPSampler httpSampler = new HTTPSampler();
        httpSampler.setName("TestCodeproject");
        httpSampler.setDomain("test.test.cn");
        httpSampler.setPort(80);
        httpSampler.setProtocol("http");
        httpSampler.setMethod("GET");
        httpSampler.setPath("/"// Thread Group
        ThreadGroup threadGroup = new ThreadGroup();
        threadGroup.setName("Test");
        threadGroup.setNumThreads(2000);  //2000并发     
        //数据源
        MyResultCollector requestCollector = new MyResultCollector();

 

ResultCollector可得到数据(性能数据源):

public class MyResultCollector extends ResultCollector {
  @Override
    public void sampleOccurred(SampleEvent e) {
        super.sampleOccurred(e);
        SampleResult r = e.getResult();
        String threadGroupName= e.getThreadGroup();
        System.out.println(threadGroupName);

        System.out.println(r.getEndTime() +"  getEndTime");
        System.out.println(r.getStartTime() +"  getStartTime");
        System.out.println(r.getErrorCount() +"  getErrorCount");
        System.out.println(r.getIdleTime() +"  getIdleTime");
        System.out.println(r.getLatency() +"  getLatency");
        System.out.println(r.getTime() +"  getTime");
        System.out.println(r.getTimeStamp() +"  getTimeStamp");
        System.out.println(r.getSubResults() +"  getSubResults");
        System.out.println(Arrays.toString(r.getSubResults()) +"  getSubResults");
}

 


分析计算得出log数据:

------------------
MULE TotalTime0.647
MULE ExpectedThreads 1
MULE StartedThreads 2000
MULE StoppedThreads 2000
MULE Requests 2000
MULE Throughput 3092.7357032457494
MULE BytesPerSecond 238.59218025502318 KB
MULE BytesPerRequestAvg 78.99750124937532 B
MULE Error% 0.0
MULE AvgLatency 32.093453273363316
MULE MinLatency 21
MULE 50thPercentile 25
MULE 90thPercentile 39
MULE 95thPercentile 108
MULE 99thPercentile 141
MULE MaxLatency 165
------------------
DEBUG   2018-01-15 11:17:58.677 [jmeter.r] (): Closing: d://file.jtl
INFO    2018-01-15 11:17:59.365 [jmeter.r] (): summary =   2001 in     2s = 1298.5/s Avg:    32 Min:    21 Max:   165 Err:     0 (0.00%)
summary =   2001 in     2s = 1298.5/s Avg:    32 Min:    21 Max:   165 Err:     0 (0.00%)

 

写入接口自动化report(二次扩展zreport,写入性能数据):

 

posted @ 2018-01-14 23:19  q55091  阅读(537)  评论(2编辑  收藏  举报