How to Enjoy Performance Testing on Linux Command Line Interface

Keywords: Programming JSON Linux Apache Java

In the process of performance testing, I encountered a problem. The tester chose a Linux server with only command line interface. Execution of test cases is not very flexible. Sometimes I need to change one or two parameters to add some logs and redeploy them. Although automated construction is convenient, it feels like a big circle. After some simple attempts, two solutions have been made. One is pressure testing for a single interface to configure the text. Component form completes the assembly of each request, and then executes different test cases by adjusting concurrent parameters, and supports multiple requests to pressure together; another uses groovy script form, which requires groovy environment on the server and jar packages packaged into groovy's lib directory. .

Programme 1:

Read request assembly from text:

First read the class that assembles the request from the text:

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;


/**
 * Read interface parameters from files to send requests and configure interface requests
 * <p>Get the file suffixed. log from the current path and read the file content quasi-by the file name</p>
 */
public class RequestFile extends SourceCode {

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

    String url;

    /**
     * get For get request, post corresponds to form parameters of post request and other json parameters of post request
     */
    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"));
    }

    /**
     * Gets the configuration file in the current directory, starting with a number and suffixed with. log
     *
     * @param i
     */
    public RequestFile(int i) {
        this(i + Constant.EMPTY);
    }

    /**
     * Read the information from the configuration file to form a json object
     */
    private void getInfo() {
        String filePath = Constant.WORK_SPACE + name;
        logger.info("Configuration file address:" + filePath);
        this.info = WriteRead.readTxtByJson(filePath);
    }

    /**
     * Get the request parameters
     */
    private void getParams() {
        params = JSONObject.fromObject(info.getString("params"));
    }


    /**
     * Compose requests according to 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;
    }

}

Then parameterization is achieved by main method:


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()
    }
}

  • There is no Concurrent method here. Interested students can turn over the previous articles.

The command line to execute the use case: java -jar performance.jar test 10 login 10 1000

Explain that the test script requests 10 threads, the login script requests 10 threads, and each thread executes 1000 requests. The following is the content of 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}

Programme II:

This is relatively simple, first configure the groovy environment on the server, and then put the package jar of the interface function test and automation test project into groovy's lib directory. Jenkins automation builds are used here, adding a line of mv or cp file shell to the post-script.

Then create a new directory on the server to store the groovy script. Place a test script 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, you can edit scripts flexibly through vim, and execute different use cases, including running logs. There is no problem with anything.

Method of implementation: groovy test

Interested children's shoes are welcome to communicate with each other.

Posted by s_ainley87 on Wed, 24 Jul 2019 00:52:21 -0700