负载测试 Java Web 应用程序 - 查找 TPS/平均事务响应时间

发布于 2024-08-29 05:05:27 字数 1424 浏览 4 评论 0原文

我想用 Java 构建自己的负载测试工具,目标是能够在整个开发周期中对我正在构建的 Web 应用程序进行负载测试。 Web 应用程序将接收服务器到服务器的 HTTP Post 请求,我想找到它的每秒启动事务 (TPS) 容量以及平均响应时间。

Post 请求和响应消息将采用 XML 格式(但我认为这并不真正适用:))。

我编写了一个非常简单的 Java 应用程序来发送事务并计算它在一秒(1000 毫秒)内能够发送多少事务,但我认为这不是负载测试的最佳方法。我真正想要的是在同一时间发送任意数量的交易 - 即 10、50、100 等。

任何帮助将不胜感激!

哦,这是我当前的测试应用程序代码:

    Thread[] t = new Thread[1];

    for (int a = 0; a < t.length; a++) {
        t[a] = new Thread(new MessageLoop());
    }
    startTime = System.currentTimeMillis();
    System.out.println(startTime);
    for (int a = 0; a < t.length; a++) {
        t[a].start();
    }
    while ((System.currentTimeMillis() - startTime) < 1000 ) {

    }

    if ((System.currentTimeMillis() - startTime) > 1000 ) {
        for (int a = 0; a < t.length; a++) {
            t[a].interrupt();
        }
    }
    long endTime = System.currentTimeMillis();
    System.out.println(endTime);
    System.out.println("Total time: " + (endTime - startTime));
    System.out.println("Total transactions: " + count);

private static class MessageLoop implements Runnable {
    public void run() {
        try {
            //Test Number of transactions

            while ((System.currentTimeMillis() - startTime) < 1000 ) {
                // SEND TRANSACTION HERE
                count++;
            }
        }
        catch (Exception e) {

        }
    }
}

I would like to build my own load testing tool in Java with the goal of being able to load test a web application I am building throughout the development cycle. The web application will be receiving server to server HTTP Post requests and I would like to find its starting transaction per second (TPS) capacity along with the avgerage response time.

The Post request and response messages will be in XML (I dont' think that's really applicable though :) ).

I have written a very simple Java app to send transactions and count how many transactions it was able to send in one second (1000 ms) however I don't think this is the best way to load test. Really what I want is to send any number of transactions at exactly the same time - i.e. 10, 50, 100 etc.

Any help would be appreciated!

Oh and here is my current test app code:

    Thread[] t = new Thread[1];

    for (int a = 0; a < t.length; a++) {
        t[a] = new Thread(new MessageLoop());
    }
    startTime = System.currentTimeMillis();
    System.out.println(startTime);
    for (int a = 0; a < t.length; a++) {
        t[a].start();
    }
    while ((System.currentTimeMillis() - startTime) < 1000 ) {

    }

    if ((System.currentTimeMillis() - startTime) > 1000 ) {
        for (int a = 0; a < t.length; a++) {
            t[a].interrupt();
        }
    }
    long endTime = System.currentTimeMillis();
    System.out.println(endTime);
    System.out.println("Total time: " + (endTime - startTime));
    System.out.println("Total transactions: " + count);

private static class MessageLoop implements Runnable {
    public void run() {
        try {
            //Test Number of transactions

            while ((System.currentTimeMillis() - startTime) < 1000 ) {
                // SEND TRANSACTION HERE
                count++;
            }
        }
        catch (Exception e) {

        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暗恋未遂 2024-09-05 05:05:27

您的代码存在一些问题。使用 System.currentTimeMillis() 是一个糟糕的选择,因为它使用的系统计时器的精度为 1 毫秒,但精度约为 10-20 毫秒。您应该使用 System.nanoTime() 进行此测量。并且负载测试工具应该能够在多线程环境中测试代码。

因此,您应该使用开源工具 :)

There is some problems in your code. It's bad choice to use System.currentTimeMillis() as it use system timer which have precision of 1 millisecond, but accuracy about 10-20 milliseconds. You should use System.nanoTime() for this measurements. And also load testing tool should be able to test code in multithreaded environment.

So, You should use open source tools :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文