Apache ab负载测试

Apache ab负载测试,apache,benchmarking,Apache,Benchmarking,有人能告诉我如何使用(ab)加载测试我的网站吗 我想知道以下几点: 网站每分钟能处理多少人? 请引导我通过我应该运行的命令来解决这个问题 我尝试了每一个教程,但它们都让人困惑。apache基准测试工具非常基础,虽然它会让您对某些性能有一个确切的了解,但如果您计划让您的站点在生产过程中承受严重的压力,只依赖它是一个坏主意。 话虽如此,以下是最常见和最简单的参数: -c:(“并发”)。指示将同时访问站点的客户端(人员/用户)数量。当ab运行时,将有-c客户端访问该站点。这实际上决定了您的站点在基准测

有人能告诉我如何使用(
ab
)加载测试我的网站吗

我想知道以下几点:

网站每分钟能处理多少人?

请引导我通过我应该运行的命令来解决这个问题


我尝试了每一个教程,但它们都让人困惑。

apache基准测试工具非常基础,虽然它会让您对某些性能有一个确切的了解,但如果您计划让您的站点在生产过程中承受严重的压力,只依赖它是一个坏主意。

话虽如此,以下是最常见和最简单的参数:

-c
:(“并发”)。指示将同时访问站点的客户端(人员/用户)数量。当
ab
运行时,将有
-c
客户端访问该站点。这实际上决定了您的站点在基准测试期间所承受的压力大小

-n
:表示将发出多少请求。这就决定了基准的长度。服务器可以支持的高
-n
值和
-c
值是一个好主意,可以确保事情不会在持续的压力下破裂:支持压力5秒与支持压力5小时不同

-k
:这就是“KeepAlive”功能浏览器的本质。您不需要为
-k
传递一个值,因为它是“布尔值”(意思是:它表示您希望您的测试使用HTTP中的Keep-Alive头并维持连接)。由于浏览器可以做到这一点,并且您可能希望模拟您的站点将从浏览器中获得的压力和流量,因此建议您使用此功能进行基准测试

最后一个参数就是主机。默认情况下,如果您没有指定它,它将点击http://protocol

ab -k -c 350 -n 20000 example.com/
通过发出上述命令,您将同时连接350个连接,直到满足20000个请求。它将使用keep-alive标头完成

在流程完成20000个请求后,您将收到关于统计数据的反馈。这将告诉您在使用上述参数时,站点在您施加的压力下表现如何

要了解站点同时可以处理多少人,只需查看响应时间(平均值、最小和最大响应时间、失败请求等)是否是站点可以接受的数字(不同的站点可能需要不同的速度)。您可以使用不同的-c值来运行该工具,直到您到达您说“如果我增加它,它开始得到失败的请求,并且它会中断”的位置

根据您的网站,您预计平均每分钟的请求数。这变化太大了,你不能用ab来模拟。但是,这样想:如果你的平均用户每分钟会发出5个请求,而你发现有效的平均响应时间是2秒,这意味着每分钟有10秒1个用户会收到请求,这意味着只有1/6的时间它会进入网站。这也意味着,如果您有6个用户同时使用ab访问站点,那么模拟中可能有36个用户,即使您的并发级别(-c)只有6个

<>这取决于你希望用户使用网站的行为,但是你可以从“我希望我的用户每分钟点击X请求,我认为平均响应时间有效,如果它是2秒”。然后只需修改-c级别,直到达到平均响应时间的2秒(但确保最大响应时间和stddev仍然有效),然后看看您可以将-c设置得多大

我希望我能解释清楚:)祝你好运

请引导我通过我应该运行的命令来解决这个问题

您可以做的最简单的测试是执行1000个请求,每次10个(这大约模拟了10个并发用户在测试期间每个用户获得100个页面)

-n 1000
是要发出的请求数

-c10
告诉AB一次执行10个请求,而不是一次执行1个请求,以更好地模拟并发访问者(与顺序访问者相比)

-k
发送
KeepAlive
头,它要求web服务器在每个请求完成后不要关闭连接,而是继续重用连接

我还发送了额外的头文件
Accept Encoding:gzip,deflate
,因为mod_deflate几乎总是用于压缩文本/html输出25%-75%——由于它会影响web服务器的整体性能,因此不应忽略其影响(即,可以在相同的时间内传输2倍的数据,等等)

结果:

Benchmarking www.example.com (be patient)
Completed 100 requests
...
Finished 1000 requests


Server Software:        Apache/2.4.10
Server Hostname:        www.example.com
Server Port:            80

Document Path:          /
Document Length:        428 bytes

Concurrency Level:      10
Time taken for tests:   1.420 seconds
Complete requests:      1000
Failed requests:        0
Keep-Alive requests:    995
Total transferred:      723778 bytes
HTML transferred:       428000 bytes
Requests per second:    704.23 [#/sec] (mean)
Time per request:       14.200 [ms] (mean)
Time per request:       1.420 [ms] (mean, across all concurrent requests)
Transfer rate:          497.76 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       1
Processing:     5   14   7.5     12      77
Waiting:        5   14   7.5     12      77
Total:          5   14   7.5     12      77

Percentage of the requests served within a certain time (ms)
  50%     12
  66%     14
  75%     15
  80%     16
  90%     24
  95%     29
  98%     36
  99%     41
 100%     77 (longest request)
对于最简单的解释,请忽略以下内容:

Requests per second:    704.23 [#/sec] (mean)
乘以60,你每分钟就有你的请求

为了获得真实的结果,您需要测试Wordpress,而不是一些静态HTML或index.php文件,因为您需要知道所有内容是如何一起执行的:包括复杂的php代码和多个MySQL查询

例如,这里是在同一系统和WAMP环境上测试新安装的Wordpress的结果(我使用的是WampDeveloper,但也有Xampp、WampServer和其他)

现在慢了37倍

在负载测试之后,您可以做很多事情来提高总体性能(每秒请求数),并使web服务器在更大负载下更稳定(例如,增加
-n
-c
会使Apache崩溃),您可以在这里阅读:


仅使用ab对API进行负载测试是不够的。然而,我认为这是一个很好的工具,可以让你对网站的性能有一个基本的了解

如果您想在中使用ab命令测试多个API端点,使用不同的数据,同时在后台进行测试,则需要使用“nohup”com
Requests per second:    704.23 [#/sec] (mean)
Requests per second:    18.68 [#/sec] (mean)
<?php

for($i=1;$i<50000;$i++){
    print ('qwertyuiopasdfghjklzxcvbnm1234567890');
}
?>
<?php
?>
"c:\xampp\apache\bin\abs.exe" -n 10000 http://localhost/bench/void.php
"c:\xampp\apache\bin\abs.exe" -n 10000 http://localhost/bench/bench.php
pause
c:\xampp\htdocs\bench>"c:\xampp\apache\bin\abs.exe" -n 10000 http://localhost/bench/void.php
This is ApacheBench, Version 2.3 <$Revision: 1826891 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests


Server Software:        Apache/2.4.33
Server Hostname:        localhost
Server Port:            80

Document Path:          /bench/void.php
Document Length:        0 bytes

Concurrency Level:      1
Time taken for tests:   11.219 seconds
Complete requests:      10000
Failed requests:        0
Total transferred:      2150000 bytes
HTML transferred:       0 bytes
Requests per second:    891.34 [#/sec] (mean)
Time per request:       1.122 [ms] (mean)
Time per request:       1.122 [ms] (mean, across all concurrent requests)
Transfer rate:          187.15 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.3      0       1
Processing:     0    1   0.9      1      17
Waiting:        0    1   0.9      1      17
Total:          0    1   0.9      1      17

Percentage of the requests served within a certain time (ms)
  50%      1
  66%      1
  75%      1
  80%      1
  90%      1
  95%      2
  98%      2
  99%      3
 100%     17 (longest request)

c:\xampp\htdocs\bench>"c:\xampp\apache\bin\abs.exe" -n 10000 http://localhost/bench/bench.php
This is ApacheBench, Version 2.3 <$Revision: 1826891 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests


Server Software:        Apache/2.4.33
Server Hostname:        localhost
Server Port:            80

Document Path:          /bench/bench.php
Document Length:        1799964 bytes

Concurrency Level:      1
Time taken for tests:   177.006 seconds
Complete requests:      10000
Failed requests:        0
Total transferred:      18001600000 bytes
HTML transferred:       17999640000 bytes
Requests per second:    56.50 [#/sec] (mean)
Time per request:       17.701 [ms] (mean)
Time per request:       17.701 [ms] (mean, across all concurrent requests)
Transfer rate:          99317.00 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.3      0       1
Processing:    12   17   3.2     17      90
Waiting:        0    1   1.1      1      26
Total:         13   18   3.2     18      90

Percentage of the requests served within a certain time (ms)
  50%     18
  66%     19
  75%     19
  80%     20
  90%     21
  95%     22
  98%     23
  99%     26
 100%     90 (longest request)

c:\xampp\htdocs\bench>pause
Press any key to continue . . .
ab -n 100 -c 10 -k -H "Accept-Encoding: gzip, deflate" http://localhost:port/
ab -k -c 25 -n 2000 http://192.168.1.113:3001/api/v1/filters/3