Customizing Request Marking for Java API Performance Testing
This article explains how to modify a Java search API and write custom performance‑testing scripts that embed unique request marks, enabling precise measurement of each request's response time when header‑based marking is unavailable.
In earlier posts the author described marking each request's response time using a header field in the HTTPrequestbase object, but in many real‑world scenarios the header cannot be used because multiple HTTP requests are generated or the service does not expose a distinguishing header.
To solve this, the article presents a searchable API that accepts a keyword parameter. The original method is shown below:
/**
* 首页智课搜索
*
* @param keyword
* @return
*/
public JSONObject ailearnSearch(String keyword) {
String url = LaunchApi.AILEARN_SEARCH;
JSONObject params = getParams();
params.put("keyword", keyword);
params.put("orgIdArray", Arrays.asList(80, 54, 640, 3018, 3346, 3308, 3305));
params.put("page", 1);
params.put("pageSize", 10);
params.put("stageId", 2); //1:小学,2:初中,3:高中
JSONObject response = getPostResponse(url, params);
output(response);
return response;
}The original getParams() method simply returns a JSON object containing a header‑derived mark. To make the mark controllable during performance tests, the method is rewritten to accept a mark argument and a new API method ailearnSearchP is added:
/**
* 首页智课搜索(用于压测)
*
* @param mark
* @return
*/
public JSONObject ailearnSearchP(String mark) {
String url = LaunchApi.AILEARN_SEARCH;
JSONObject params = new JSONObject();
params.put("mark", mark);
params.put("keyword", rstring.get(t.getAndIncrement() % rstring.size()));
params.put("orgIdArray", Arrays.asList(80, 54, 640, 3018, 3346, 3308, 3305));
params.put("page", 1);
params.put("pageSize", 10);
params.put("stageId", 2); //1:小学,2:初中,3:高中
JSONObject response = getPostResponse(url, params);
output(response);
return response;
}For the performance test itself, a custom multithreaded script is built using an internal static class that overrides the doing() method. The script creates a thread‑safe AtomicInteger counter to generate a large random integer and appends a four‑character random string as the unique mark for each request.
package com.okayqa.elasticsearch.performance;
import com.fun.base.constaint.ThreadLimitTimesCount;
import com.fun.frame.execute.Concurrent;
import com.fun.frame.httpclient.ClientManage;
import com.fun.utils.ArgsUtil;
import com.fun.utils.RString;
import com.okayqa.elasticsearch.base.OkayBase;
import com.okayqa.elasticsearch.function.Launch;
import java.util.concurrent.atomic.AtomicInteger;
public class LaunchSearch extends OkayBase {
static AtomicInteger count = new AtomicInteger(getRandomIntRange(100, 999) * 1000000);
public static void main(String[] args) {
ClientManage.init(5, 3, -1, EMPTY, 0);
ArgsUtil util = new ArgsUtil(args);
int thread = util.getIntOrdefault(0, 20);
int times = util.getIntOrdefault(1, 30);
Launch launch = new Launch();
List<FUN> threads = new ArrayList<>();
for (int i = 0; i < thread; i++) {
threads.add(new FUN(times));
}
new Concurrent(threads, "首页智课搜索").start();
allOver();
}
static class FUN extends ThreadLimitTimesCount {
Launch drive = new Launch();
FUN(int times) { super(null, times, null); }
@Override
protected void doing() throws Exception {
this.threadmark = count.getAndIncrement() + RString.getString(4);
drive.ailearnSearchP(threadmark);
}
}
}The use of AtomicInteger guarantees thread‑safety while generating distinct marks, preventing duplicate requests during high‑concurrency load testing.
Finally, the author notes that the article is part of the FunTester series, inviting readers to follow for more testing insights.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
