基于HTTP请求的多线程实现类--视频讲解

2020-05-01 18:23:39 浏览数 (1)

相信一万行代码的理论!

上期将了定时和定量两种压测模式的虚拟类,本期分享一下基于单个HTTP请求对象HTTPrequestbase的两个压测模式的具体实现类。比较关键的就是GCThread的启动和结束,还有就是doing()方法的实现,就是把HTTPrequestbase对象发送请求然后解析响应,这里并没有去管响应结果的校验和断言,原因就是比较复杂,需要具体情况具体处理,难以通过一个通用的方法校验,还有一个原因就是很多时候没必要,可以通过监控服务端日志和其他统计方式统计相关业务数据来达到判断所有请求是否有报错和不成功的请求。其中应该着重注意就是对象拷贝,不管是多线程类对象还是HTTPrequestbase对象,如果不实现clone()方法,可能会有BUG。

性能测试系列视频如下:

  • 性能框架多线程基类和执行类--视频讲解
  • 定时和定量压测模式实现--视频讲解
  • 接口测试视频基础部分已经录完了,后台回复“接口视频”可观看完整接口测试视频

基于HTTP请求的多线程实现类

gitee地址:https://gitee.com/fanapi/tester

定量模式

代码语言:javascript复制
package com.fun.frame.thead;

import com.fun.base.constaint.ThreadLimitTimesCount;
import com.fun.base.interfaces.MarkThread;
import com.fun.frame.httpclient.FanLibrary;
import com.fun.frame.httpclient.FunRequest;
import com.fun.frame.httpclient.GCThread;
import org.apache.http.client.methods.HttpRequestBase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * http请求多线程类
 */
public class RequestThreadTimes extends ThreadLimitTimesCount {

    static Logger logger = LoggerFactory.getLogger(RequestThreadTimes.class);

    /**
     * 请求
     */
    public HttpRequestBase request;

    /**
     * 单请求多线程多次任务构造方法
     *
     * @param request 被执行的请求
     * @param times   每个线程运行的次数
     */
    public RequestThreadTimes(HttpRequestBase request, int times) {
        super(null, times, null);
        this.request = request;
    }

    /**
     * 应对对每个请求进行标记的情况
     *
     * @param request
     * @param times
     * @param mark
     */
    public RequestThreadTimes(HttpRequestBase request, int times, MarkThread mark) {
        super(null, times, mark);
        this.request = request;
    }

    protected RequestThreadTimes() {
        super();
    }

    @Override
    public void before() {
        super.before();
        GCThread.starts();
    }

    /**
     * @throws Exception
     */
    @Override
    protected void doing() throws Exception {
        FanLibrary.excuteSimlple(request);
    }

    @Override
    public RequestThreadTimes clone() {
        RequestThreadTimes threadTimes = new RequestThreadTimes();
        threadTimes.times = this.times;
        threadTimes.request = FunRequest.cloneRequest(request);
        threadTimes.mark = mark == null ? null : mark.clone();
        return threadTimes;
    }


}

定时模式

代码语言:javascript复制
package com.fun.frame.thead;

import com.fun.base.constaint.ThreadLimitTimeCount;
import com.fun.base.interfaces.MarkThread;
import com.fun.frame.httpclient.FanLibrary;
import com.fun.frame.httpclient.FunRequest;
import com.fun.frame.httpclient.GCThread;
import org.apache.http.client.methods.HttpRequestBase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * http请求多线程类
 */
public class RequestThreadTime extends ThreadLimitTimeCount {

    static Logger logger = LoggerFactory.getLogger(RequestThreadTime.class);

    /**
     * 请求
     */
    public HttpRequestBase request;

    /**
     * 单请求多线程多次任务构造方法
     *
     * @param request 被执行的请求
     * @param time    每个线程运行的次数
     */
    public RequestThreadTime(HttpRequestBase request, int time) {
        super(null,time,null);
        this.request = request;
    }

    /**
     * @param request 被执行的请求
     * @param time    执行时间
     * @param mark    标记类对象
     */
    public RequestThreadTime(HttpRequestBase request, int time, MarkThread mark) {
        super(null,time, mark);
        this.request = request;
    }

    protected RequestThreadTime() {
        super();
    }

    @Override
    public void before() {
        super.before();
        GCThread.starts();
    }

    @Override
    protected void doing() throws Exception {
        FanLibrary.excuteSimlple(request);
    }


    @Override
    public RequestThreadTime clone() {
        RequestThreadTime threadTime = new RequestThreadTime();
        threadTime.time = this.time;
        threadTime.request = FunRequest.cloneRequest(request);
        threadTime.mark = mark == null ? null : mark.clone();
        return threadTime;
    }


}

0 人点赞