Kavi
Kavi

Reputation: 111

Jmeter- How to post N number of messages by X Number of users every T second with a user defined interval

Context:I have a token API and a post API. both API have custom headers. I was able to define them in jmeter using jsr223 sampler. I have for example 1000 txt files in my local desktop folder where each text file has a message in it.

Scenario:The Post API takes the bearer token from token API . Also it needs to read the messages within each txt file and post it. I named by files as 1a.txt, 2a.txt etc. Each thread needs to read unique file and post it. If there are duplicates the response would fail.

Task to do: How do I achieve the below task? I don't want to hardcode anything in the script like I did.I read constant throughput timer will do the job but if someone can give me high level guidance that would be great. So the number of messages, the number of threads, the wait time that each thread needs to post multiple messages and wait time between threads needs to be dynamic . Thank you

  1. Number of users : 1----> Every 1 sec 1 message will be posted
  2. Number of users : 2----> Every 1 sec 2 message will be posted
  3. Number of users : 5----> Every 1 sec 5 message will be posted

It also needs to do the below:

  1. Lets say total messages =5000 ,Number of message/sec: 20/sec, 20 users may be introduce wait time between each posting? lets say thread 1 when posting, post the message every 5 seconds etc?

  2. Total 5000 Messages ,Number of messages/sec: 30/sec, 30 users

How do I achieve it dynamically without hardcoding anything?

Code:

import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.FileEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import java.io.File;
import java.io.FilenameFilter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

// Start timing
long startTime = System.currentTimeMillis();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

try {
    CloseableHttpClient httpClient = HttpClients.createDefault();
    StringBuilder threadLog = new StringBuilder(); // StringBuilder for collecting logs

    try {
        String authToken = vars.get("authToken");

        if (authToken == null) {
            String tokenApiUrl =  “https:/token.com/token”;
            HttpPost tokenRequest = new HttpPost(tokenApiUrl);
            tokenRequest.setHeader("customheader", "value1");
            HttpResponse tokenResponse = httpClient.execute(tokenRequest);
            authToken = EntityUtils.toString(tokenResponse.getEntity()).trim();
            vars.put("authToken", authToken);
        }

        String apiEndpoint = “https://postpai.com/api”;
        String directoryPath = "/Users/username/Desktop/foldername";

        File dir = new File(directoryPath);
        File[] files = dir.listFiles((FilenameFilter) (d, name) -> name.endsWith(".txt"));
        List<File> fileList = new ArrayList<>();
        Collections.addAll(fileList, files);

        int threadNum = ctx.getThreadNum(); // Get the current thread number
        int totalThreads = ctx.getThreadGroup().getNumThreads(); // Get the total number of threads

        // Ensure the files are sorted in a specific order (assumes file names are sequential like 1a.txt, 2a.txt, etc.)
        fileList.sort();

        // Assign a specific file to each thread based on thread number
        File file = fileList.get(threadNum % fileList.size());

        // Log the file each thread is posting
        String logPrefix = String.format("%s INFO o.a.j.p.j.s.J.JSR223 Sampler: Thread %d", sdf.format(new Date()), threadNum);
        threadLog.append(String.format("%s will post file %s.%n", logPrefix, file.getName()));

        // Determine the number of messages to post per second based on the number of users (threads)
        int messagesPerSecond = totalThreads; // Each thread posts messages per second equal to the total number of users
        long interval = TimeUnit.SECONDS.toMillis(1) / messagesPerSecond; // Calculate the interval between messages

        long requestStartTime = System.currentTimeMillis(); // Capture start time before sending the request
        String formattedStartTime = sdf.format(new Date(requestStartTime));

        HttpPost postRequest = new HttpPost(apiEndpoint);
        postRequest.setEntity(new FileEntity(file, "text/plain"));
        postRequest.setHeader("Content-type", "text/plain");
        postRequest.setHeader("Authorization", "Bearer " + authToken);
        postRequest.setHeader("newcustomerheader", "XXX");


        int maxRetries = 3;
        boolean success = false;
        String responseString = "";
        for (int retry = 0; retry < maxRetries; retry++) {
            HttpResponse response = httpClient.execute(postRequest);
            int statusCode = response.getStatusLine().getStatusCode();

            if (statusCode >= 200 && statusCode < 300) {
                responseString = EntityUtils.toString(response.getEntity()).trim();
                success = true;
                break;
            } else {
                responseString = EntityUtils.toString(response.getEntity()).trim();
            }
        }

        long requestEndTime = System.currentTimeMillis(); // Capture end time after receiving the response
        long requestDuration = requestEndTime - requestStartTime; // Calculate request duration

        if (success) {
            threadLog.append(String.format("%s - Successfully processed %s: Response ID - %s. Request took %d milliseconds%n",
                    logPrefix, file.getName(), responseString, requestDuration));
        } else {
            threadLog.append(String.format("%s - Failed to process %s after %d retries. Response: %s%n",
                    logPrefix, file.getName(), maxRetries, responseString));
        }

        // Calculate the sleep duration to ensure the specified interval between messages
        long sleepDuration = interval - requestDuration;
        if (sleepDuration > 0) {
            Thread.sleep(sleepDuration);
        }
    } catch (Exception e) {
        threadLog.append(String.format("%s encountered an error: %s%n", sdf.format(new Date()), e.getMessage()));
    } finally {
        if (httpClient != null) {
            httpClient.close();
        }
    }

    // Synchronize log output
    synchronized (this) {
        log.info(threadLog.toString());
    }
} finally {
    long endTime = System.currentTimeMillis();
    long totalTime = endTime - startTime;
    log.info(String.format("Total execution time: %d milliseconds", totalTime));
}

Upvotes: 0

Views: 37

Answers (1)

Ivan G
Ivan G

Reputation: 2732

  1. Your code doesn't will be executed by each thread (virtual user) individually hence total concurrency will be unknown (and depend on the response time of the system under test)
  2. You won't be able to get metrics like connect time/ latency, etc. and JSR223 Sampler's response time will contain cumulative script execution time which will combine 2 endpoints so I would go for 2 HTTP Request samplers instead, custom headers can be added using HTTP Header Manager
  3. If you don't want to hardcode anything you can i.e. pass the values in the runtime using -J command-line argument or .properties files

Upvotes: 0

Related Questions