18

如何模拟对基于 REST 的 Web 服务器的数千个 GET 和 PUT 请求?是否有可用的工具?如果有,有哪些可用的工具?

4

4 回答 4

34

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

在这个例子中:

  • “-c 100”表示 100 个并发请求,并且
  • “-n 100”表示 100 个请求
于 2012-06-08T15:50:10.310 回答
4

我知道这是一个老问题,但是我需要一个简单的基于 AJAX 的脚本来测试多个并发连接。如果你们中的任何人有类似的要求,那么您也可以像这样使用这个来进行测试。

模拟多个 Ajax 请求的 JS 代码快照

请查看这个 js fiddle 链接或查看附件(无论哪个适合您,因为两者都指向相同的 javascript 代码)

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>

于 2019-10-25T08:05:40.570 回答
2

试试jmeter,有第三方REST插件:http ://smartrics.blogspot.co.uk/2009/04/jmeter-to-test-robustness-of-system.html

于 2012-06-08T17:33:13.350 回答
-2

几乎任何 HTTP 性能测试工具,无论是商业的还是开源的,都可以用来对 REST 接口进行性能测试。

于 2012-06-10T04:14:51.743 回答