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 0set $memc_exptime $arg_exptime; # defaults to 0memc_cmds_allowed get set add delete flush_all;memc_pass 127.0.0.1:11211;}

对应的php代码文件如下:

<?php$mem=new Memcache;$mem->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 requestsServer Software:        nginx/1.1.12
Server Hostname:        127.0.0.1
Server Port:            80Document Path:          /bar?cmd=set
Document Length:        193 bytesConcurrency 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] receivedConnection 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      45Percentage of the requests served within a certain time (ms)50%     2166%     2275%     2580%     2790%     3095%     3298%     3499%     35100%     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 requestsServer Software:        nginx/1.1.12
Server Hostname:        127.0.0.1
Server Port:            80Document Path:          /memc.php?cmd=set
Document Length:        164 bytesConcurrency 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] receivedConnection 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     131Percentage of the requests served within a certain time (ms)50%     9266%     9375%     9480%     9790%    10895%    11698%    12399%    124100%    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 requestsServer Software:        nginx/1.1.12
Server Hostname:        127.0.0.1
Server Port:            80Document Path:          /bar?cmd=set
Document Length:        193 bytesConcurrency 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] receivedConnection 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     107Percentage of the requests served within a certain time (ms)50%     3266%     3975%     4180%     4390%     4695%     5298%     9699%    101100%    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 requestsServer Software:        nginx/1.1.12
Server Hostname:        127.0.0.1
Server Port:            80Document Path:          /memc.php?cmd=set
Document Length:        164 bytesConcurrency 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] receivedConnection 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   10480Percentage of the requests served within a certain time (ms)50%    13166%    13375%    14180%    14990%    16795%    19698%   313799%   3353100%  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 requestsServer Software:        nginx/1.1.12
Server Hostname:        127.0.0.1
Server Port:            80Document Path:          /bar?cmd=get
Document Length:        193 bytesConcurrency 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] receivedConnection 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      47Percentage of the requests served within a certain time (ms)50%     2366%     2575%     3080%     3190%     3395%     3598%     3899%     41100%     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 requestsServer Software:        nginx/1.1.12
Server Hostname:        127.0.0.1
Server Port:            80Document Path:          /memc.php?cmd=get
Document Length:        164 bytesConcurrency 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] receivedConnection 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     145Percentage of the requests served within a certain time (ms)50%     9866%    10075%    10280%    10690%    11795%    12298%    12699%    128100%    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 requestsServer Software:        nginx/1.1.12
Server Hostname:        127.0.0.1
Server Port:            80Document Path:          /bar?cmd=get
Document Length:        193 bytesConcurrency 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] receivedConnection 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     469Percentage of the requests served within a certain time (ms)50%     3366%     3975%     4380%     4590%     5195%     6598%     9999%    108100%    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 requestsServer Software:        nginx/1.1.12
Server Hostname:        127.0.0.1
Server Port:            80Document Path:          /memc.php?cmd=get
Document Length:        164 bytesConcurrency 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] receivedConnection 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   10853Percentage of the requests served within a certain time (ms)50%    13666%    14575%    15980%    16790%    19495%    22298%   300999%   3162100%  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操作指令的支持,所以还是功能比较完整的模块,值得推广使用。

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

  1. nginx使用ssl模块配置支持HTTPS访问

    背景: 项目开发中用到了微信小程序,但是服务器配置URL必须是HTTPS,所以需要通过配置nginx的SSL模块来支持HTTPS访问,也就是说,要做一个网站域名为 XXX.com 要求通过HTTPS: ...

  2. Nginx research, nginx module development

    catalog 1. 初探nginx架构 2. handler模块 3. Nginx编译.安装.配置 4. Hello World模块开发 1. 初探nginx架构 nginx在启动后,在unix系统 ...

  3. Nginx学习之五:Nginx第三方模块

    为什么80%的码农都做不了架构师?>>>    第三方模块 这些模块不被官方所支持,亦未必能跨越Nginx的版本兼容.不过其中有些模块证明是对多数用户是有用的.只在于个人的风险问题. ...

  4. Nginx handler模块

    Nginx handler模块 handler模块简介 相信大家在看了前一章的模块概述以后,都对nginx的模块有了一个基本的认识.基本上作为第三方开发者最可能开发的就是三种类型的模块,即handle ...

  5. centos 7.6 —— Nginx 配置网页防盗链FPM参数优化

    centos 7.6 -- Nginx 配置网页防盗链&&FPM参数优化 一.网页防盗链 (1)防盗链端--服务端配置(192.168.75.134) 1.1 服务端配置DNS服务,域 ...

  6. NGINX 常见模块

    文章目录 **NGINX 常见模块** 4.11 第三方模块 4.11.1 nginx-module-vts 模块实现流量监控 4.11.2 echo实现信息显示 4.12 Nginx 变量使用 4. ...

  7. nginx strip模块优化页面

    为什么80%的码农都做不了架构师?>>>    nginx strip模块删除不必要的空格 在nginx官方wiki的第三方模块中看到nginx strip模块,简单的看下功能,大意 ...

  8. 《深入理解Nginx:模块开发与架构解析》一1.2 为什么选择Nginx

    1.2 为什么选择Nginx 为什么选择Nginx?因为它具有以下特点: (1)更快 这表现在两个方面:一方面,在正常情况下,单次请求会得到更快的响应:另一方面,在高峰期(如有数以万计的并发请求),N ...

  9. nginx自定义模块编写-实时统计模块--转载

    原文:http://www.vimer.cn/2012/05/nginx%E8%87%AA%E5%AE%9A%E4%B9%89%E6%A8%A1%E5%9D%97%E7%BC%96%E5%86%99- ...

最新文章

  1. 【Android RTMP】Android Camera 视频数据采集预览 ( 视频采集相关概念 | 摄像头预览参数设置 | 摄像头预览数据回调接口 )
  2. java做的模板商城_不吹不黑4个超火Java开源项目,接私活、练手、必备项目
  3. ubuntu服务器安装指南
  4. eclipse插件安装的方法
  5. 如何使用TensorFlow构建简单的图像识别系统(第2部分)
  6. BigPipe 大的页面分割成一个一个管道
  7. 力扣150-逆波兰表达式求值(C++,附思路)
  8. countdownlatch的使用详解(好懂!!)
  9. 配置hadoop 使用fair scheduler调度器
  10. Linux BASH多进程并行处理的方法实现
  11. 如何为VMware创建的虚拟PC机安装Linux操作系统
  12. 【密码专栏】动手计算双线性对(上)
  13. 单片机原理及应用C语言实验,《单片机原理及应用》实验指导书.doc
  14. matlab全局变量和局部变量和子函数
  15. 2021年9月份最新数据库排行榜出炉
  16. VSCode 的扩展包C/C++ IntelliSense, debugging, and code browsing的IntelliSense功能无法使用
  17. openjdk Font实现斜体
  18. 【CodingNoBorder - 07】无际软工队 - 求职岛:ALPHA 阶段测试报告
  19. 树形结构模型Django-MPTT
  20. 技能终将过时,而能力与时俱进

热门文章

  1. SiteRAS一款外贸网站SEO分析工具,给您的网站做个深度体检
  2. 操作系统-CPU与外设交互方式
  3. lEO数值资产系统新版本c2c币数值合约交易平台自动撮合松机器人功能
  4. Linux常用命令简略版
  5. 【LOJ】#2289. 「THUWC 2017」在美妙的数学王国中畅游
  6. 图像中的天空区域检测!
  7. 智能电视应用与移动应用开发的比较
  8. create remote oracle odbc data source on win10
  9. MySQL学习博客第一篇
  10. ubuntu14.10 tftp服务配置20150824