How to Tag Every API Request with RequestID for Precise Performance Testing

This article explains how to implement request‑ID tagging for each API call in a Java performance‑testing framework, describes the updated MarkRequest implementations, shows how to configure connection‑pool timeouts and retries, and provides full source code examples for both header‑based and string‑based markers.

FunTester
FunTester
FunTester
How to Tag Every API Request with RequestID for Precise Performance Testing

In a previous post the author introduced tagging each request with a requestID to simplify performance testing; the recorded timeout IDs and response times were logged in the format "responseTime_requestID".

This article updates that approach by replacing the inner‑class implementation of MarkRequest (which caused serialization errors in Groovy) with two concrete classes, one that adds a numeric header and another that generates a string value.

Main script

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

HeaderMarkInt implementation

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

HeaderMarkStr implementation

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

Connection‑pool reinitialisation

/**
 * Re‑initialise the HTTP client pool to temporarily change timeout thresholds.
 *
 * @param timeout   connection timeout in milliseconds
 * @param accepttime maximum time to wait for a connection to be accepted
 * @param retrytime number of retry attempts (0 disables retry)
 */
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();
}

Readers interested in the full project can reply with “git” to obtain the repository address; the article was originally published on the FunTester public account.

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.

JavaPerformance TestingConnection PoolHTTPCode ExampleRequest ID
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.