Operations 7 min read

Configurable Load Testing on a Linux Server Using Java and Groovy

The article explains two approaches for flexible performance testing on a head‑only Linux server—one using configuration files to assemble requests and another using Groovy scripts—detailing code implementations, command‑line execution, and deployment steps.

FunTester
FunTester
FunTester
Configurable Load Testing on a Linux Server Using Java and Groovy

While conducting performance testing on a Linux server that only provides a command‑line interface, the author needed a more flexible way to modify parameters and add logs without repeatedly repackaging and redeploying the test suite.

Two solutions were created: the first builds each request from a configuration file, allowing concurrent execution of multiple requests and easy adjustment of parameters; the second uses Groovy scripts, requiring a Groovy environment on the server and placement of the project's JAR in Groovy's lib directory.

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;

/**
 * Reads interface‑related parameters from a file to send requests, achieving configurable request generation.
 */
public class RequestFile extends SourceCode {
    private static Logger logger = LoggerFactory.getLogger(RequestFile.class);
    String url;
    /** get for GET, post for form POST, others for JSON POST */
    JSONObject headers;
    String requestType;
    String name;
    JSONObject info;
    JSONObject params;
    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"));
    }
    public RequestFile(int i) {
        this(i + Constant.EMPTY);
    }
    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"));
    }
    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;
    }
}

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 command to run a test case is: java -jar performance.jar test 10 login 10 1000 This command launches 10 threads for the test script, 10 threads for the login script, and each thread performs 1000 requests. An example configuration file ( test.log) contains URL, request type, parameters, and headers.

The second approach simply sets up a Groovy environment on the server, copies the test JAR into Groovy's lib directory, and creates a Groovy script to invoke the same logic. Example Groovy script:

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

The Groovy script can be edited directly on the server with vim and executed via: groovy test Both methods allow flexible editing of test cases, parameter adjustments, and log collection without the need for full rebuilds, making performance testing more efficient on head‑only Linux machines.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaperformancelinuxLoad TestingGroovy
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.