标签:
最近在做通知系统,前端用的轮询方式(后端压力不小),因为时间比较紧,开始我准备把未读通知标识存在数据库中,但是每次拿数据的时候需要查询一遍数据库,总监说你这样效率较低,说你可以根据用户id作为key放在redis中存储。说说自己在做这个的一些体会和闲下来总结的一些笔记。
phpredis connect pconnect
我最开始使用的是connect,每次请求完毕关闭连接
1 <?php 2 $redis = new Redis(); 3 $nError = $redis->connect(‘127.0.0.1‘, 6379); 4 if ($nError != 1) 5 echo -9998; 6 $redis->incr(‘newCount‘); 7 $redis->close();
而另外一种是pconnect
1 <?php 2 $redis = new Redis(); 3 $nError = $redis->pconnect(‘127.0.0.1‘, 6379); 4 if ($nError != 1) 5 echo -9998; 6 $redis->incr(‘newCount‘);
进行了一下ab压测 ab -c500 -n10000 http://192.168.23.128/redis.php
1.connect
Concurrency Level: 500
Time taken for tests: 25.484 seconds
Complete requests: 10000
Failed requests: 146
(Connect: 0, Receive: 0, Length: 146, Exceptions: 0)
Non-2xx responses: 146
Total transferred: 1868840 bytes
HTML transferred: 78402 bytes
Requests per second: 392.40 [#/sec] (mean)
Time per request: 1274.198 [ms] (mean)
Time per request: 2.548 [ms] (mean, across all concurrent requests)
Transfer rate: 71.62 [Kbytes/sec] received
2.pconnect
Concurrency Level: 500
Time taken for tests: 13.274 seconds
Complete requests: 10000
Failed requests: 4
(Connect: 0, Receive: 0, Length: 4, Exceptions: 0)
Non-2xx responses: 4
Total transferred: 1792160 bytes
HTML transferred: 2148 bytes
Requests per second: 753.34 [#/sec] (mean)
Time per request: 663.713 [ms] (mean)
Time per request: 1.327 [ms] (mean, across all concurrent requests)
Transfer rate: 131.85 [Kbytes/sec] received
会发现pconnect比connect会快一倍(会重用redis连接,不需要消耗建立连接时间)
phpredis api上有描述The connection will not be closed on close
or end of request until the php process ends
也就是说在php脚本访问结束,也并不会关闭连接,直到php进程结束
我在做pconnect的ab压测结束后,发现有几个redis的连接一直在,直到我杀死php-fpm的进程,也可以通过设置redis.conf文件中的timeout参数
nginx+lua+redis 短连接 连接池
上篇文章提到过nginx+lua+redis,之所以没用在项目中,主要原因是时间紧,对新的东西了解的不多,这里贴一下我闲暇时对于nginx+lua+redis的ab测试
短连接
1 local redis = require "resty.redis" 2 local red = redis:new() 3 local ok, err = red:connect("127.0.0.1", 6379) 4 if not ok then 5 ngx.say("failed to connect: ", err) 6 return 7 end 8 ok,err=red:incr("newCount") 9 if not ok then 10 ngx.say("failed to increase newCount",err) 11 return 12 end 13 14 local ok,err = red:close() 15 if not ok then 16 ngx.say("close redis error : ",err) 17 return 18 end
连接池
1 local redis = require "resty.redis" 2 local red = redis:new() 3 local ok, err = red:connect("127.0.0.1", 6379) 4 if not ok then 5 ngx.say("failed to connect: ", err) 6 return 7 end 8 ok,err=red:incr("newCount") 9 if not ok then 10 ngx.say("failed to increase newCount",err) 11 return 12 end 13 14 local pool_max_idle_time = 10000 --10s 15 local pool_size = 100 --连接池大小 16 local ok,err = red:set_keepalive(pool_max_idle_time,pool_size) 17 if not ok then 18 ngx.say("set keepalive error : ",err) 19 return 20 end
ab测试 ab -c500 -n10000 http://192.168.23.128/redis
短连接
Concurrency Level: 500
Time taken for tests: 4.791 seconds
Complete requests: 10000
Failed requests: 72
(Connect: 0, Receive: 0, Length: 72, Exceptions: 0)
Total transferred: 1492016 bytes
HTML transferred: 1944 bytes
Requests per second: 2087.06 [#/sec] (mean)
Time per request: 239.571 [ms] (mean)
Time per request: 0.479 [ms] (mean, across all concurrent requests)
Transfer rate: 304.09 [Kbytes/sec] received
连接池
Concurrency Level: 500
Time taken for tests: 2.986 seconds
Complete requests: 10000
Failed requests: 0
Total transferred: 1490000 bytes
HTML transferred: 0 bytes
Requests per second: 3348.62 [#/sec] (mean)
Time per request: 149.315 [ms] (mean)
Time per request: 0.299 [ms] (mean, across all concurrent requests)
Transfer rate: 487.25 [Kbytes/sec] received
从上面结果可以看出nginx+lua+redis的效率比phpredis的快5倍左右,以后有机会试试在项目中使用
参考链接
http://blog.csdn.net/qmhball/article/details/46988111
https://github.com/openresty/lua-resty-redis
nginx+php+redis vs nginx+lua+redis
标签:
原文地址:http://www.cnblogs.com/mingao/p/5043552.html