我该如何模拟发送数千个GET和PUT请求到我的基于REST的Web服务器?是否有可用的工具,如果有,都有哪些工具?
我该如何模拟发送数千个GET和PUT请求到我的基于REST的Web服务器?是否有可用的工具,如果有,都有哪些工具?
ab - Apache HTTP服务器基准测试工具 http://httpd.apache.org/docs/2.0/programs/ab.html
这是一个用于测试REST API的好工具。
示例:
ab -c 100 -n 100 http://service/path/to/resource
在这个例子中:
var interval;
var queue = [];
var globalElapsedTime;
function getUrl() {
return $.trim($("#txtAjayUrl").val());
}
$("#btnLaunchRequests").on("click", function () {
queue = [];
$("#divTimeElapsed").html("<i>calculating</i>");
globalElapsedTime = window.performance.now();
$("#divRequestStatus").show();
$("#btnLaunchRequests").prop("disabled", true);
var totalRequests = 50;
var inputValue = $("#txtNumberOfConcurrentRequests").val();
totalRequests = inputValue.trim();
$("#divTotalNumberOfProcessed").html(totalRequests.toString());
if (interval != null && interval != undefined) {
clearInterval(interval);
}
interval = window.setInterval(function () {
$("#divNumberOfCurrentRequests").text(queue.length);
}, 500);
for (let i = 1; i <= totalRequests; i++) {
console.log("Loop No. " + i);
if (i == totalRequests) {
$.get(getUrl(), function (data) {
queue.push("1");
$("#btnLaunchRequests").prop("disabled", false);
$("#divNumberOfCurrentRequests").text(queue.length);
//clearInterval(interval);
}).always(function () {
$("#btnLaunchRequests").prop("disabled", false);
globalElapsedTime = window.performance.now() - globalElapsedTime;
globalElapsedTime = Math.round((globalElapsedTime / 1000) * 100) / 100;
console.log("%cLast Result Processed in " + globalElapsedTime + " Seconds.", "color:green");
$("#divTimeElapsed").text(globalElapsedTime + " seconds");
});
}
else {
$.get(getUrl(), function (data) {
queue.push("1");
});
}
}
});
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<div class="container">
<div class="row" style="padding-top:50px;">
<div class="col-sm-12">
<h3>HTTP GET Concurrent Requests Tester</h3>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<ul>
<li>Sample Ajax URL for concurrent request testing: <input type="text" id="txtAjayUrl" value="http://localhost:4500/Api/Home/GetCustomerDetails/36603/Test" style="width:100%"> <br><br>
</li>
<li><input type="number" value="50" id="txtNumberOfConcurrentRequests"> <input type="button" value="Launch Concurrent Requests" id="btnLaunchRequests"> <br></li>
</ul>
</div>
</div>
<div class="row" id="divRequestStatus" style="">
<div class="col-sm-12">
<table class="table table-bordered">
<thead>
<tr>
<th>
Total Number of Requests
</th>
<th>
Total Number of Requests Processed
</th>
<th>
Total Time Elapsed
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<div id="divTotalNumberOfProcessed"></div>
</td>
<td>
<div id="divNumberOfCurrentRequests" style="font-weight: bolder;"></div>
</td>
<td>
<div id="divTimeElapsed"></div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
尝试使用JMeter,有一个第三方REST插件:http://smartrics.blogspot.co.uk/2009/04/jmeter-to-test-robustness-of-system.html
几乎任何商业或开源的HTTP性能测试工具都可以用来对你的REST接口进行性能测试。
sudo apt-get install apache2-utils
。 - gavenkoa