How to happily perform performance testing on the Linux command line interface? These 2 plans are amazing!

In the process of doing performance testing, I encountered a problem. The test machine chose a Linux server with only a command line interface. Executing test cases is not very flexible. Sometimes I need to change one or two parameters and add some logs, and I need to repackage and deploy. Although automated construction is more convenient, it feels like a big circle. After some simple attempts, it is done. two options

One is for stress testing of a single interface, completes the assembly of each request in the form of a configuration file, and then executes different test cases by adjusting concurrent parameters, and supports stress testing of multiple requests at the same time; the other executes test cases in the form of groovy scripts, You need to configure the groovy environment on the server and push the packaged jar package to the groovy lib directory.

Option One:

package com.fun.utils.request;

import com.fun.frame.SourceCode;
import com.fun.httpclient.FanLibrary;
import com.fun.profile.Constant;
import com.fun.utils.WriteRead;
import net.sf.json.JSONObject;
import org.apache.http.client.methods.HttpRequestBase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * 从文件中读取接口相关参数,用来发送请求,实现接口请求的配置化
 * <p>从当前路径下获取后缀为.log的文件,以文件名为准读取文件内容</p>
 */
public class RequestFile extends SourceCode {

    private static Logger logger = LoggerFactory.getLogger(RequestFile.class);

    String url;

    /**
     * get对应get请求,post对应post请求表单参数,其他对应post请求json参数
     */
    JSONObject headers;

    String requestType;

    String name;

    JSONObject info;

    JSONObject params;

    /**
     * @param name
     */
    public RequestFile(String name) {
        this.name = name;
        getInfo();
        this.url = this.info.getString("url");
        requestType = this.info.getString("requestType");
        getParams();
        headers = JSONObject.fromObject(this.info.getString("headers"));
    }

    /**
     * 获取当前目录下的配置文件,以数字开头,后缀是.log的
     *
     * @param i
     */
    public RequestFile(int i) {
        this(i + Constant.EMPTY);
    }

    /**
     * 从配置文件中读取信息,组成一个json对象
     */
    private void getInfo() {
        String filePath = Constant.WORK_SPACE + name;
        logger.info("配置文件地址:" + filePath);
        this.info = WriteRead.readTxtByJson(filePath);
    }

    /**
     * 获取请求参数
     */
    private void getParams() {
        params = JSONObject.fromObject(info.getString("params"));
    }


    /**
     * 根据info组成请求
     *
     * @return
     */
    public HttpRequestBase getRequest() {
        HttpRequestBase requestBase = requestType.equalsIgnoreCase(Constant.REQUEST_TYPE_POST) ? FanLibrary.getHttpPost(this.url, this.params) : requestType.equalsIgnoreCase(Constant.REQUEST_TYPE_GET) ? FanLibrary.getHttpGet(this.url, this.params) : FanLibrary.getHttpPost(this.url, this.params.toString());
        FanLibrary.addHeaders(requestBase, headers);
        FanLibrary.setHeaderKey();
        output(FanLibrary.getHttpResponse(requestBase));
        return requestBase;
    }

}
然后是通过main方法实现参数化:


class PerformanceFromFile extends SourceCode {
    public static void main(String[] args) {
        MySqlTest.setFlag();
        def size = args.size();
        List<HttpRequestBase> list = new ArrayList<>()
        for (int i = 0; i < size - 1; i += 2) {
            def name = args[i]
            int thread = changeStringToInt(args[i + 1])
            def request = new RequestFile(name).getRequest()
            for (int j = 0; j < thread; j++) {
                list.add(request)
            }
        }
        int perTimes = changeStringToInt(args[size - 1])
        def concurrent = new Concurrent(list, perTimes)
        concurrent.start()
        FanLibrary.testOver()
    }
}

The methods of the Concurrent class are not included here, and interested students can read the previous articles.

The command line to execute the use case:

java -jar performance.jar test 10 login 10 1000

To explain, the request of the test script allocates 10 threads, the request of the login script allocates 10 threads, and each thread executes 1000 requests. The following is the content of the test:

url=http://127.0.0.1:8050/api/pad/user/login
requestType=peost
params={"uname":"81951375115","pwd":"QJ81KU2LV6z1X4nA+czzvqVZVDsQnjOIKt857kEbemcs/SJW8GXL+sjOcemH5GFIm6rKKpqIOrqp1z0DUig/9QJouhBp1OQnZbNlkXSS84+IOQS022kbsN9e51r+GeyZDCrr7WWLenZJcyIE1BRrMeq1EkWCBotzwegXUJjR6Qs="}
headers={requestId:88888888}

Option One:

This is relatively simple. First, configure the groovy environment on the server, and then put the packaged jar of the interface function test and automation test project into the lib directory of groovy. Jenkins automated construction is used here, just add a line of mv or cp file shell in the post script.

Then create a new directory on the server to store the groovy script, and put a test script content below:

import com.fun.httpclient.FanLibrary
import com.okayqa.studentapd.base.OkayBase
import com.fun.frame.excute.Concurrent

class T8 extends OkayBase{
    public static void main(String[] args) {
        def base = getBase()
        output(base.getLoginResponse())
        def get = FanLibrary.requests.get(FanLibrary.requests.size() - 1)
      //  new Concurrent(get,10,100).start()
        FanLibrary.testOver()
    }
}

Then on the server through vim, you can flexibly edit scripts and execute different use cases, including managing logs, everything is no problem.

Execution method:

groovy test

Of course, you can also pass in parameters when calling.

The following are supporting learning materials. For friends who do [software testing], it should be the most comprehensive and complete preparation warehouse. This warehouse also accompanied me through the most difficult journey. I hope it can help you too!

Software testing interview applet

The software test question bank maxed out by millions of people! ! ! Who is who knows! ! ! The most comprehensive quiz mini program on the whole network, you can use your mobile phone to do the quizzes, on the subway or on the bus, roll it up!

The following interview question sections are covered:

1. Basic theory of software testing, 2. web, app, interface function testing, 3. network, 4. database, 5. linux

6. web, app, interface automation, 7. performance testing, 8. programming basics, 9. hr interview questions, 10. open test questions, 11. security testing, 12. computer basics

Information acquisition method:

Guess you like

Origin blog.csdn.net/jiangjunsss/article/details/132105748