PHP7+Swoole比Nginx/Golang性能高75%

来自PHP百科全书
跳转至: 导航搜索

性能对比

使用apache bench工具对Nginx静态页、Golang Http程序、PHP7+Swoole Http程序进行压力测试。在并发100进行100万次Http请求的基准测试中,PHP7+Swoole比Nginx/Golang性能高75%,QPS对比如下:

软件 QPS
Nginx 164489.92
Golang 166838.68
PHP7+Swoole 287104.12
Nginx-1.9.9 245058.70

注:Nginx升级到最新的1.9.9,关闭access_log,启用open_file_cache缓存静态文件到内存,最新测试达到了245058.70qps

测试环境

硬件配置

  • CPU:Intel® Core™ i5-4590 CPU @ 3.30GHz × 4
  • 内存:16G
  • 磁盘:128G SSD
  • 操作系统:Ubuntu14.04 (Linux 3.16.0-55-generic)

压测工具

 ab -c 100 -n 1000000 -k http://127.0.0.1:8080/

软件信息

Nginx

版本

nginx/1.4.6 (Ubuntu)

VHOST配置

server {
    listen 80 default_server;
    root /data/webroot;
    index index.html;
}

测试页面

<h1>Hello World!</h1>

进程数量

Nginx开启了4个Worker进程

htf@htf-All-Series:~/soft/php-7.0.0$ ps aux|grep nginx
root      1221  0.0  0.0  86300  3304 ?        Ss   12月07   0:00 nginx: master process /usr/sbin/nginx
www-data  1222  0.0  0.0  87316  5440 ?        S    12月07   0:44 nginx: worker process
www-data  1223  0.0  0.0  87184  5388 ?        S    12月07   0:36 nginx: worker process
www-data  1224  0.0  0.0  87000  5520 ?        S    12月07   0:40 nginx: worker process
www-data  1225  0.0  0.0  87524  5516 ?        S    12月07   0:45 nginx: worker process

Golang

版本

 go version go1.5.2 linux/amd64

测试代码

package main

import (
    "log"
    "net/http"
    "runtime"
)

func main() {
    runtime.GOMAXPROCS(runtime.NumCPU() - 1)

    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        w.Header().Add("Last-Modified", "Thu, 18 Jun 2015 10:24:27 GMT")
        w.Header().Add("Accept-Ranges", "bytes")
        w.Header().Add("E-Tag", "55829c5b-17")
        w.Header().Add("Server", "golang-http-server")
        w.Write([]byte("<h1>\nHello world!\n</h1>\n"))
    })

    log.Printf("Go http Server listen on :8080")
    log.Fatal(http.ListenAndServe(":8080", nil))
}

PHP7+Swoole

PHP7已启用OpCache加速器。

PHP版本

htf@htf-All-Series:~/soft/php-7.0.0$ php -v
PHP 7.0.0 (cli) (built: Dec 10 2015 14:36:26) ( NTS )
Copyright (c) 1997-2015 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2015 Zend Technologies
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies

Swoole版本

 swoole-1.7.22-alpha

PHP扩展

[PHP Modules]
Core
ctype
curl
date
dom
fileinfo
filter
gd
hash
iconv
json
libxml
mbstring
mcrypt
mysqli
mysqlnd
pcntl
pcre
PDO
pdo_sqlite
Phar
posix
Reflection
session
SimpleXML
sockets
SPL
sqlite3
standard
swoole
tokenizer
xml
xmlreader
xmlwriter
Zend OPcache
zlib

[Zend Modules]
Zend OPcache

测试代码

<?php
$http = new swoole_http_server("127.0.0.1", 9501, SWOOLE_BASE);

$http->set([
    'worker_num' => 4,
]);

$http->on('request', function ($request, swoole_http_response $response) {
    $response->header('Last-Modified', 'Thu, 18 Jun 2015 10:24:27 GMT');
    $response->header('E-Tag', '55829c5b-17');
    $response->header('Accept-Ranges', 'bytes');    
    $response->end("<h1>\nHello Swoole.\n</h1>");
});

$http->start();

测试结果

详细的测试结果输出如下:

Nginx

htf@htf-All-Series:~/workspace/swoole/examples/bench$ ab -c 100 -n 1000000 -k http://127.0.0.1/
This is ApacheBench, Version 2.3 <$Revision: 1528965 $>
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 100000 requests
Completed 200000 requests
Completed 300000 requests
Completed 400000 requests
Completed 500000 requests
Completed 600000 requests
Completed 700000 requests
Completed 800000 requests
Completed 900000 requests
Completed 1000000 requests
Finished 1000000 requests


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

Document Path:          /
Document Length:        23 bytes

Concurrency Level:      100
Time taken for tests:   6.079 seconds
Complete requests:      1000000
Failed requests:        0
Keep-Alive requests:    990048
Total transferred:      266950240 bytes
HTML transferred:       23000000 bytes
Requests per second:    164489.92 [#/sec] (mean)
Time per request:       0.608 [ms] (mean)
Time per request:       0.006 [ms] (mean, across all concurrent requests)
Transfer rate:          42881.47 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       1
Processing:     0    1   1.1      0      22
Waiting:        0    1   1.1      0      22
Total:          0    1   1.1      0      22

Percentage of the requests served within a certain time (ms)
  50%      0
  66%      1
  75%      1
  80%      1
  90%      1
  95%      1
  98%      2
  99%      5
 100%     22 (longest request)

Golang

htf@htf-All-Series:~/workspace/swoole/examples/bench$ ab -c 100 -n 1000000 -k http://127.0.0.1:8080/
This is ApacheBench, Version 2.3 <$Revision: 1528965 $>
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 100000 requests
Completed 200000 requests
Completed 300000 requests
Completed 400000 requests
Completed 500000 requests
Completed 600000 requests
Completed 700000 requests
Completed 800000 requests
Completed 900000 requests
Completed 1000000 requests
Finished 1000000 requests


Server Software:        golang-http-server
Server Hostname:        127.0.0.1
Server Port:            8080

Document Path:          /
Document Length:        24 bytes

Concurrency Level:      100
Time taken for tests:   5.994 seconds
Complete requests:      1000000
Failed requests:        0
Keep-Alive requests:    1000000
Total transferred:      280000000 bytes
HTML transferred:       24000000 bytes
Requests per second:    166838.68 [#/sec] (mean)
Time per request:       0.599 [ms] (mean)
Time per request:       0.006 [ms] (mean, across all concurrent requests)
Transfer rate:          45619.95 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       1
Processing:     0    1   0.4      1       9
Waiting:        0    1   0.4      1       9
Total:          0    1   0.4      1       9

Percentage of the requests served within a certain time (ms)
  50%      1
  66%      1
  75%      1
  80%      1
  90%      1
  95%      1
  98%      2
  99%      2
 100%      9 (longest request)

PHP7+Swoole

htf@htf-All-Series:~/workspace/swoole/examples/bench$ ab -c 100 -n 1000000 -k http://127.0.0.1:9501/
This is ApacheBench, Version 2.3 <$Revision: 1528965 $>
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 100000 requests
Completed 200000 requests
Completed 300000 requests
Completed 400000 requests
Completed 500000 requests
Completed 600000 requests
Completed 700000 requests
Completed 800000 requests
Completed 900000 requests
Completed 1000000 requests
Finished 1000000 requests


Server Software:        swoole-http-server
Server Hostname:        127.0.0.1
Server Port:            9501

Document Path:          /
Document Length:        24 bytes

Concurrency Level:      100
Time taken for tests:   3.483 seconds
Complete requests:      1000000
Failed requests:        0
Keep-Alive requests:    1000000
Total transferred:      265000000 bytes
HTML transferred:       24000000 bytes
Requests per second:    287104.12 [#/sec] (mean)
Time per request:       0.348 [ms] (mean)
Time per request:       0.003 [ms] (mean, across all concurrent requests)
Transfer rate:          74299.40 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       5
Processing:     0    0   0.2      0      16
Waiting:        0    0   0.2      0      16
Total:          0    0   0.2      0      16

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

资源消耗

Nginx

sar

16时31分05秒     all      0.00      0.00      0.00      0.00      0.00    100.00
16时31分06秒     all     11.65      0.00     29.11      0.00      0.00     59.24
16时31分07秒     all     31.58      0.00     64.16      0.00      0.00      4.26
16时31分08秒     all     29.38      0.00     66.17      0.00      0.00      4.44
16时31分09秒     all     31.58      0.00     63.91      0.00      0.00      4.51
16时31分10秒     all     27.00      0.00     66.75      0.00      0.00      6.25
16时31分11秒     all     28.68      0.00     67.01      0.00      0.00      4.31
16时31分12秒     all     26.30      0.00     28.29      0.00      0.00     45.41
16时31分13秒     all      0.25      0.00      0.00      0.00      0.00     99.75

vmstat

htf@htf-All-Series:~/workspace/swoole/examples/bench$ vmstat -w 1 10000
procs ---------------memory-------------- ---swap-- -----io---- -system-- ------cpu-----
 r  b     swpd     free     buff    cache   si   so    bi    bo   in   cs us sy id wa st
 0  0        0  3812080   559240  9321032    0    0     4    27    2   25  2  3 95  0  0
 0  0        0  3811672   559240  9321644    0    0     0     0  289 1176  3  1 97  0  0
 0  0        0  3803612   559240  9329452    0    0     0    40  261 1204  2  1 97  0  0
 0  0        0  3803900   559240  9329488    0    0     0     0  285  870  2  1 98  0  0
 0  0        0  3802896   559240  9329488    0    0     0     0  242  802  2  0 98  0  0
 7  0        0  3784512   559240  9339224    0    0     0     0 1021 98609 21 44 35  0  0
 5  0        0  3764016   559240  9354012    0    0     0     0 1288 84593 30 65  5  0  0
 5  0        0  3741588   559240  9368512    0    0     0     0 1386 77834 30 65  5  0  0
 5  0        0  3720552   559240  9383812    0    0     0     0 1316 76846 30 65  5  0  0
 5  0        0  3714152   559240  9398800    0    0     0     0 1319 72068 30 66  5  0  0
 1  0        0  3692460   559240  9413572    0    0     0   104 1501 75930 32 63  5  0  0
 0  0        0  3721076   559240  9416880    0    0     0     0  532 8329 18 12 70  0  0

Golang

sar

16时42分02秒     all      2.72      0.00      0.99      0.00      0.00     96.29
16时42分03秒     all     30.18      0.00     24.55      0.00      0.00     45.27
16时42分04秒     all     55.33      0.00     37.31      0.00      0.00      7.36
16时42分05秒     all     54.31      0.00     35.79      0.00      0.00      9.90
16时42分06秒     all     54.80      0.00     36.36      0.00      0.00      8.84
16时42分07秒     all     54.96      0.00     35.62      0.00      0.00      9.41
16时42分08秒     all     55.98      0.00     36.13      0.00      0.00      7.89
16时42分09秒     all     39.20      0.00     19.85      0.00      0.00     40.95
16时42分10秒     all      2.47      0.00      1.23      0.00      0.00     96.30
16时42分11秒     all      1.24      0.00      0.50      0.00      0.00     98.26

vmstat

htf@htf-All-Series:~/workspace/swoole/examples/bench$ vmstat -w 1 10000
procs ---------------memory-------------- ---swap-- -----io---- -system-- ------cpu-----
 r  b     swpd     free     buff    cache   si   so    bi    bo   in   cs us sy id wa st
 0  0        0  3667384   559244  9402656    0    0     4    27    2   27  2  3 95  0  0
 0  0        0  3667296   559244  9402760    0    0     0     0  266  886  3  1 96  0  0
 4  0        0  3663588   559244  9402668    0    0     0     0  880 34829 18 11 71  0  0
 4  0        0  3672008   559244  9402680    0    0     0     0 2206 121758 55 36  8  0  0
 5  0        0  3664720   559244  9402680    0    0     0     0 2397 119907 53 38  9  0  0
 3  0        0  3661864   559244  9402616    0    0     0     0 1754 121168 54 36  9  0  0
 4  0        0  3655944   559244  9402616    0    0     0     0 1907 121098 53 39  8  0  0
 4  0        0  3651896   559244  9402616    0    0     0     0 1739 122829 57 34  9  0  0
 1  0        0  3632884   559244  9402616    0    0     0    12 1554 95129 50 26 24  0  0
 0  0        0  3680560   559244  9402616    0    0     0     0  298  954  8  1 92  0  0

PHP7+Swoole

sar

16时44分49秒     CPU     %user     %nice   %system   %iowait    %steal     %idle
16时44分50秒     all      1.97      0.00      0.99      0.00      0.00     97.04
16时44分51秒     all      1.25      0.00      0.25      0.00      0.00     98.50
16时44分52秒     all      1.00      0.00      0.25      0.00      0.00     98.75
16时44分53秒     all      3.25      0.00      4.00      0.25      0.00     92.50
16时44分54秒     all     43.72      0.00     45.73      0.00      0.00     10.55
16时44分55秒     all     45.27      0.00     45.27      0.00      0.00      9.45
16时44分56秒     all     45.00      0.00     45.00      0.00      0.00     10.00
16时44分57秒     all     38.42      0.00     28.08      0.00      0.00     33.50
16时44分58秒     all      3.26      0.00      0.00      0.00      0.00     96.74
16时44分59秒     all      0.74      0.00      0.99      0.00      0.00     98.26

vmstat

htf@htf-All-Series:~/workspace/swoole/examples/bench$ vmstat -w 1 10000
procs ---------------memory-------------- ---swap-- -----io---- -system-- ------cpu-----
 r  b     swpd     free     buff    cache   si   so    bi    bo   in   cs us sy id wa st
 0  0        0  3660916   559244  9416532    0    0     4    27    2   28  2  3 95  0  0
 0  0        0  3660528   559244  9416868    0    0     0    12  278 1115  2  1 96  0  0
 1  0        0  3653212   559244  9416932    0    0     0     0 1352 15161 33 33 34  0  0
 1  0        0  3642976   559244  9416868    0    0     0     0 1598 22613 49 45  6  0  0
 1  0        0  3634752   559244  9416868    0    0     0    28 1676 23568 47 47  6  0  0
 1  0        0  3607328   559244  9417000    0    0     0     0 1374 15695 40 35 25  0  0
 0  0        0  3660644   559244  9417000    0    0     0   148  301  945  6  1 93  0  0
 0  0        0  3660776   559244  9417000    0    0     0     0  247 1051  1  1 98  0  0
 1  0        0  3660776   559244  9416936    0    0     0     0  259  818  1  2 97  0  0