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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật