memc-nginx-module模块和php-fpm访问memcache性能比较

nginx服务器拥有支持高并发和低延迟的特性。它的第三方模块也受益于这个特点,我用一个nginx第三方模块HttpMemcModule和一般的php访问memcache的程序做个比较,看看两者之间的性能差距。

我的测试环境是典型的LNMP环境,自己在虚拟机上搭建的,php-cgi进程开了6个。

nginx的配置信息如下:

    location /bar {
        set $memc_cmd $arg_cmd;
        set $memc_key $arg_key;
        set $memc_value $arg_val;
        set $memc_flags $arg_flags; # defaults to 0
        set $memc_exptime $arg_exptime; # defaults to 0

        memc_cmds_allowed get set add delete flush_all;

        memc_pass 127.0.0.1:11211;
    }

对应的php代码文件如下:

connect('localhost',11211) or die("Cound not connect");

$cmd=$_GET["cmd"];
$key=$_GET["key"];
$val=$_GET["val"];

if($cmd=="get"){
   echo $mem->get($key);
}
else if($cmd=="set"){
   $mem->set($key,$val,0,0);
}
else if($cmd=="delete"){
   $mem->delete($key,0);
}

?>
可以看出只是针对memcache的几个基本操作

我们下面用ab测试工具做几组测试:(上nginx模块测试结果,下php代码测试结果)

A,100并发,10000请求,Set

[root@fedora nginx-1.1.12]# ab -c 100 -n 10000 http://127.0.0.1/bar?cmd=set&key=apple&val=fruits
[1] 27618
[2] 27619
[root@fedora nginx-1.1.12]# 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 127.0.0.1 (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:        nginx/1.1.12
Server Hostname:        127.0.0.1
Server Port:            80

Document Path:          /bar?cmd=set
Document Length:        193 bytes

Concurrency Level:      100
Time taken for tests:   2.268 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Non-2xx responses:      10000
Total transferred:      3550000 bytes
HTML transferred:       1930000 bytes
Requests per second:    4409.82 [#/sec] (mean)
Time per request:       22.677 [ms] (mean)
Time per request:       0.227 [ms] (mean, across all concurrent requests)
Transfer rate:          1528.79 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   2.5      1      27
Processing:     9   21   4.8     20      37
Waiting:        7   20   5.2     20      36
Total:         14   22   4.8     21      45

Percentage of the requests served within a certain time (ms)
  50%     21
  66%     22
  75%     25
  80%     27
  90%     30
  95%     32
  98%     34
  99%     35
 100%     45 (longest request)
访问很迅速,因为是本地测试的缘故,成功率也达到100%。

[root@fedora nginx-1.1.12]# ab -c 100 -n 10000 http://127.0.0.1/memc.php?cmd=set&key=pear&val=fruits
[1] 27789
[2] 27790
[root@fedora nginx-1.1.12]# 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 127.0.0.1 (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:        nginx/1.1.12
Server Hostname:        127.0.0.1
Server Port:            80

Document Path:          /memc.php?cmd=set
Document Length:        164 bytes

Concurrency Level:      100
Time taken for tests:   9.590 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Total transferred:      3370000 bytes
HTML transferred:       1640000 bytes
Requests per second:    1042.71 [#/sec] (mean)
Time per request:       95.904 [ms] (mean)
Time per request:       0.959 [ms] (mean, across all concurrent requests)
Transfer rate:          343.16 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   3.4      0      40
Processing:    14   95   8.7     92     127
Waiting:       13   95   8.7     92     127
Total:         50   95   8.1     92     131

Percentage of the requests served within a certain time (ms)
  50%     92
  66%     93
  75%     94
  80%     97
  90%    108
  95%    116
  98%    123
  99%    124
 100%    131 (longest request)
php的代码明显慢了许多,单个请求的处理时间大致为nginx模块的4到5倍

B,300并发,10000请求,Set

[root@fedora nginx-1.1.12]# ab -c 300 -n 10000 http://127.0.0.1/bar?cmd=set&key=bananp&val=fruits
[1] 28501
[2] 28502
[root@fedora nginx-1.1.12]# 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 127.0.0.1 (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:        nginx/1.1.12
Server Hostname:        127.0.0.1
Server Port:            80

Document Path:          /bar?cmd=set
Document Length:        193 bytes

Concurrency Level:      300
Time taken for tests:   2.767 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Non-2xx responses:      10000
Total transferred:      3550000 bytes
HTML transferred:       1930000 bytes
Requests per second:    3613.84 [#/sec] (mean)
Time per request:       83.014 [ms] (mean)
Time per request:       0.277 [ms] (mean, across all concurrent requests)
Transfer rate:          1252.84 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    2   9.5      0      73
Processing:    20   35   7.1     32      67
Waiting:       18   35   7.1     32      67
Total:         20   37  13.1     32     107

Percentage of the requests served within a certain time (ms)
  50%     32
  66%     39
  75%     41
  80%     43
  90%     46
  95%     52
  98%     96
  99%    101
 100%    107 (longest request)

300的并发现nginx模块的处理能力还是很强,正确率100%

[root@fedora nginx-1.1.12]# ab -c 300 -n 10000 http://127.0.0.1/memc.php?cmd=set&key=banane&val=fruits
[1] 28427
[2] 28428
[root@fedora nginx-1.1.12]# 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 127.0.0.1 (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:        nginx/1.1.12
Server Hostname:        127.0.0.1
Server Port:            80

Document Path:          /memc.php?cmd=set
Document Length:        164 bytes

Concurrency Level:      300
Time taken for tests:   12.669 seconds
Complete requests:      10000
Failed requests:        151
   (Connect: 0, Receive: 0, Length: 151, Exceptions: 0)
Write errors:           0
Non-2xx responses:      151
Total transferred:      3368188 bytes
HTML transferred:       1641359 bytes
Requests per second:    789.31 [#/sec] (mean)
Time per request:       380.076 [ms] (mean)
Time per request:       1.267 [ms] (mean, across all concurrent requests)
Transfer rate:          259.63 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0   92 559.1      0    9024
Processing:     1  158 317.2    131    6605
Waiting:        1  158 317.2    131    6605
Total:        104  250 737.2    131   10480

Percentage of the requests served within a certain time (ms)
  50%    131
  66%    133
  75%    141
  80%    149
  90%    167
  95%    196
  98%   3137
  99%   3353
 100%  10480 (longest request)
300的并发下,php代码的测试速度已经比较慢了,而且失败的请求数所占比有1.5%,虽然平均每个请求处理时间仍然为nginx模块的4到5倍,但是存在一些请求的相应非常慢,最长耗时请求是nginx模块的接近100倍

C,100并发,10000请求,Get

[root@fedora nginx-1.1.12]# ab -c 100 -n 10000 http://127.0.0.1/bar?cmd=get&key=bananp
[1] 28575
[root@fedora nginx-1.1.12]# 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 127.0.0.1 (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:        nginx/1.1.12
Server Hostname:        127.0.0.1
Server Port:            80

Document Path:          /bar?cmd=get
Document Length:        193 bytes

Concurrency Level:      100
Time taken for tests:   2.577 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Non-2xx responses:      10000
Total transferred:      3550000 bytes
HTML transferred:       1930000 bytes
Requests per second:    3880.01 [#/sec] (mean)
Time per request:       25.773 [ms] (mean)
Time per request:       0.258 [ms] (mean, across all concurrent requests)
Transfer rate:          1345.12 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   2.1      0      29
Processing:     9   25   5.1     23      47
Waiting:        6   25   5.2     23      47
Total:         13   25   5.2     23      47

Percentage of the requests served within a certain time (ms)
  50%     23
  66%     25
  75%     30
  80%     31
  90%     33
  95%     35
  98%     38
  99%     41
 100%     47 (longest request)
nginx模块的get相应也是很快,和set操作差不多

[root@fedora nginx-1.1.12]# ab -c 100 -n 10000 http://127.0.0.1/memc.php?cmd=get&key=bananp
[1] 28648
[root@fedora nginx-1.1.12]# 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 127.0.0.1 (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:        nginx/1.1.12
Server Hostname:        127.0.0.1
Server Port:            80

Document Path:          /memc.php?cmd=get
Document Length:        164 bytes

Concurrency Level:      100
Time taken for tests:   10.220 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Total transferred:      3370000 bytes
HTML transferred:       1640000 bytes
Requests per second:    978.43 [#/sec] (mean)
Time per request:       102.205 [ms] (mean)
Time per request:       1.022 [ms] (mean, across all concurrent requests)
Transfer rate:          322.00 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   3.9      0      49
Processing:    18  101   9.4     98     133
Waiting:       17  101   9.4     98     133
Total:         57  102   8.7     98     145

Percentage of the requests served within a certain time (ms)
  50%     98
  66%    100
  75%    102
  80%    106
  90%    117
  95%    122
  98%    126
  99%    128
 100%    145 (longest request)

D,300并发,10000请求,Get

[root@fedora nginx-1.1.12]# ab -c 300 -n 10000 http://127.0.0.1/bar?cmd=get&key=bananp
[1] 28757
[root@fedora nginx-1.1.12]# 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 127.0.0.1 (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:        nginx/1.1.12
Server Hostname:        127.0.0.1
Server Port:            80

Document Path:          /bar?cmd=get
Document Length:        193 bytes

Concurrency Level:      300
Time taken for tests:   2.717 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Non-2xx responses:      10007
Total transferred:      3552485 bytes
HTML transferred:       1931351 bytes
Requests per second:    3680.52 [#/sec] (mean)
Time per request:       81.510 [ms] (mean)
Time per request:       0.272 [ms] (mean, across all concurrent requests)
Transfer rate:          1276.85 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    6   9.1      3      71
Processing:     7   32  14.9     28     468
Waiting:        6   29  15.3     25     468
Total:         17   37  18.5     33     469

Percentage of the requests served within a certain time (ms)
  50%     33
  66%     39
  75%     43
  80%     45
  90%     51
  95%     65
  98%     99
  99%    108
 100%    469 (longest request)
下面是php的300并发的测试结果

[root@fedora nginx-1.1.12]# ab -c 300 -n 10000 http://127.0.0.1/memc.php?cmd=get&key=bananp
[1] 28878
[root@fedora nginx-1.1.12]# 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 127.0.0.1 (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:        nginx/1.1.12
Server Hostname:        127.0.0.1
Server Port:            80

Document Path:          /memc.php?cmd=get
Document Length:        164 bytes

Concurrency Level:      300
Time taken for tests:   13.379 seconds
Complete requests:      10000
Failed requests:        149
   (Connect: 0, Receive: 0, Length: 149, Exceptions: 0)
Write errors:           0
Non-2xx responses:      149
Total transferred:      3368212 bytes
HTML transferred:       1641341 bytes
Requests per second:    747.41 [#/sec] (mean)
Time per request:       401.385 [ms] (mean)
Time per request:       1.338 [ms] (mean, across all concurrent requests)
Transfer rate:          245.84 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0   86 562.4      0    9027
Processing:     1  169 365.5    134    7848
Waiting:        1  169 365.5    134    7848
Total:         82  255 777.1    136   10853

Percentage of the requests served within a certain time (ms)
  50%    136
  66%    145
  75%    159
  80%    167
  90%    194
  95%    222
  98%   3009
  99%   3162
 100%  10853 (longest request)
可以看出效率的差距也是比较明显的,而且失败的请求也有1.5%左右。(而在500并发的测试下php连接memcache就有点力不从心了,测试经常崩溃掉)

从上面的几组测试可以看出nginx的高并发低延迟的性能是非常明显的,而且较短的时间内响应可以减轻服务器的cpu负担,我们在测试过程中可以通过vmstat看到cpu资源经常被耗尽,都是满速在运转。

memc-nginx-module这个模块提供了get,set,add,replace,append,delete,flush_all,stats等常用memcache操作指令的支持,所以还是功能比较完整的模块,值得推广使用。

你可能感兴趣的:(Server)