- Apache Bench 教程
- Apache Bench - 主页
- Apache Bench - 概述
- Apache Bench - 环境设置
- 测试我们的示例应用程序
- 并发测试多个网址
- 动态页面测试准备
- 动态页面的顺序测试用例
- 输出比较
- Apache Bench 有用资源
- Apache Bench - 快速指南
- Apache Bench - 有用资源
- Apache Bench - 讨论
并发测试多个网址
在本章中,我们将了解如何同时测试多个 URL。为此,我们需要编辑我们的应用程序文件 app.py 以包含两个 URL -
from bottle import Bottle, run app = Bottle() @app.route('/') @app.route('/hello1') def hello(): return "Hello World! It is first URL." @app.route('/hello2') def hello(): return "Hello World! It is second URL." run(app,server = 'gunicorn',host = '127.0.0.1', port = 8080)
创建一个简单的 shell 脚本
你可以通过创建带有多个 ab 调用的 shell 脚本来执行此操作。创建一个文件 test.sh 并向其中添加以下行 -
ab -n 100 -c 10 http://127.0.0.1:8080/hello1 ab -n 100 -c 10 http://127.0.0.1:8080/hello2
添加上述行后,保存并关闭该文件。使文件可执行 -
chmod u+x test.sh
现在让我们运行脚本 -
./test.sh
为了避免重复和澄清目的,我们将只显示 ab 输出中的相关部分,并用点来表示已省略的部分,如下所示。
输出
. . . Document Path: /hello1 Document Length: 732 bytes Concurrency Level: 10 Time taken for tests: 0.040 seconds Complete requests: 100 Failed requests: 0 Non-2xx responses: 100 Total transferred: 90000 bytes HTML transferred: 73200 bytes Requests per second: 2496.13 [#/sec] (mean) Time per request: 4.006 [ms] (mean) Time per request: 0.401 [ms] (mean, across all concurrent requests) Transfer rate: 2193.87 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 0.8 0 3 Processing: 1 3 1.0 4 5 Waiting: 0 3 1.2 4 4 Total: 1 4 0.6 4 5 WARNING: The median and mean for the processing time are not within a normal deviation These results are probably not that reliable. . . .
将 Apache Bench 输出保存到文件的 Shell 脚本
你可以通过创建带有多个 ab 调用的 shell 脚本将 Apache Bench 输出保存到文件。在每一行的末尾,放置一个 &;;这会使命令在后台运行,并让下一条命令开始执行。你还会希望将每个 URL 的输出重定向到一个文件,使用 <filename>。例如,我们的文件 test.sh 在修改后将如下所示 -
$ ab -n 100 -c 10 http://127.0.0.1:8080/hello1 > test1.txt & $ ab -n 100 -c 10 http://127.0.0.1:8080/hello2 > test2.txt &
此处,test1.txt 和 test2.txt 是保存输出数据的文件。
你可以检查上面的脚本是否创建了两个文件 test1.txt 和 test2.txt,其中包含各个 URL 的 ab 输出 -
$ ls -l
输出
... -rw-r--r-- 1 root root 5225 May 30 12:11 out.data -rwxr--r-- 1 root root 118 Jun 10 12:24 test.sh -rw-r--r-- 1 root root 1291 Jun 10 12:31 test1.txt -rwxr--r-- 1 root root 91 Jun 10 13:22 test2.sh -rw-r--r-- 1 root root 1291 Jun 10 12:31 test2.txt ...
注意情况
在使用 ab 时,你应该注意未经警告就进行的失败测试。例如,如果你检查一个错误的 URL,你可能会得到类似以下内容(我们在这里故意更改了端口)。
$ ab -l -r -n 100 -c 10 -k -H "Accept-Encoding: gzip, deflate" http://127.0.0.1:805/
输出
This is ApacheBench, Version 2.3 <$Revision: 1604373 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, https://apache.org/ Benchmarking 127.0.0.1 (be patient).....done Server Software: Server Hostname: 127.0.0.1 Server Port: 805 Document Path: / Document Length: Variable Concurrency Level: 10 Time taken for tests: 0.002 seconds Complete requests: 100 Failed requests: 150 (Connect: 0, Receive: 100, Length: 0, Exceptions: 50) Keep-Alive requests: 0 Total transferred: 0 bytes HTML transferred: 0 bytes Requests per second: 44984.26 [#/sec] (mean) Time per request: 0.222 [ms] (mean) Time per request: 0.022 [ms] (mean, across all concurrent requests) Transfer rate: 0.00 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 0.0 0 0 Processing: 0 0 0.2 0 0 Waiting: 0 0 0.0 0 0 Total: 0 0 0.2 0 0 Percentage of the requests served within a certain time (ms) 50% 0 66% 0 75% 0 80% 0 90% 0 95% 0 98% 0 99% 0 100% 0 (longest request)
广告