How to Model and Simulate API Traffic for Precise Load Testing

This article explains how to build traffic‑ratio models for GET APIs, scale them, and implement a random‑selection framework in Java/Groovy to quantitatively simulate real‑world request loads for both individual endpoints and whole services.

FunTester
FunTester
FunTester
How to Model and Simulate API Traffic for Precise Load Testing

This guide demonstrates how to apply segment‑based random traffic simulation to a service module whose APIs are primarily simple GET requests with single parameters.

Module Traffic Model

Log statistics are used to build a map that records the average QPS for each endpoint. The map is defined as:

public static final Map<String, Integer> urlsQps = new HashMap<String, Integer>() {{
    put("a", 9502);
    put("b", 6445);
    put("c", 1228);
    put("d", 973);
    put("e", 827);
    put("f", 546);
    put("g", 132);
    put("h", 113);
    put("i", 47);
    put("j", 1);
}};

All values are normalised by dividing each QPS by the smallest value (1 QPS) to obtain relative traffic tiers. Endpoints whose raw traffic is below a threshold (e.g., <5 QPS) are simulated at a fixed 20× baseline to provide a realistic background load while keeping the test volume manageable.

Parameter Distribution Model

For each API, the frequencies of its input parameters are also extracted from logs. Because the distribution is heavily skewed, only the most frequent values (typically the top 10‑20) are retained. The parameter list is stored as:

public static final String BS = "****";
public static final List<String> BS_ARGS = SourceCode.randomMem(new HashMap<String, Integer>() {{
    put("aaa", 3);
    put("bbb", 2);
    put("ccc", 2);
    put("ddd", 1);
    put("eee", 3);
    put("fff", 1);
}});

The integer values represent the relative weight of each argument; the random‑selection utility will sample according to these weights.

Interface‑Level Simulation

A helper method is written for each endpoint. It obtains a parameter value from MApi.BS_ARGS using the weighted random sampler, builds the request URL, attaches the parameter, sends the GET request, and returns the JSON response.

/**
 * Get information
 * @param instId randomly selected from BS_ARGS
 * @return JSON response
 */
JSONObject bs(String instId = random(MApi.BS_ARGS)) {
    String url = MApi.BS;
    def params = getParams();
    params.instId = instId;
    def response = getGetResponse(url, params);
    print(response);
    return response;
}

Service‑Level Simulation

To simulate traffic across the whole service, the framework randomly picks an endpoint identifier from MApi.urls and invokes the corresponding method:

/**
 * Quantitative simulation of all interfaces
 */
public void all() {
    def url = random(MApi.urls);
    if (url == MApi.a) { a(); }
    else if (url == MApi.b) { b(); }
    else if (url == MApi.c) { c(); }
    else if (url == MApi.d) { d(); }
    else if (url == MApi.e) { e(); }
    else if (url == MApi.f) { f(); }
    // add further branches as needed
}

The random utility is implemented with a thread‑local random generator (e.g., ThreadLocalRandom) to guarantee thread safety and high throughput. Benchmarks show it can sustain up to one million QPS without contention, making it suitable for multi‑threaded load‑testing scenarios.

Key Takeaways

Derive both endpoint‑level QPS ratios and parameter‑level weight distributions from production logs.

Scale low‑traffic endpoints to a fixed multiple (e.g., 20×) to keep background load realistic.

Retain only the most frequent parameter values to avoid exhaustive enumeration while preserving distribution fidelity.

Use a thread‑safe, high‑performance random sampler to select both endpoints and parameters according to their calculated ratios.

The resulting framework enables accurate, automated simulation of both low‑volume and high‑volume API traffic without extensive manual configuration.

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.

JavaBackend DevelopmentPerformance TestingLoad TestingAPI testingtraffic simulation
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.