openresty/1.11.2.1性能测试

 测试数据

ab -n 100000 -c 100 -k http://127.0.0.1/get_cache_value

nginx.conf

 lua_shared_dict cache_ngx 128m;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
        lua_code_cache on;
        location /get_cache_value {
            #root   html;
            content_by_lua_file /opt/openresty/nginx/conf/Lua/get_cache_value.lua;
        }
}

get_cache_value.lua

local json = require("cjson")
local redis = require("resty.redis")
local red = redis:new()

red:set_timeout(1000)

local ip = "127.0.0.1"
local port = 6379
local ok, err = red:connect(ip, port)
if not ok then
        ngx.say("connect to redis error : ", err)
        return ngx.exit(500)
end

-- set Cache cache_ngx
function set_to_cache(key,value,exptime)
    if not exptime then
        exptime = 0
    end
    local cache_ngx = ngx.shared.cache_ngx
    local succ, err, forcible = cache_ngx:set(key,value,exptime)
    return succ
end

--get Cache cache_ngx
function get_from_cache(key)
    local cache_ngx = ngx.shared.cache_ngx
    local value = cache_ngx:get(key)
    if not value then
        value = ngx.time()
        set_to_cache(key, value)
    end
    return value
end

function get_from_redis(key)
    local res, err = red:get("dog")
    if res then
        return res
    else
        return nil
    end
end
local res = get_from_cache('dog')
ngx.say(res)

一、默认配置AB压力测试

ab -n 100000 -c 100 -k http://127.0.0.1/

官方nginx/1.10.3 测试结果:

Server Software:        nginx/1.10.3
Server Hostname:        127.0.0.1
Server Port:            80

Document Path:          /
Document Length:        612 bytes

Concurrency Level:      100
Time taken for tests:   4.226 seconds    -- 表示所有这些请求被处理完成所花费的总时间
Complete requests:      100000
Failed requests:        0
Keep-Alive requests:    99004
Total transferred:      84995020 bytes
HTML transferred:       61200000 bytes
Requests per second:    23665.05 [#/sec] (mean)    -- 吞吐率,大家最关心的指标之一,相当于 LR 中的每秒事务数,后面括号中的 mean 表示这是一个平均值
Time per request:       4.226 [ms] (mean)        -- 用户平均请求等待时间,大家最关心的指标之二,相当于 LR 中的平均事务响应时间,后面括号中的 mean 表示这是一个平均值
Time per request:       0.042 [ms] (mean, across all concurrent requests)    --服务器平均请求处理时间,大家最关心的指标之三
Transfer rate:          19642.69 [Kbytes/sec] received

openresty/1.11.2.1测试结果:

Server Software:        openresty/1.11.2.1
Server Hostname:        127.0.0.1
Server Port:            80

Document Path:          /
Document Length:        558 bytes

Concurrency Level:      100
Time taken for tests:   1.158 seconds
Complete requests:      100000
Failed requests:        0
Keep-Alive requests:    99049
Total transferred:      80195245 bytes
HTML transferred:       55800000 bytes
Requests per second:    86321.79 [#/sec] (mean)
Time per request:       1.158 [ms] (mean)
Time per request:       0.012 [ms] (mean, across all concurrent requests)
Transfer rate:          67603.49 [Kbytes/sec] received

二、缓存测试(openresty/1.11.2.1):

ab -n 100000 -c 100 -k http://127.0.0.1/get_cache_value

1、lua_shared_dict cache_ngx 128m 缓存测试

Server Software:        openresty/1.11.2.1
Server Hostname:        127.0.0.1
Server Port:            80

Document Path:          /get_cache_value
Document Length:        11 bytes

Concurrency Level:      100
Time taken for tests:   87.087 seconds
Complete requests:      100000
Failed requests:        43539
   (Connect: 0, Receive: 0, Length: 43539, Exceptions: 0)
Keep-Alive requests:    99050
Total transferred:      19887244 bytes
HTML transferred:       3091994 bytes
Requests per second:    1148.27 [#/sec] (mean)
Time per request:       87.087 [ms] (mean)
Time per request:       0.871 [ms] (mean, across all concurrent requests)
Transfer rate:          223.01 [Kbytes/sec] received

2、Redis 缓存结果

Server Software:        openresty/1.11.2.1
Server Hostname:        127.0.0.1
Server Port:            80

Document Path:          /get_cache_value
Document Length:        20 bytes

Concurrency Level:      100
Time taken for tests:   74.190 seconds
Complete requests:      100000
Failed requests:        43538
   (Connect: 0, Receive: 0, Length: 43538, Exceptions: 0)
Keep-Alive requests:    99049
Total transferred:      20406151 bytes
HTML transferred:       3610906 bytes
Requests per second:    1347.89 [#/sec] (mean)
Time per request:       74.190 [ms] (mean)
Time per request:       0.742 [ms] (mean, across all concurrent requests)
Transfer rate:          268.61 [Kbytes/sec] received

 

 

===============================默认单个服务器和负载均衡服务器测试

CPU (cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c)8

8  Intel(R) Xeon(R) CPU E5-2609 v4 @ 1.70GHz

内存:(cat /proc/meminfo) 16GB

MemTotal:       16317068 kB 
MemFree:          176696 kB
Buffers:          149680 kB
Cached:         15087652 kB

ab 服务器,阿里云云主机:ab -n 100000 -c 100 http://127.7.7.7:8081/

默认单个服务器

ocument Path:          /
Document Length:        558 bytes

Concurrency Level:      100
Time taken for tests:   14.389 seconds
Complete requests:      100000
Failed requests:        0
Total transferred:      79700000 bytes
HTML transferred:       55800000 bytes
Requests per second:    6949.80 [#/sec] (mean)
Time per request:       14.389 [ms] (mean)
Time per request:       0.144 [ms] (mean, across all concurrent requests)
Transfer rate:          5409.17 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        1   10  91.0      2    3005
Processing:     1    4  20.5      2     412
Waiting:        1    4  20.5      2     412
Total:          3   14  93.3      4    3007

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

负载均衡:

Document Path:          /
Document Length:        557 bytes

Concurrency Level:      100
Time taken for tests:   13.720 seconds
Complete requests:      100000
Failed requests:        33335
   (Connect: 0, Receive: 0, Length: 33335, Exceptions: 0)
Total transferred:      81133335 bytes
HTML transferred:       55733335 bytes
Requests per second:    7288.44 [#/sec] (mean)
Time per request:       13.720 [ms] (mean)
Time per request:       0.137 [ms] (mean, across all concurrent requests)
Transfer rate:          5774.76 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        1    9  86.5      2    3004
Processing:     2    4  19.0      2     410
Waiting:        1    4  19.0      2     410
Total:          3   13  88.6      4    3208

Percentage of the requests served within a certain time (ms)
  50%      4
  66%      4
  75%      5
  80%      5
  90%      5
  95%      5
  98%      7
  99%    207
 100%   3208 (longest request)

m3u8 文件

Document Path:          /live/tinywan123.m3u8
Document Length:        178 bytes

Concurrency Level:      100
Time taken for tests:   13.345 seconds
Complete requests:      100000
Failed requests:        0
Total transferred:      59100000 bytes
HTML transferred:       17800000 bytes
Requests per second:    7493.47 [#/sec] (mean)
Time per request:       13.345 [ms] (mean)
Time per request:       0.133 [ms] (mean, across all concurrent requests)
Transfer rate:          4324.84 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        1    9  83.4      2    3007
Processing:     2    4  19.2      2     410
Waiting:        2    4  19.2      2     410
Total:          3   13  85.7      4    3010

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

 

Openresty提供了lua-resty-limit-traffic模块进行限流,模块实现了limit.connlimit.req的功能和算法

local limit_req = require "resty.limit.req"
local rate = 2 --固定平均速率2r/s
local burst = 10 --桶容量
local error_status = 503
local nodelay = false --是否需要不延迟处理
--ngx.say('1111111111111111')

-- my_limit_req_store
local lim, err = limit_req.new("my_limit_req_store", rate, burst)
if not lim then --申请limit_req对象失败
    ngx.log(ngx.ERR,
            "failed to instantiate a resty.limit.req object: ", err)
    return ngx.exit(500)
end

--ngx.say("local lim")
local key = ngx.var.binary_remote_addr
local delay, err = lim:incoming(key, true)

if not delay then
    if err == "rejected" then
        return ngx.exit(503)
    end
    ngx.log(ngx.ERR, "failed to limit req: ", err)
    return ngx.exit(500)
end

--ngx.log(ngx.ERR, "failed to limit req_test: ")
if delay > 0 then
    -- 第二个参数(err)保存着超过请求速率的请求数
    -- 例如err等于31,意味着当前速率是231 req/sec
    local excess = err

    -- 当前请求超过200 req/sec 但小于 300 req/sec
    -- 因此我们sleep一下,保证速率是200 req/sec,请求延迟处理
    ngx.sleep(delay) --非阻塞sleep(秒)
end

 

 

 

 

 

apr_socket_recv: Connection reset by peer (104) 

详解地址:http://www.cnblogs.com/archoncap/p/5883723.html

 

posted @ 2017-04-22 18:25  Tinywan  阅读(1374)  评论(0编辑  收藏  举报