Concurrent Testing Strategy for Updating Limited-Value Fields in Java APIs
The article presents a practical approach for load‑testing API endpoints that modify a database field with a small set of possible values, using per‑thread counters and modulo arithmetic to avoid duplicate parameters, and includes a complete Java demo implementation.
When benchmarking services that update a single database field, performance is higher when the new value differs from the previous one; repeated updates with the same value can skew results.
During load testing, generating a unique value for each request can be challenging when the field has a limited range, such as a gender attribute represented by the numbers 0‑3 (unknown, male, female, secret). Random selection leads to a high probability of duplication.
The proposed solution binds each thread to an int object that serves as the current value. After each request, the value is incremented and taken modulo 4, ensuring a cyclic but non‑repeating sequence across threads. The pseudo‑code is:
int i;
// loop start
doRequest(i);
i++;
i=i%4;
// loop end
// single thread execution completedA full Java demonstration follows, showing how to create 100 threads, each performing 1000 requests while rotating the gender value using the described technique.
package com.fun;
import com.fun.base.constaint.ThreadBase;
import com.fun.frame.SourceCode;
import com.fun.frame.excute.Concurrent;
import com.fun.frame.httpclient.FanLibrary;
import java.util.ArrayList;
import java.util.List;
public class G extends SourceCode {
public static void main(String[] args) {
int threadNum = 100;
int times = 1000;
List<ThreadBase> threadTask = new ArrayList<>();
for (int i = 0; i < threadNum; i++) {
ThreadBase<Integer> threadBase = new ThreadBase<Integer>(i, times) {
User user;
@Override
protected void before() {
user = new User(t);
}
@Override
protected void doing() throws Exception {
user.doRequest(t++);
// user.check(); // validation
t = t % 4;
}
@Override
protected void after() {
}
};
threadTask.add(threadBase);
}
Concurrent concurrent = new Concurrent(threadTask);
concurrent.start();
FanLibrary.testOver();
}
}
class User {
public User(int i) {
}
public void doRequest(int i) {
System.out.println("完成!" + i);
}
public boolean check() {
return true;
}
}After completing the script, the thread count and request count can be parameterized to make the tool more flexible for different testing scenarios.
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.
