负载测试 Java Web 应用程序 - 查找 TPS/平均事务响应时间
我想用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码存在一些问题。使用 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 useSystem.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 :)