当你搭建了apache服务器并在上面部署了web网站,在网站运行前,为了使apache服务器的性能得到更好的应用,我们可以先对其进行压力测试。进行压力测试其实非常简单,我们也不用再额外下载安装什么测试工具,因为Apache HTTP Server自带了一个压力测试工具——ab,ab的可选参数比较多,但一般我们测试只需要使用其中的两个即可
-n 即requests,用于指定压力测试总共的执行次数。
-c 即concurrency,用于指定压力测试的并发数。
在命令行执行命令:
[root@RS1 ~]# ab -n 4000 -c 500 http://192.168.0.162/index.htmlThis is ApacheBench, Version 2.3 <$Revision: 655654 $>Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/Licensed to The Apache Software Foundation, http://www.apache.org/Benchmarking 192.168.0.162 (be patient)Completed 400 requestsCompleted 800 requestsCompleted 1200 requestsCompleted 1600 requestsCompleted 2000 requestsCompleted 2400 requestsCompleted 2800 requestsCompleted 3200 requestsCompleted 3600 requestsCompleted 4000 requestsFinished 4000 requestsServer Software: Apache (服务器软件名称及版本信息)Server Hostname: 192.168.0.162 (服务器主机名)Server Port: 80 (服务器端口)Document Path: /index.html (供测试的URL相对路径)Document Length: 117 bytes (供测试的URL返回的文档大小)Concurrency Level: 500 (测试并发数)Time taken for tests: 1.469 seconds (压力测试消耗的总时间)Complete requests: 4000 (完成请求的总次数)Failed requests: 0 (失败的请求数)Write errors: 0 (网络连接写入错误数)Total transferred: 1477476 bytes (传输的总数据量)HTML transferred: 468468 bytes (HTML文档的总数据量)Requests per second: 2722.46 [#/sec] (mean) (平均每秒的请求数)吞吐速率Time per request: 183.657 [ms] (mean) (所有并发用户(这里是500)都请求一次的平均时间)用户平均请求等待时间Time per request: 0.367 [ms] (mean, across all concurrent requests) (单个用户请求一次的平均时间)服务器平均请求处理的时间Transfer rate: 982.02 [Kbytes/sec] received (平均每秒网络上的流量,可以帮助排除是否有存在网络流量过大导致响应时间延长的问题)Connection Times (ms) min mean[+/-sd] median maxConnect: 0 27 152.9 2 1008Processing: 1 79 266.9 26 1446Waiting: 0 77 266.9 24 1445Total: 16 106 305.2 28 1466Percentage of the requests served within a certain time (ms) 50% 28 66% 29 75% 29 80% 30 90% 31 95% 1026 98% 1450 99% 1455 100% 1466 (longest request)
以上结果可以看出,服务器压力很小,下面测试一个压力大的
Server Software: ApacheServer Hostname: 192.168.0.162Server Port: 80Document Path: /index.htmlDocument Length: 117 bytesConcurrency Level: 4000Time taken for tests: 3.257 secondsComplete requests: 10000Failed requests: 0Write errors: 0Total transferred: 3749409 bytesHTML transferred: 1188837 bytesRequests per second: 3070.65 [#/sec] (mean) Time per request: 1302.655 [ms] (mean) 可以看出用户平均等待时间明显增加Time per request: 0.326 [ms] (mean, across all concurrent requests)Transfer rate: 1124.33 [Kbytes/sec] received
在加大并发数,会发现:
[root@RS1 ~]# ab -n 100000 -c 10000 http://192.168.0.162/index.htmlThis is ApacheBench, Version 2.3 <$Revision: 655654 $>Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/Licensed to The Apache Software Foundation, http://www.apache.org/Benchmarking 192.168.0.162 (be patient)Completed 10000 requestsCompleted 20000 requestsCompleted 30000 requestsapr_socket_recv: Connection reset by peer (104)Total of 35491 requests completed (只完成了3W多个请求就断了)
注意:在上面的压力测试中,Apache使用的是默认配置,并没有经过任何优化措施处理。实际上,Apache在经过配置优化后,只要服务器硬件够用,Apache服务器是能够撑起更多的并发量的。