Apache ab 压力测试

apache 的 ab 真的是非常容易用,直接的说,就是下面这条命令就够了

C:Program FilesApache Software FoundationApache2.2bin>ab -n 100 -c 10 http://localhost/index2.html

这里我 index 的 html 文件就是 apache 的 it works 文件,在我的机子上,测试结果的吞吐率是 2000 多,应该算是比较低的一个数字了,对于这么简单的文本来说,正式的服务器,应该有 5000 左右,不过我的平台是 Windows 嘛。。

C:Program FilesApache Software FoundationApache2.2bin>ab -n 100 -c 10 http:/
/localhost/index2.html
This 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 localhost (be patient).....done

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

Document Path:          /index2.html
Document Length:        44 bytes

Concurrency Level:      10
Time taken for tests:   0.049 seconds
Complete requests:      100
Failed requests:        0
Write errors:           0
Total transferred:      32000 bytes
HTML transferred:       4400 bytes
Requests per second:    2040.69 [#/sec] (mean)
Time per request:       4.900 [ms] (mean)
Time per request:       0.490 [ms] (mean, across all concurrent requests)
Transfer rate:          637.72 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.4      0       1
Processing:     2    4   2.5      3      20
Waiting:        1    4   2.5      3      19
Total:          2    5   2.5      4      20

Percentage of the requests served within a certain time (ms)
  50%      4
  66%      4
  75%      5
  80%      6
  90%      8
  95%      8
  98%      9
  99%     20
 100%     20 (longest request)

C:Program FilesApache Software FoundationApache2.2bin>

对于一个打印 phpinfo 的 php 页面来说,成绩就更加惨不忍睹了,只有 90 多

C:Program FilesApache Software FoundationApache2.2bin>ab -n 100 -c 10 http:/
/localhost/index2.php
This 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 localhost (be patient).....done

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

Document Path:          /index2.php
Document Length:        67839 bytes

Concurrency Level:      10
Time taken for tests:   1.015 seconds
Complete requests:      100
Failed requests:        45
   (Connect: 0, Receive: 0, Length: 45, Exceptions: 0)
Write errors:           0
Total transferred:      6801290 bytes
HTML transferred:       6783990 bytes
Requests per second:    98.57 [#/sec] (mean)
Time per request:       101.454 [ms] (mean)
Time per request:       10.145 [ms] (mean, across all concurrent requests)
Transfer rate:          6546.67 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   1.2      0       8
Processing:     3   50 153.5     15    1012
Waiting:        3   49 153.6     13    1011
Total:          3   51 153.4     15    1012

Percentage of the requests served within a certain time (ms)
  50%     15
  66%     18
  75%     21
  80%     25
  90%     88
  95%    348
  98%   1012
  99%   1012
 100%   1012 (longest request)

C:Program FilesApache Software FoundationApache2.2bin>

那么,如果我们把 it work 用 php echo 出来呢,就是说,送进 php 解释器里面转一圈,来看看,依然只有 90 多

C:Program FilesApache Software FoundationApache2.2bin>ab -n 100 -c 10 http:/
/localhost/index3.php
This 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 localhost (be patient).....done

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

Document Path:          /index3.php
Document Length:        8 bytes

Concurrency Level:      10
Time taken for tests:   1.049 seconds
Complete requests:      100
Failed requests:        0
Write errors:           0
Total transferred:      18100 bytes
HTML transferred:       800 bytes
Requests per second:    95.32 [#/sec] (mean)
Time per request:       104.906 [ms] (mean)
Time per request:       10.491 [ms] (mean, across all concurrent requests)
Transfer rate:          16.85 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.5      0       1
Processing:     2   18  99.6      8    1004
Waiting:        2   18  99.7      8    1004
Total:          2   19  99.6      9    1004

Percentage of the requests served within a certain time (ms)
  50%      9
  66%      9
  75%     10
  80%     11
  90%     13
  95%     14
  98%     31
  99%   1004
 100%   1004 (longest request)

C:Program FilesApache Software FoundationApache2.2bin>

再来看看 php 访问数据库的时候的成绩,访问数据我们就用一条 select 语句,php 代码如下

<?php
mysql_connect("localhost", "root", "root") or die(mysql_error());
mysql_select_db("mailbox");
mysql_query("set names gbk");
$result = mysql_query("select * from mail where id = 118593");
while($row = mysql_fetch_array($result)) {
	var_dump($row);
}

算是非常中规中矩的读取了,结果如下,吞吐率只有 80 多

C:Program FilesApache Software FoundationApache2.2bin>ab -n 100 -c 10 http:/
/localhost/index4.php
This 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 localhost (be patient).....done

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

Document Path:          /index4.php
Document Length:        6108 bytes

Concurrency Level:      10
Time taken for tests:   1.157 seconds
Complete requests:      100
Failed requests:        0
Write errors:           0
Total transferred:      628100 bytes
HTML transferred:       610800 bytes
Requests per second:    86.43 [#/sec] (mean)
Time per request:       115.707 [ms] (mean)
Time per request:       11.571 [ms] (mean, across all concurrent requests)
Transfer rate:          530.12 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.4      0       1
Processing:     6  100 301.8     10    1154
Waiting:        6   99 301.8     10    1154
Total:          6  100 301.7     11    1154

Percentage of the requests served within a certain time (ms)
  50%     11
  66%     16
  75%     16
  80%     16
  90%     22
  95%   1145
  98%   1153
  99%   1154
 100%   1154 (longest request)

C:Program FilesApache Software FoundationApache2.2bin>

那么,如果我们用 apache 把 php 的结果缓存起来呢?这个想法非常赞,可惜我这里配的是 apache fastcgi php 的模式,简单 google 了一下并没有发现相关的文章,倒是 nginx fastcgi php 的缓存有很多的讨论,也是,人家本来就是做反向代理的嘛,不过,根据高性能 web 那本书的记载,应该是可以有 5 倍左右的提升的,还是很垂涎的,呵呵。强烈推荐这本高性能 web 的书,http://book.douban.com/subject…

最后插播一下,对于一台 debian 的虚拟机,成绩其实没有想象中的飞跃,这台虚拟机开的内存是 256,一样请求一个静态文件,结果如下,顺便一提,虚拟机里面跑着的是 nginx,不是 apache

C:Program FilesApache Software FoundationApache2.2bin>ab -n 1000 -c 10 http:
//192.168.128.133:8080/index.html
This 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.128.133 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests

Server Software:        Apache/2.2.20
Server Hostname:        192.168.128.133
Server Port:            8080

Document Path:          /index.html
Document Length:        12 bytes

Concurrency Level:      10
Time taken for tests:   0.632 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      297000 bytes
HTML transferred:       12000 bytes
Requests per second:    1582.19 [#/sec] (mean)
Time per request:       6.320 [ms] (mean)
Time per request:       0.632 [ms] (mean, across all concurrent requests)
Transfer rate:          458.90 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   0.5      1       2
Processing:     1    6   1.1      6      19
Waiting:        0    4   1.5      4      17
Total:          2    6   1.1      6      20

Percentage of the requests served within a certain time (ms)
  50%      6
  66%      6
  75%      6
  80%      7
  90%      7
  95%      8
  98%      9
  99%     10
 100%     20 (longest request)

C:Program FilesApache Software FoundationApache2.2bin>

Leave a Reply

Your email address will not be published. Required fields are marked *