一、短连接开始测试
ab -n 5000 -c 100 -k 127.0.0.1/test_redis_short
#demo1
Concurrency Level: 100 Time taken for tests: 0.745 seconds Complete requests: 5000 Failed requests: 0 Keep-Alive requests: 5000 Total transferred: 1095000 bytes HTML transferred: 330000 bytes Requests per second: 6708.13 [#/sec] (mean) Time per request: 14.907 [ms] (mean) Time per request: 0.149 [ms] (mean, across all concurrent requests) Transfer rate: 1434.65 [Kbytes/sec] received
#demo2
Concurrency Level: 100 Time taken for tests: 0.697 seconds Complete requests: 5000 Failed requests: 0 Keep-Alive requests: 5000 Total transferred: 1095000 bytes HTML transferred: 330000 bytes Requests per second: 7177.65 [#/sec] (mean) Time per request: 13.932 [ms] (mean) Time per request: 0.139 [ms] (mean, across all concurrent requests) Transfer rate: 1535.06 [Kbytes/sec] received
ab -n 10000 -c 100 -k 127.0.0.1/test_redis_short
#demo1
Concurrency Level: 100 Time taken for tests: 1.211 seconds Complete requests: 10000 Failed requests: 0 Keep-Alive requests: 9945 Total transferred: 2189725 bytes HTML transferred: 660000 bytes Requests per second: 8256.17 [#/sec] (mean) Time per request: 12.112 [ms] (mean) Time per request: 0.121 [ms] (mean, across all concurrent requests) Transfer rate: 1765.50 [Kbytes/sec] received
#demo2 比较卡
Concurrency Level: 100 Time taken for tests: 17.364 seconds Complete requests: 10000 Failed requests: 0 Keep-Alive requests: 9948 Total transferred: 2189740 bytes HTML transferred: 660000 bytes Requests per second: 575.90 [#/sec] (mean) Time per request: 173.643 [ms] (mean) Time per request: 1.736 [ms] (mean, across all concurrent requests) Transfer rate: 123.15 [Kbytes/sec] received
ab -n 20000 -c 100 -k 127.0.0.1/test_redis_short
#demo1
Concurrency Level: 100 Time taken for tests: 19.374 seconds Complete requests: 20000 Failed requests: 116 (Connect: 0, Receive: 0, Length: 116, Exceptions: 0) Keep-Alive requests: 19866 Total transferred: 4377590 bytes HTML transferred: 1318260 bytes Requests per second: 1032.32 [#/sec] (mean) Time per request: 96.869 [ms] (mean) Time per request: 0.969 [ms] (mean, across all concurrent requests) Transfer rate: 220.66 [Kbytes/sec] received
Nginx CPU内存情况
ab -n 30000 -c 100 -k 127.0.0.1/test_redis_short
#demo1
Concurrency Level: 100 Time taken for tests: 50.960 seconds Complete requests: 30000 Failed requests: 20304 (Connect: 0, Receive: 0, Length: 20304, Exceptions: 0) Keep-Alive requests: 29767 Total transferred: 6423395 bytes HTML transferred: 1834560 bytes Requests per second: 588.69 [#/sec] (mean) Time per request: 169.867 [ms] (mean) Time per request: 1.699 [ms] (mean, across all concurrent requests) Transfer rate: 123.09 [Kbytes/sec] received
Nginx CPU内存情况
短连接一旦-n 大于 10000 ,nginx 刚开始直接100%,但是redis-server 负载相对较小,以下是 -n = 40000
Requests per second: 1105.62 [#/sec] (mean)
第二次 -n = 40000 测试结果
Requests per second: 933.82 [#/sec] (mean)
总结:
使用短连接一开始nginx 负载就满载100% ,太可怕了,但是redis-server 负载相对较小,(30000--->100%) 、(20000--->90%)、(10000--->70~90%),测试数据显示,安全最大连接在5000 以内
二、长连接开始测试
ab -n 5000 -c 100 -k 127.0.0.1/test_redis_long
#demo1
Concurrency Level: 100 Time taken for tests: 0.289 seconds Complete requests: 5000 Failed requests: 0 Keep-Alive requests: 5000 Total transferred: 1035000 bytes HTML transferred: 270000 bytes Requests per second: 17322.98 [#/sec] (mean) Time per request: 5.773 [ms] (mean) Time per request: 0.058 [ms] (mean, across all concurrent requests) Transfer rate: 3501.81 [Kbytes/sec] received
#demo2
Concurrency Level: 100 Time taken for tests: 0.278 seconds Complete requests: 5000 Failed requests: 0 Keep-Alive requests: 5000 Total transferred: 1035000 bytes HTML transferred: 270000 bytes Requests per second: 17977.46 [#/sec] (mean) Time per request: 5.563 [ms] (mean) Time per request: 0.056 [ms] (mean, across all concurrent requests) Transfer rate: 3634.12 [Kbytes/sec] received
ab -n 10000 -c 100 -k 127.0.0.1/test_redis_long
#demo1
Concurrency Level: 100 Time taken for tests: 0.670 seconds Complete requests: 10000 Failed requests: 0 Keep-Alive requests: 9905 Total transferred: 2069525 bytes HTML transferred: 540000 bytes Requests per second: 14927.29 [#/sec] (mean) Time per request: 6.699 [ms] (mean) Time per request: 0.067 [ms] (mean, across all concurrent requests) Transfer rate: 3016.84 [Kbytes/sec] received
#demo2
Concurrency Level: 100 Time taken for tests: 0.812 seconds Complete requests: 10000 Failed requests: 0 Keep-Alive requests: 9915 Total transferred: 2069575 bytes HTML transferred: 540000 bytes Requests per second: 12310.57 [#/sec] (mean) Time per request: 8.123 [ms] (mean) Time per request: 0.081 [ms] (mean, across all concurrent requests) Transfer rate: 2488.05 [Kbytes/sec] received
#demo3
Concurrency Level: 100 Time taken for tests: 0.662 seconds Complete requests: 10000 Failed requests: 0 Keep-Alive requests: 9942 Total transferred: 2069710 bytes HTML transferred: 540000 bytes Requests per second: 15114.05 [#/sec] (mean) Time per request: 6.616 [ms] (mean) Time per request: 0.066 [ms] (mean, across all concurrent requests) Transfer rate: 3054.85 [Kbytes/sec] received
ab -n 20000 -c 100 -k 127.0.0.1/test_redis_long
#demo1
Concurrency Level: 100 Time taken for tests: 0.981 seconds Complete requests: 20000 Failed requests: 0 Keep-Alive requests: 19869 Total transferred: 4139345 bytes HTML transferred: 1080000 bytes Requests per second: 20390.39 [#/sec] (mean) Time per request: 4.904 [ms] (mean) Time per request: 0.049 [ms] (mean, across all concurrent requests) Transfer rate: 4121.23 [Kbytes/sec] received
ab -n 30000 -c 100 -k 127.0.0.1/test_redis_long
#demo1
Concurrency Level: 100 Time taken for tests: 1.392 seconds Complete requests: 30000 Failed requests: 0 Keep-Alive requests: 29745 Total transferred: 6208725 bytes HTML transferred: 1620000 bytes Requests per second: 21556.79 [#/sec] (mean) Time per request: 4.639 [ms] (mean) Time per request: 0.046 [ms] (mean, across all concurrent requests) Transfer rate: 4356.78 [Kbytes/sec] received
ab -n 40000 -c 100 -k 127.0.0.1/test_redis_long
#demo1
Concurrency Level: 100 Time taken for tests: 1.711 seconds Complete requests: 40000 Failed requests: 0 Keep-Alive requests: 39651 Total transferred: 8278255 bytes HTML transferred: 2160000 bytes Requests per second: 23377.05 [#/sec] (mean) Time per request: 4.278 [ms] (mean) Time per request: 0.043 [ms] (mean, across all concurrent requests) Transfer rate: 4724.64 [Kbytes/sec] received
ab -n 50000 -c 100 -k 127.0.0.1/test_redis_long
#demo1
Concurrency Level: 100 Time taken for tests: 1.995 seconds Complete requests: 50000 Failed requests: 0 Keep-Alive requests: 49555 Total transferred: 10347775 bytes HTML transferred: 2700000 bytes Requests per second: 25059.81 [#/sec] (mean) Time per request: 3.990 [ms] (mean) Time per request: 0.040 [ms] (mean, across all concurrent requests) Transfer rate: 5064.71 [Kbytes/sec] received
ab -n 100000 -c 100 -k 127.0.0.1/test_redis_long
#demo1
Concurrency Level: 100 Time taken for tests: 3.613 seconds Complete requests: 100000 Failed requests: 0 Keep-Alive requests: 99049 Total transferred: 20695245 bytes HTML transferred: 5400000 bytes Requests per second: 27676.11 [#/sec] (mean) Time per request: 3.613 [ms] (mean) Time per request: 0.036 [ms] (mean, across all concurrent requests) Transfer rate: 5593.40 [Kbytes/sec] received
ab -n 200000 -c 100 -k 127.0.0.1/test_redis_long
#demo1
Concurrency Level: 100 Time taken for tests: 7.562 seconds Complete requests: 200000 Failed requests: 0 Keep-Alive requests: 198052 Total transferred: 41390260 bytes HTML transferred: 10800000 bytes Requests per second: 26447.14 [#/sec] (mean) Time per request: 3.781 [ms] (mean) Time per request: 0.038 [ms] (mean, across all concurrent requests) Transfer rate: 5344.99 [Kbytes/sec] received
ab -n 500000 -c 100 -k 127.0.0.1/test_redis_long
#demo1
Concurrency Level: 100 Time taken for tests: 17.541 seconds Complete requests: 500000 Failed requests: 0 Keep-Alive requests: 495051 Total transferred: 103475255 bytes HTML transferred: 27000000 bytes Requests per second: 28505.36 [#/sec] (mean) Time per request: 3.508 [ms] (mean) Time per request: 0.035 [ms] (mean, across all concurrent requests) Transfer rate: 5760.94 [Kbytes/sec] received
ab -n 1000000 -c 100 -k 127.0.0.1/test_redis_long
#demo1
Concurrency Level: 100 Time taken for tests: 36.749 seconds Complete requests: 1000000 Failed requests: 0 Keep-Alive requests: 990054 Total transferred: 206950270 bytes HTML transferred: 54000000 bytes Requests per second: 27211.29 [#/sec] (mean) Time per request: 3.675 [ms] (mean) Time per request: 0.037 [ms] (mean, across all concurrent requests) Transfer rate: 5499.40 [Kbytes/sec] received
ab -n 1500000 -c 100 -k 127.0.0.1/test_redis_long
#demo1
Concurrency Level: 100 Time taken for tests: 57.548 seconds Complete requests: 1500000 Failed requests: 0 Keep-Alive requests: 1485050 Total transferred: 310425250 bytes HTML transferred: 81000000 bytes Requests per second: 26065.16 [#/sec] (mean) Time per request: 3.837 [ms] (mean) Time per request: 0.038 [ms] (mean, across all concurrent requests) Transfer rate: 5267.76 [Kbytes/sec] received
ab -n 2000000 -c 100 -k 127.0.0.1/test_redis_long
#demo1
Concurrency Level: 100 Time taken for tests: 76.746 seconds Complete requests: 2000000 Failed requests: 0 Keep-Alive requests: 1980048 Total transferred: 413900240 bytes HTML transferred: 108000000 bytes Requests per second: 26060.04 [#/sec] (mean) Time per request: 3.837 [ms] (mean) Time per request: 0.038 [ms] (mean, across all concurrent requests) Transfer rate: 5266.73 [Kbytes/sec] received
Nginx CPU内存情况
ab -n 2500000 -c 100 -k 127.0.0.1/test_redis_long
#demo1
Concurrency Level: 100 Time taken for tests: 90.761 seconds Complete requests: 2500000 Failed requests: 0 Keep-Alive requests: 2475045 Total transferred: 517375225 bytes HTML transferred: 135000000 bytes Requests per second: 27544.95 [#/sec] (mean) Time per request: 3.630 [ms] (mean) Time per request: 0.036 [ms] (mean, across all concurrent requests) Transfer rate: 5566.83 [Kbytes/sec] received
Nginx CPU内存情况
长连接-n = 2500000 ,nginx 刚开始不会超过50%,但是redis-server 负载相对高点,最高在50%左右,测试结果:
Concurrency Level: 100 Time taken for tests: 93.482 seconds Complete requests: 2500000 Failed requests: 0 Keep-Alive requests: 2475048 Total transferred: 517375240 bytes HTML transferred: 135000000 bytes Requests per second: 26743.15 [#/sec] (mean) Time per request: 3.739 [ms] (mean) Time per request: 0.037 [ms] (mean, across all concurrent requests) Transfer rate: 5404.78 [Kbytes/sec] received
ab -n 5000000 -c 100 -k 127.0.0.1/test_redis_long
#demo1
Concurrency Level: 100 Time taken for tests: 221.492 seconds Complete requests: 5000000 Failed requests: 0 Keep-Alive requests: 4950051 Total transferred: 1034750255 bytes HTML transferred: 270000000 bytes Requests per second: 22574.15 [#/sec] (mean) Time per request: 4.430 [ms] (mean) Time per request: 0.044 [ms] (mean, across all concurrent requests) Transfer rate: 4562.23 [Kbytes/sec] received
Nginx CPU内存情况
Completed 2000000 requests 请求截图
Completed 3500000 requests 请求截图 ,这时候的redis-server 负载保持在38%左右
测试到最后AB 压力直接爆掉
(-n---->nginx负载----->redis-server负载---->Requests per second)、
(100000--->37%--->37%---->28303.90) 表示很快完成
(200000--->50%--->56%--->33518.21)
(500000--->67%--->74%--->23959.52) redis-server 不稳定
(1000000--->50%--->50%--->23959.52) 2个都比较稳定,都能够保持在50%以下
总结:
使用长连接,10W 连接时毫无压力的,示,安全最大连接在20W 以内(测试环境可以100W没问题)
1、短连接Nginx负载太高 10000 链接负载直接飙到90+%
三、测试代码
test_long.lua
--[[----------------------------------------------------------------------- * | Copyright (C) Shaobo Wan (Tinywan) * | Github: https://github.com/Tinywan * | Author: Tinywan * | Date: 2017/5/8 16:25 * | Mail: Overcome.wan@Gmail.com * |------------------------------------------------------------------------ * | version: 1.0 * | description: redis 长连接带连接池测试 * |------------------------------------------------------------------------ --]] local redis = require "resty.redis" local red = redis:new() local ok, err = red:connect("127.0.0.1", 63700) if not ok then ngx.say("failed to connect: ", err) return end -- auth local count count, err = red:get_reused_times() if 0 == count then local res, err = red:auth("tinywan123456") if not res then ngx.say("failed to authenticate: ", err) return end elseif err then ngx.say("failed to get_reused_times: ", err) end ok, err = red:set("dog_test_long", "an animal 222"..ngx.time()) if not ok then ngx.say("failed to set dog: ", err) return end ngx.say("set result: ", ok) local res, err = red:get("dog_test_long") if not res then ngx.say("failed to get dog: ", err) return end if res == ngx.null then ngx.say("dog not found.") return end ngx.say("dog_test_long: ", res) local ok, err = red:set_keepalive(10000, 100) if not ok then ngx.say("failed to set keepalive: ", err) return end
test_short.lua
--[[----------------------------------------------------------------------- * | Copyright (C) Shaobo Wan (Tinywan) * | Github: https://github.com/Tinywan * | Author: Tinywan * | Date: 2017/5/8 16:25 * | Mail: Overcome.wan@Gmail.com * |------------------------------------------------------------------------ * | version: 1.0 * | description: redis 短连接测试 * |------------------------------------------------------------------------ --]] local redis = require "resty.redis" local red = redis:new() --red:set_timeout(1000) -- 1 sec -- or connect to a unix domain socket file listened -- by a redis server: -- local ok, err = red:connect("unix:/path/to/redis.sock") local ok, err = red:connect("127.0.0.1", 63700) if not ok then ngx.say("failed to connect: ", err) return end local res, err = red:auth("tinywan123456") if not res then ngx.say("failed to authenticate: ", err) return end ok, err = red:set("dog_test_short", "an animal dog_test_short"..ngx.time()) if not ok then ngx.say("failed to set dog: ", err) return end ngx.say("set result: ", ok) local res, err = red:get("dog_test_short") if not res then ngx.say("failed to get dog: ", err) return end if res == ngx.null then ngx.say("dog not found.") return end ngx.say("dog_test_short: ", res)
redis_iresty_short.lua
--[[----------------------------------------------------------------------- * | Copyright (C) Shaobo Wan (Tinywan) * | Github: https://github.com/Tinywan * | Author: Tinywan * | Date: 2017/5/8 16:25 * | Mail: Overcome.wan@Gmail.com * |------------------------------------------------------------------------ * | version: 1.0 * | description: redis_iresty 短连接测试 * |------------------------------------------------------------------------ --]] local redis = require "resty.redis_iresty" local red = redis:new() local res, err = red:auth("tinywan123456") if not res then ngx.say("failed to authenticate: ", err) return end local ok, err = red:set("OPenresty_short", "NGINX-based OPenresty_short") if not ok then ngx.say("failed to set: ", err) return end ngx.say("set result: ", ok)
ab -n 5000 -c 100 -k 127.0.0.1/redis_iresty_short
Concurrency Level: 100 Time taken for tests: 0.682 seconds Complete requests: 5000 Failed requests: 0 Keep-Alive requests: 5000 Total transferred: 840000 bytes HTML transferred: 75000 bytes Requests per second: 7332.55 [#/sec] (mean) Time per request: 13.638 [ms] (mean) Time per request: 0.136 [ms] (mean, across all concurrent requests) Transfer rate: 1203.00 [Kbytes/sec] received
ab -n 10000 -c 100 -k 127.0.0.1/redis_iresty_short
Concurrency Level: 100 Time taken for tests: 1.120 seconds Complete requests: 10000 Failed requests: 0 Keep-Alive requests: 9900 Total transferred: 1679500 bytes HTML transferred: 150000 bytes Requests per second: 8925.68 [#/sec] (mean) Time per request: 11.204 [ms] (mean) Time per request: 0.112 [ms] (mean, across all concurrent requests) Transfer rate: 1463.93 [Kbytes/sec] received
ab -n 100000 -c 100 -k 127.0.0.1/redis_iresty_short
Concurrency Level: 100 Time taken for tests: 10.308 seconds Complete requests: 100000 Failed requests: 0 Keep-Alive requests: 99051 Total transferred: 16795255 bytes HTML transferred: 1500000 bytes Requests per second: 9701.45 [#/sec] (mean) Time per request: 10.308 [ms] (mean) Time per request: 0.103 [ms] (mean, across all concurrent requests) Transfer rate: 1591.19 [Kbytes/sec] received
CPU
ab -n 500000 -c 100 -k 127.0.0.1/redis_iresty_short
Concurrency Level: 100 Time taken for tests: 55.198 seconds Complete requests: 500000 Failed requests: 0 Keep-Alive requests: 495047 Total transferred: 83975235 bytes HTML transferred: 7500000 bytes Requests per second: 9058.32 [#/sec] (mean) Time per request: 11.040 [ms] (mean) Time per request: 0.110 [ms] (mean, across all concurrent requests) Transfer rate: 1485.69 [Kbytes/sec] received
ab -n 1000000 -c 100 -k 127.0.0.1/redis_iresty_short
Concurrency Level: 100 Time taken for tests: 112.221 seconds Complete requests: 1000000 Failed requests: 0 Keep-Alive requests: 990050 Total transferred: 167950250 bytes HTML transferred: 15000000 bytes Requests per second: 8911.00 [#/sec] (mean) Time per request: 11.222 [ms] (mean) Time per request: 0.112 [ms] (mean, across all concurrent requests) Transfer rate: 1461.53 [Kbytes/sec] received
CPU 负载
ab -n 5000000 -c 100 -k 127.0.0.1/redis_iresty_short
比较慢,但是能跑
Concurrency Level: 100 Time taken for tests: 534.139 seconds Complete requests: 5000000 Failed requests: 0 Keep-Alive requests: 4950051 Total transferred: 839750255 bytes HTML transferred: 75000000 bytes Requests per second: 9360.85 [#/sec] (mean) Time per request: 10.683 [ms] (mean) Time per request: 0.107 [ms] (mean, across all concurrent requests) Transfer rate: 1535.31 [Kbytes/sec] received
redis_iresty_long.lua
--[[----------------------------------------------------------------------- * | Copyright (C) Shaobo Wan (Tinywan) * | Github: https://github.com/Tinywan * | Author: Tinywan * | Date: 2017/5/8 16:25 * | Mail: Overcome.wan@Gmail.com * |------------------------------------------------------------------------ * | version: 1.0 * | description: redis_iresty 长连接带连接池测试 * |------------------------------------------------------------------------ --]] local redis = require "resty.redis_iresty" local red = redis:new() -- auth local count count, err = red:get_reused_times() if 0 == count then local res, err = red:auth("tinywan123456") if not res then ngx.say("failed to authenticate: ", err) return end elseif err then ngx.say("failed to get_reused_times: ", err) end local ok, err = red:set("OPenresty", "NGINX-based Media Streaming Server") if not ok then ngx.say("failed to set: ", err) return end ngx.say("set result: ", ok) local ok, err = red:set_keepalive(10000, 100) if not ok then ngx.say("failed to set keepalive: ", err) return end
ab -n 5000 -c 100 -k 127.0.0.1/redis_iresty_long 很好
Concurrency Level: 100 Time taken for tests: 1.110 seconds Complete requests: 5000 Failed requests: 0 Non-2xx responses: 5000 Keep-Alive requests: 0 Total transferred: 1835000 bytes HTML transferred: 995000 bytes Requests per second: 4502.59 [#/sec] (mean) Time per request: 22.209 [ms] (mean) Time per request: 0.222 [ms] (mean, across all concurrent requests) Transfer rate: 1613.72 [Kbytes/sec] received
ab -n 10000 -c 100 -k 127.0.0.1/redis_iresty_long 很好
Concurrency Level: 100 Time taken for tests: 2.065 seconds Complete requests: 10000 Failed requests: 0 Non-2xx responses: 10000 Keep-Alive requests: 0 Total transferred: 3670000 bytes HTML transferred: 1990000 bytes Requests per second: 4843.59 [#/sec] (mean) Time per request: 20.646 [ms] (mean) Time per request: 0.206 [ms] (mean, across all concurrent requests) Transfer rate: 1735.94 [Kbytes/sec] received
ab -n 100000 -c 100 -k 127.0.0.1/redis_iresty_long 不是很好(不推荐)
Concurrency Level: 100 Time taken for tests: 19.675 seconds Complete requests: 100000 Failed requests: 0 Non-2xx responses: 100000 Keep-Alive requests: 0 Total transferred: 36700000 bytes HTML transferred: 19900000 bytes Requests per second: 5082.71 [#/sec] (mean) Time per request: 19.675 [ms] (mean) Time per request: 0.197 [ms] (mean, across all concurrent requests) Transfer rate: 1821.64 [Kbytes/sec] received
CPU 负载
ab -n 500000 -c 100 -k 127.0.0.1/redis_iresty_long 不是很好(不推荐)
Concurrency Level: 100 Time taken for tests: 95.891 seconds Complete requests: 500000 Failed requests: 0 Non-2xx responses: 500000 Keep-Alive requests: 0 Total transferred: 183500000 bytes HTML transferred: 99500000 bytes Requests per second: 5214.28 [#/sec] (mean) Time per request: 19.178 [ms] (mean) Time per request: 0.192 [ms] (mean, across all concurrent requests) Transfer rate: 1868.79 [Kbytes/sec] received
ab -n 1000000 -c 100 -k 127.0.0.1/redis_iresty_long (不推荐)下面不再测试
Concurrency Level: 100 Time taken for tests: 190.008 seconds Complete requests: 1000000 Failed requests: 0 Non-2xx responses: 1000000 Keep-Alive requests: 0 Total transferred: 367000000 bytes HTML transferred: 199000000 bytes Requests per second: 5262.94 [#/sec] (mean) Time per request: 19.001 [ms] (mean) Time per request: 0.190 [ms] (mean, across all concurrent requests) Transfer rate: 1886.23 [Kbytes/sec] received
CPU