性能测试中记录每一个耗时请求

在之前的文章性能测试中标记每个请求中提到,把每一个接口都进行requestID的标记,接下来的工作就简单了,就是设置各种超时配置,然后进行压测,会记录超时的请求ID和响应时间(采取“响应时间_requestID”形式),结果如图:

5274_8816271578991944051
6352_8816271578991944325
8319_8826061578991944051
5336_8826061578991944370

中间在使用匿名类实现的时候遇到了一个坑,已经解决了:如何在匿名thread子类中保证线程安全

本文较上一篇文章更新,主要更新了MarkRequest的实现类,因为在Groovy的类中使用内部类中继承Serializable接口会报错,所以不再采取内部类的形式实现标记类。还有一个更新是初始化连接池的设置,重新配置各种超时配置以及重试配置,这里我选择了10s超时配置,不再重试直接记录错误。

脚本如下:

package com.funscript.teacherpad.flowfail

import com.fun.frame.excute.Concurrent
import com.fun.frame.httpclient.ClientManage
import com.fun.frame.httpclient.FanLibrary
import com.fun.frame.thead.HeaderMarkInt
import com.fun.frame.thead.RequestThreadTimes
import com.fun.utils.ArgsUtil
import com.okayqa.teacherpad.base.OkayBase
import com.okayqa.teacherpad.function.Klass

class GetClassOnline extends OkayBase {

    public static void main(String[] args) {
        def argsUtil = new ArgsUtil(args)
        def thread = argsUtil.getIntOrdefault(0, 2)
        def times = argsUtil.getIntOrdefault(1, 5)
        ClientManage.init(10000, 5000, 0)

        def base = getBase()

        def klass = new Klass(base)
        klass.getClassOline(47159)
        def request = FanLibrary.getLastRequest()
        def markInt = new HeaderMarkInt("requestid")

        def timesthread = new RequestThreadTimes(request, times, markInt)
        new Concurrent(timesthread, thread, "获取当前班级上课列表,内容流转二期压测接口").start()

        allOver()
    }
}

实现类如下:

package com.fun.frame.thead;

import com.fun.base.interfaces.MarkRequest;
import com.fun.frame.SourceCode;
import org.apache.http.client.methods.HttpRequestBase;

import java.io.Serializable;

public class HeaderMarkInt extends SourceCode implements MarkRequest, Cloneable, Serializable {

    private static final long serialVersionUID = -1595942567071153477L;

    String headerName;

    int i;

    int num = 1000_0000;

    @Override
    public String mark(HttpRequestBase request) {
        request.removeHeaders(headerName);
        i = i == 0 ? getRandomInt(8999) + 1000 : i;
        String value = 88 + EMPTY + i + num++;
        request.addHeader(headerName, value);
        return value;
    }

    @Override
    public HeaderMarkInt clone() {
        return new HeaderMarkInt(headerName);
    }

    public HeaderMarkInt(String headerName) {
        this.headerName = headerName;
    }


}

还有一个str实现类:

package com.fun.frame.thead;

import com.fun.base.interfaces.MarkRequest;
import com.fun.frame.SourceCode;
import com.fun.utils.RString;
import com.fun.utils.Time;
import org.apache.http.client.methods.HttpRequestBase;

import java.io.Serializable;

public class HeaderMarkStr extends SourceCode implements MarkRequest, Cloneable, Serializable {

    private static final long serialVersionUID = 3461028184513435518L;

    String headerName;

    String m;

    @Override
    public String mark(HttpRequestBase request) {
        request.removeHeaders(headerName);
        m = m == null ? RString.getStringWithoutNum(4) : m;
        String value = "fun_" + m + CONNECTOR + Time.getTimeStamp();
        request.addHeader(headerName, value);
        return value;
    }

    @Override
    public HeaderMarkStr clone() {
        return new HeaderMarkStr(headerName);
    }

    public HeaderMarkStr(String headerName) {
        this.headerName = headerName;
    }


}

初始化连接池的方法:

    /**
     * 重新初始化连接池,用于临时改变超时和超时标准线的重置
     *
     * @param timeout
     * @param accepttime
     * @param retrytime
     */
    public static void init(int timeout, int accepttime, int retrytime) {
        HttpClientConstant.CONNECT_REQUEST_TIMEOUT = timeout;
        HttpClientConstant.CONNECT_TIMEOUT = timeout;
        HttpClientConstant.SOCKET_TIMEOUT = timeout;
        HttpClientConstant.MAX_ACCEPT_TIME = accepttime;
        HttpClientConstant.TRY_TIMES = retrytime;
        requestConfig = getRequestConfig();
        httpsClient = getCloseableHttpsClients();
        httpRequestRetryHandler = getHttpRequestRetryHandler();
    }

有兴趣可以后台回复“git”,获取项目的git地址,注意分支。


  • 郑重声明:文章首发于公众号“FunTester”,禁止第三方(腾讯云除外)转载、发表。

技术类文章精选

非技术文章精选

posted @ 2020-01-27 12:51  FunTester  阅读(532)  评论(1编辑  收藏  举报