nginx+apache+php 与 nginx+lua+redis 性能对比

系统环境

内存:16G

cpu:Intel® Core™ i3-4130 CPU @ 3.40GHz × 4

系统:ubuntu 14.04 lts 64bit 桌面版

nginx version: openresty/1.9.7.4

Apache/2.4.7 (Ubuntu)

PHP 5.5.9-1ubuntu4.9,启用opcache

LuaJIT 2.1.0-beta1

Redis server v=2.8.4

 

用同一个nginx,用worker_processes 为1,配置如下

server {
        listen       81;

        location /lua/lottery {
            default_type text/plain; 
            content_by_lua_file /www/lua/lottery.lua; 
            
        }

        location /php {
		    proxy_set_header Host $host;
            proxy_pass      http://127.0.0.1:8080/; # apache
        }

    }

 

 

简单输出

测试代码

lua ngx.say(‘hello world!’)

php echo ‘hello world!’;

 

apache+php

➜ ~ ab -c 500 -n 10000 http://127.0.0.1:8080/php/lottery.php

Concurrency Level: 500
Time taken for tests: 6.574 seconds
Complete requests: 10000
Failed requests: 0
Total transferred: 1990000 bytes
HTML transferred: 120000 bytes
Requests per second: 1521.09 [#/sec] (mean)
Time per request: 328.711 [ms] (mean)
Time per request: 0.657 [ms] (mean, across all concurrent requests)
Transfer rate: 295.60 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 13 112.0 0 1002
Processing: 2 168 850.5 23 6560
Waiting: 2 167 850.6 22 6560
Total: 9 182 872.8 23 6568

Percentage of the requests served within a certain time (ms)
50% 23
66% 25
75% 28
80% 31
90% 40
95% 47
98% 4938
99% 4990
100% 6568 (longest request)

 

nginx+apache+php

➜ ~ ab -c 500 -n 10000 http://127.0.0.1:81/php/lottery.php

Concurrency Level: 500
Time taken for tests: 3.456 seconds
Complete requests: 10000
Failed requests: 0
Total transferred: 1950000 bytes
HTML transferred: 120000 bytes
Requests per second: 2893.90 [#/sec] (mean)
Time per request: 172.777 [ms] (mean)
Time per request: 0.346 [ms] (mean, across all concurrent requests)
Transfer rate: 551.08 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 1 2.2 0 12
Processing: 7 109 315.4 26 3344
Waiting: 7 109 315.4 26 3344
Total: 14 110 316.3 26 3355

Percentage of the requests served within a certain time (ms)
50% 26
66% 28
75% 30
80% 32
90% 41
95% 1004
98% 1229
99% 1670
100% 3355 (longest request)

nginx+lua

➜ ~ ab -c 500 -n 10000 http://127.0.0.1:81/lua/lottery

Concurrency Level: 500
Time taken for tests: 0.402 seconds
Complete requests: 10000
Failed requests: 0
Total transferred: 1610000 bytes
HTML transferred: 130000 bytes
Requests per second: 24892.46 [#/sec] (mean)
Time per request: 20.086 [ms] (mean)
Time per request: 0.040 [ms] (mean, across all concurrent requests)
Transfer rate: 3913.76 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 2 5 1.0 5 12
Processing: 2 6 1.4 6 14
Waiting: 2 5 1.3 4 13
Total: 7 11 1.6 11 19

Percentage of the requests served within a certain time (ms)
50% 11
66% 11
75% 11
80% 12
90% 12
95% 16
98% 18
99% 18
100% 19 (longest request)

 

Redis 读取键值

键值内容对 aa: hi,redis!

测试脚本

lua

local redis = require "resty.redis"
local red =redis:new()
red:connect('127.0.0.1', '6379')
local res,err = red:get('aa')
ngx.say(res)

php
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
echo $$redis->get('aa');


nginx+apache+php

➜ ~ ab -c 500 -n 10000 http://127.0.0.1:81/php/lottery.php

Server Software: openresty/1.9.7.4
Server Hostname: 127.0.0.1
Server Port: 81

Document Path: /php/lottery.php
Document Length: 9 bytes

Concurrency Level: 500
Time taken for tests: 3.498 seconds
Complete requests: 10000
Failed requests: 0
Total transferred: 1910000 bytes
HTML transferred: 90000 bytes
Requests per second: 2859.16 [#/sec] (mean)
Time per request: 174.877 [ms] (mean)
Time per request: 0.350 [ms] (mean, across all concurrent requests)
Transfer rate: 533.30 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 34 177.4 1 1016
Processing: 3 78 186.0 37 3012
Waiting: 3 77 186.2 36 3012
Total: 5 113 253.4 39 3018

Percentage of the requests served within a certain time (ms)
50% 39
66% 45
75% 49
80% 52
90% 100
95% 1022
98% 1056
99% 1064
100% 3018 (longest request)

 

nginx+lua

➜ ~ ab -c 500 -n 10000 http://127.0.0.1:81/lua/lottery

Server Software: openresty/1.9.7.4
Server Hostname: 127.0.0.1
Server Port: 81

Document Path: /lua/lottery
Document Length: 10 bytes

Concurrency Level: 500
Time taken for tests: 1.144 seconds
Complete requests: 10000
Failed requests: 0
Total transferred: 1580000 bytes
HTML transferred: 100000 bytes
Requests per second: 8738.81 [#/sec] (mean)
Time per request: 57.216 [ms] (mean)
Time per request: 0.114 [ms] (mean, across all concurrent requests)
Transfer rate: 1348.37 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 1 2.1 0 13
Processing: 7 24 89.9 11 1014
Waiting: 7 24 89.9 11 1014
Total: 8 24 90.3 11 1014

Percentage of the requests served within a certain time (ms)
50% 11
66% 13
75% 13
80% 14
90% 16
95% 104
98% 114
99% 116
100% 1014 (longest request)

 

写入Redis列表

测试代码

php

$redis = new Redis();
$redis->connect(‘127.0.0.1’, 6379);
$redis->rPush(‘list_php’, ‘hi’);
echo ‘ok’;

lua

local redis = require “resty.redis”
local red =redis:new()
red:connect(‘127.0.0.1’, ‘6379’)
red:rpush(‘list_lua’, ‘hi’);
ngx.say(‘ok’)

 

nginx+apache+php

➜ ~ ab -c 500 -n 10000 http://127.0.0.1:81/php/lottery.php

 

Concurrency Level: 500
Time taken for tests: 4.036 seconds
Complete requests: 10000
Failed requests: 0
Total transferred: 1840000 bytes
HTML transferred: 20000 bytes
Requests per second: 2477.77 [#/sec] (mean)
Time per request: 201.794 [ms] (mean)
Time per request: 0.404 [ms] (mean, across all concurrent requests)
Transfer rate: 445.23 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 16 122.6 0 1000
Processing: 13 138 479.8 33 3316
Waiting: 13 138 479.9 33 3316
Total: 23 154 525.2 34 4022

Percentage of the requests served within a certain time (ms)
50% 34
66% 35
75% 37
80% 39
90% 52
95% 1004
98% 2823
99% 3306
100% 4022 (longest request)

 

nginx+lua

➜ ~ ab -c 500 -n 10000 http://127.0.0.1:81/lua/lottery

Concurrency Level: 500
Time taken for tests: 1.064 seconds
Complete requests: 10000
Failed requests: 0
Total transferred: 1500000 bytes
HTML transferred: 30000 bytes
Requests per second: 9398.94 [#/sec] (mean)
Time per request: 53.197 [ms] (mean)
Time per request: 0.106 [ms] (mean, across all concurrent requests)
Transfer rate: 1376.80 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 1.6 0 12
Processing: 6 13 7.7 12 418
Waiting: 6 13 7.7 12 418
Total: 7 13 8.5 12 418

Percentage of the requests served within a certain time (ms)
50% 12
66% 13
75% 13
80% 13
90% 15
95% 28
98% 35
99% 36
100% 418 (longest request)

 

测试结果

每秒处理请求数对比

nginx+apache+php nginx+lua
简单输出 1521.09(无nginx)/2893.90 24892.46
Redis 读取键值  2859.16 8738.81
写入Redis列表  2477.77  9398.94

说明

用apache自带ab测试10000次请求,500并发无失败。redis 写入全部成功。



分类 技术流

1条评论

发表评论

无觅相关文章插件,快速提升流量