背景

最近用 Go 重写了之前用 PHP(Laravel) 写的一个较复杂的接口, 顺便作了个简单的性能测试对比。
目前网上很多性能对比只是简单的作一个 hello world 的对比, 基于实际业务的对比较少,所以记录一下这次对比结果。

HUGOMORE42

业务场景

这是多一个类似于获取商品详情的接口, 面向的用户是商店工作人员。多商家,每个商家可自定义自己的商品字段。
服务端收到请求大约做了这几件工作:

业务场景

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
@startuml
skinparam defaultFontName "pingFang SC"
title 业务场景
== 请求 ==
User -> WebService: 请求
note right: 请求中带 JWT 信息 JWT 中只包含用户 uuid、商家名称)
== 用户身份 ==
WebService -> MySQL:查询用户信息
MySQL --> WebService: 返回数据
WebService -> MySQL: 查询商家信息
note left: 商家基本信息、配额信息、商品自定义字段信息
MySQL --> WebService: 返回数据
== 商品信息 ==
WebService -> MySQL: 查询商品数据
MySQL --> WebService: 返回数据
... 组装商品数据 ...
== 输出 ==
WebService --> User: 输出JSON
@enduml

先表明一下观点, 在一个项目运作过程中(特别是在初期)性能并不是首先要考虑的因素 ,所有读取数据操作均 未使用缓存 (初步估算用户增长规模 项目上线第一年基本不会出现性能瓶颈, 在最经济的人力、物力资源下快速推出新功能才是最重要的 😏 )。

测试环境

硬件: 阿里云 ECS 2核 4G内存 (购于2014年2月,普通云盘)
操作系统: CentOS 7.3 64位
测试工具: apache ab

为减小因带宽因素影响,测试使用内网网络(Go直接使用内网IP、PHP更改 hosts 文件, 请求从局域网内另一台 ECS 服务器发起);
机器上同时运行有其它服务。

项目 PHP Go
版本 PHP 7.1 Go 1.8.3
Web 框架 Laravel 5.5 Gin 1.2
ORM Laravel Eloquent XORM
JWT 中间件 tymon/jwt-auth dgrijalva/jwt-go

PHP 相关的其它重要参数:

nginx:
1.13.3 worker_processes=2, worker_connections=1024
php-fpm:
pm.max_children=20, pm.max_requests=2000
Laravel Cache:
php artisan route:cache
php artisan config:cache

测试数据

PHP: .php -c 600 -t 10

先直接直接输出 helloworld.php 以反映机器大致性能情况。

1
2
3
4
5
<?php

// public/helloworld.php

echo 'hello,world!';

ab -c 500 -t 10:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Server Software:        nginx/1.13.3
Server Hostname:        api.***.com
Server Port:            80

Document Path:          /helloworld.php
Document Length:        12 bytes

Concurrency Level:      500
Time taken for tests:   10.001 seconds
Complete requests:      11961
Failed requests:        0
Write errors:           0
Total transferred:      2081214 bytes
HTML transferred:       143532 bytes
Requests per second:    1195.95 [#/sec] (mean)
Time per request:       418.078 [ms] (mean)
Time per request:       0.836 [ms] (mean, across all concurrent requests)
Transfer rate:          203.22 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        1  145 508.1      2    7017
Processing:    14  229 325.1    118    6493
Waiting:        3  229 325.0    118    6493
Total:         41  375 621.6    123    7497

Percentage of the requests served within a certain time (ms)
  50%    123
  66%    181
  75%    310
  80%    347
  90%   1124
  95%   1323
  98%   3109
  99%   3172
 100%   7497 (longest request)

vmstat:

1
2
3
4
5
6
7
8
9
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0      0 181028 553948 1370796    0    0     0     5    1    1  2  1 97  0  1
23  0      0 194260 553948 1374792    0    0     0     0 6025 2311  7 29 56  0  9
20  0      0 182940 553948 1386020    0    0     0     0 19241 6104 17 79  0  0  4
17  0      0 172396 553948 1396988    0    0     0   378 17796 5474 17 74  1  0  9
17  0      0 164180 553952 1407020    0    0     0   356 16665 5237 15 71  0  0 14
24  0      0 153556 553952 1417892    0    0     0    16 18515 5963 16 75  1  0  8
 0  0      0 146764 553952 1425468    0    0     0     0 13192 4378 10 51 37  0  2

HeyLaravel -c 100 -t 10

直接访问 Laravel 中的测试控制器方法:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class TestController extends Controller
{

    public function hey($uuid)
    {
        return ['uuid'=> $uuid];
    }
}

ab:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Server Software:        nginx/1.13.3
Server Hostname:        api.***.com
Server Port:            80

Document Path:          /v1/test/hey/this-is-uuid
Document Length:        23 bytes

Concurrency Level:      100
Time taken for tests:   10.021 seconds
Complete requests:      811
Failed requests:        0
Write errors:           0
Total transferred:      344675 bytes
HTML transferred:       18653 bytes
Requests per second:    80.93 [#/sec] (mean)
Time per request:       1235.639 [ms] (mean)
Time per request:       12.356 [ms] (mean, across all concurrent requests)
Transfer rate:          33.59 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        1   16 110.5      2    1004
Processing:    90 1141 260.9   1147    2701
Waiting:       84 1141 260.9   1147    2701
Total:         91 1157 287.0   1152    2969

Percentage of the requests served within a certain time (ms)
  50%   1152
  66%   1219
  75%   1271
  80%   1306
  90%   1424
  95%   1553
  98%   1922
  99%   2086
 100%   2969 (longest request)

vmstat:

1
2
3
4
5
6
7
8
9
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0      0 322900 553984 1373104    0    0     0     5    1    1  2  1 97  0  1
43  0      0 311596 553984 1373356    0    0     0     0 5795 2503 51 15 28  0  6
43  0      0 306108 553984 1373904    0    0     0     8 8339 3261 72 19  0  0  9
43  0      0 304828 553988 1375144    0    0     0     6 7692 3067 68 16  0  0 16
40  0      0 303064 553988 1376660    0    0     0     8 8069 3223 71 16  0  0 13
45  0      0 301652 553988 1378288    0    0     0     0 8974 3509 75 18  0  0  7
 0  0      0 300816 553992 1379176    0    0     0     6 6699 2756 56 12 31  0  0

HeyLaravel -c 200 -t 10

ab:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Server Software:        nginx/1.13.3
Server Hostname:        api.***.com
Server Port:            80

Document Path:          /v1/test/hey/this-is-uuid
Document Length:        23 bytes

Concurrency Level:      200
Time taken for tests:   10.023 seconds
Complete requests:      930
Failed requests:        0
Write errors:           0
Total transferred:      395250 bytes
HTML transferred:       21390 bytes
Requests per second:    92.79 [#/sec] (mean)
Time per request:       2155.480 [ms] (mean)
Time per request:       10.777 [ms] (mean, across all concurrent requests)
Transfer rate:          38.51 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        1   29 155.4      2    1006
Processing:    57 1821 776.3   1719    5206
Waiting:       44 1821 776.3   1719    5206
Total:         59 1850 792.3   1730    5208

Percentage of the requests served within a certain time (ms)
  50%   1730
  66%   1816
  75%   1926
  80%   2322
  90%   2824
  95%   3159
  98%   4665
  99%   4832
 100%   5208 (longest request)

vmstat:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
1  0      0 300628 553992 1378960    0    0     0     5    1    1  2  1 97  0  1
46  0      0 299852 553992 1379600    0    0     0     6 2301 1335 18  6 76  0  1
37  0      0 298844 553992 1380180    0    0     0     0 9489 3495 82 19  0  0  0
45  0      0 297448 553992 1380952    0    0     0     8 8940 3489 74 17  0  0  9
42  0      0 296320 554000 1382216    0    0     0    34 9076 3511 78 19  0  0  4
43  0      0 294276 554000 1384288    0    0     0     0 10212 3838 81 19  0  0  0
45  0      0 294056 554000 1385776    0    0     0    20 9091 3390 75 20  0  0  6
0  0      0 293792 554000 1386104    0    0     0     0 5031 2435 45 10 42  0  3

HeyLaravel -c 500 -t 10

ab:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Server Software:        nginx/1.13.3
Server Hostname:        api.***.com
Server Port:            80

Document Path:          /v1/test/hey/this-is-uuid
Document Length:        23 bytes

Concurrency Level:      500
Time taken for tests:   10.010 seconds
Complete requests:      920
Failed requests:        0
Write errors:           0
Total transferred:      391000 bytes
HTML transferred:       21160 bytes
Requests per second:    91.91 [#/sec] (mean)
Time per request:       5440.211 [ms] (mean)
Time per request:       10.880 [ms] (mean, across all concurrent requests)
Transfer rate:          38.15 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        1   10  13.8      2      45
Processing:    86 2191 1498.3   1768    9628
Waiting:       47 2191 1498.4   1767    9628
Total:         87 2201 1499.2   1771    9649

Percentage of the requests served within a certain time (ms)
  50%   1771
  66%   1978
  75%   2485
  80%   2710
  90%   3302
  95%   5093
  98%   8661
  99%   9151
 100%   9649 (longest request)

PHP: ab -c 100 -t 10

ab:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Server Software:        nginx/1.13.3
Server Hostname:        api.***.com
Server Port:            80

Document Path:          /v1/cars/2d11a75ba3e84e31baaf1a998159328c
Document Length:        597 bytes

Concurrency Level:      100
Time taken for tests:   10.018 seconds
Complete requests:      714
Failed requests:        0
Write errors:           0
Total transferred:      713286 bytes
HTML transferred:       426258 bytes
Requests per second:    71.27 [#/sec] (mean)
Time per request:       1403.092 [ms] (mean)
Time per request:       14.031 [ms] (mean, across all concurrent requests)
Transfer rate:          69.53 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        1    2   1.7      2      11
Processing:    70 1294 316.5   1308    2469
Waiting:       63 1294 316.6   1308    2469
Total:         72 1297 315.5   1309    2471

Percentage of the requests served within a certain time (ms)
  50%   1309
  66%   1414
  75%   1482
  80%   1550
  90%   1674
  95%   1750
  98%   1857
  99%   1951
 100%   2471 (longest request)

vmstat:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
1  0      0 314660 551540 1366952    0    0     0     5    1    1  2  1 97  0  1
45  0      0 314568 551540 1366920    0    0     0     2 3827 1753 30  8 61  0  1
44  0      0 314016 551540 1367408    0    0     0     8 10766 3849 78 19  0  0  3
42  0      0 313388 551540 1368320    0    0     0     0 10276 3614 80 20  0  0  0
45  0      0 312020 551544 1369364    0    0     0     6 9566 3425 73 19  0  0  7
40  0      0 310864 551544 1370708    0    0     0     8 10025 3714 76 20  0  0  4
42  0      0 309544 551544 1371792    0    0     0     0 9700 3452 72 17  0  0 11
1  0      0 309764 551552 1371968    0    0     0    70 2962 1640 21  5 71  0  2
0  0      0 309700 551552 1371968    0    0     0     0  652  974  3  1 96  0  1
0  0      0 309732 551560 1371972    0    0     2    56  511  800  2  0 96  0  1
0  0      0 309724 551564 1371976    0    0     0    54  536  811  2  1 96  1  1
0  0      0 309724 551564 1371980    0    0     0     0  744  956

Go: ab -c 100 -t 10

ab:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Server Software:
Server Hostname:        10.*.*.*
Server Port:            9999

Document Path:          /cars/2d11a75ba3e84e31baaf1a998159328c
Document Length:        512 bytes

Concurrency Level:      100
Time taken for tests:   10.003 seconds
Complete requests:      10253
Failed requests:        0
Write errors:           0
Total transferred:      6520908 bytes
HTML transferred:       5249536 bytes
Requests per second:    1024.95 [#/sec] (mean)
Time per request:       97.566 [ms] (mean)
Time per request:       0.976 [ms] (mean, across all concurrent requests)
Transfer rate:          636.59 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        1    2   2.9      1      62
Processing:    16   95  21.1     94     205
Waiting:        9   94  21.0     93     205
Total:         17   97  21.3     96     206

Percentage of the requests served within a certain time (ms)
  50%     96
  66%    104
  75%    109
  80%    113
  90%    123
  95%    133
  98%    146
  99%    160
 100%    206 (longest request)

vmstat:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0      0 310256 551576 1366976    0    0     0     5    1    1  2  1 97  0  1
 4  0      0 308348 551576 1367184    0    0     0     6 18804 1492 44 25 29  0  2
 2  0      0 306916 551576 1368336    0    0     0     0 25924 1373 64 33  0  0  2
 2  0      0 305028 551576 1369760    0    0     0     6 28024 1273 65 32  1  0  1
 6  0      0 304132 551576 1370884    0    0     0     0 27345 1453 65 33  1  0  1
 2  0      0 302912 551576 1371872    0    0     0     0 26417 1547 64 33  1  0  2
 0  0      0 303212 551576 1372100    0    0     0     6 6186  995 18  8 67  0  7
 0  0      0 303212 551576 1372100    0    0     0     0  532  799  2  0 97  0  0

PHP: ab -c 200 -t 10

ab:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Server Software:        nginx/1.13.3
Server Hostname:        api.***.com
Server Port:            80

Document Path:          /v1/cars/2d11a75ba3e84e31baaf1a998159328c
Document Length:        597 bytes

Concurrency Level:      200
Time taken for tests:   10.010 seconds
Complete requests:      650
Failed requests:        0
Write errors:           0
Total transferred:      649350 bytes
HTML transferred:       388050 bytes
Requests per second:    64.94 [#/sec] (mean)
Time per request:       3079.852 [ms] (mean)
Time per request:       15.399 [ms] (mean, across all concurrent requests)
Transfer rate:          63.35 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        1    5   4.9      2      24
Processing:    78 2415 930.8   2371    6184
Waiting:       63 2415 930.9   2371    6184
Total:         79 2420 928.1   2373    6186

Percentage of the requests served within a certain time (ms)
  50%   2373
  66%   2564
  75%   2703
  80%   2946
  90%   3555
  95%   3883
  98%   5077
  99%   5582
 100%   6186 (longest request)

vmstat:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
2  0      0 292928 553836 1375152    0    0     0     5    1    1  2  1 97  0  1
41  0      0 291944 553836 1375872    0    0     0     6 2716 1432 20  7 73  0  1
43  0      0 290984 553836 1376396    0    0     0     0 9870 3417 81 20  0  0  0
46  0      0 290188 553836 1376808    0    0     0     8 10438 3634 77 19  0  0  3
41  0      0 289624 553840 1377208    0    0     0     6 10064 3596 79 19  0  0  1
39  0      0 288948 553840 1377832    0    0     0     0 7952 3025 65 16  0  0 19
37  0      0 289720 553840 1378656    0    0     0     8 9836 3471 76 20  0  0  5
0  0      0 289720 553844 1378824    0    0     0     6 7470 2719 58 13 29  0  1
0  0      0 289520 553844 1378824    0    0     0    36  608  907  2  1 96  0  0

Go: ab -c 200 -t 10

ab:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Server Software:
Server Hostname:        10.*.*.*
Server Port:            9999

Document Path:          /cars/2d11a75ba3e84e31baaf1a998159328c
Document Length:        512 bytes

Concurrency Level:      200
Time taken for tests:   10.014 seconds
Complete requests:      9665
Failed requests:        0
Write errors:           0
Total transferred:      6146940 bytes
HTML transferred:       4948480 bytes
Requests per second:    965.19 [#/sec] (mean)
Time per request:       207.213 [ms] (mean)
Time per request:       1.036 [ms] (mean, across all concurrent requests)
Transfer rate:          599.47 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        1    3   5.1      1      63
Processing:    25  202  47.0    195     481
Waiting:        9  201  46.9    195     481
Total:         26  204  47.7    197     483

Percentage of the requests served within a certain time (ms)
  50%    197
  66%    215
  75%    227
  80%    236
  90%    262
  95%    294
  98%    334
  99%    364
 100%    483 (longest request)

vmstat:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
10  0      0 291748 553844 1376828    0    0     0     5    1    1  2  1 97  0  1
 3  0      0 285760 553844 1377052    0    0     0     6 17493 1460 40 24 35  0  1
 6  0      0 284888 553844 1377836    0    0     0     0 27816 1635 65 34  1  0  1
 5  0      0 283936 553844 1378744    0    0     0     6 25772 1546 63 31  0  0  6
 6  0      0 283092 553844 1379264    0    0     0     0 24097 1450 61 29  0  0 10
 7  0      0 282000 553844 1379904    0    0     0     0 26060 1667 61 31  1  0  6
 1  0      0 283292 553844 1379756    0    0     0     6 10354 1181 25 14 55  0  5
 0  0      0 283300 553844 1379748    0    0     0     0  619  926  2  0 97  0  1

PHP ab -c 500 -t 10

ab:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Server Software:        nginx/1.13.3
Server Hostname:        api.***.com
Server Port:            80

Document Path:          /v1/cars/2d11a75ba3e84e31baaf1a998159328c
Document Length:        597 bytes

Concurrency Level:      500
Time taken for tests:   10.013 seconds
Complete requests:      662
Failed requests:        0
Write errors:           0
Total transferred:      661338 bytes
HTML transferred:       395214 bytes
Requests per second:    66.12 [#/sec] (mean)
Time per request:       7562.409 [ms] (mean)
Time per request:       15.125 [ms] (mean, across all concurrent requests)
Transfer rate:          64.50 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        1   49 189.9      2    1005
Processing:   100 2778 1463.9   2418    9821
Waiting:       66 2778 1464.0   2418    9821
Total:        102 2827 1497.4   2429    9843

Percentage of the requests served within a certain time (ms)
  50%   2429
  66%   2676
  75%   3323
  80%   3501
  90%   5084
  95%   5626
  98%   7520
  99%   8759
 100%   9843 (longest request)

vmstat:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
1  0      0 294180 553860 1369112    0    0     0     5    1    1  2  1 97  0  1
46  0      0 290692 553860 1371060    0    0     0    24 3593 1589 26 10 62  0  1
43  0      0 287144 553860 1372832    0    0     0     0 9621 3446 78 20  0  0  2
39  0      0 285520 553860 1374092    0    0     0     8 9761 3458 79 19  0  0  2
43  0      0 284148 553864 1375348    0    0     0     6 9733 3448 74 20  0  0  6
42  0      0 282448 553864 1376636    0    0     0     0 10608 3741 80 19  0  0  1
40  0      0 287140 553868 1376548    0    0     0    64 9933 3341 75 20  0  0  4
0  0      0 286936 553868 1376964    0    0     0     0 7922 3100 59 12 24  0  5
0  0      0 287356 553868 1376928    0    0     0    14 1279 1234  9  3 88  0  1
0  0      0 287732 553872 1376520    0    0     0     6  734  993  2  1 93  0  3
0  0      0 287804 553872 1376480    0    0     0     0  526  835  2  0 96  0  1
1  0      0 287588 553872 1376480    0    0     0    36  817 1027  2  1 96  0  1
...

Go: ab -c 500 -t 10

ab:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Server Software:
Server Hostname:        10.*.*.*
Server Port:            9999

Document Path:          /cars/2d11a75ba3e84e31baaf1a998159328c
Document Length:        512 bytes

Concurrency Level:      500
Time taken for tests:   10.006 seconds
Complete requests:      9513
Failed requests:        0
Write errors:           0
Total transferred:      6051540 bytes
HTML transferred:       4871680 bytes
Requests per second:    950.75 [#/sec] (mean)
Time per request:       525.902 [ms] (mean)
Time per request:       1.052 [ms] (mean, across all concurrent requests)
Transfer rate:          590.63 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        1   45 207.6      1    3008
Processing:    43  463 138.5    439    1190
Waiting:        9  462 138.4    438    1190
Total:         44  508 251.7    446    3406

Percentage of the requests served within a certain time (ms)
  50%    446
  66%    484
  75%    513
  80%    542
  90%    732
  95%    985
  98%   1440
  99%   1529
 100%   3406 (longest request)

vmstat:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0      0 288524 553884 1376092    0    0     0     5    1    1  2  1 97  0  1
 0  0      0 288532 553884 1376092    0    0     0     6  626  831  2  1 96  0  1
 2  0      0 272128 553884 1376700    0    0     0     2 12055 1137 29 15 48  0  8
 2  0      0 269884 553884 1377772    0    0     0     0 29165 1347 62 34  0  0  3
 8  0      0 267872 553884 1378948    0    0     0     6 28029 1476 65 34  0  0  0
 5  0      0 266756 553884 1379952    0    0     0     0 27448 1287 66 34  0  0  0
 2  0      0 264784 553884 1381204    0    0     0     6 28451 1499 64 34  0  0  1
 0  0      0 267300 553884 1380992    0    0     0     0 17680 1118 44 22 34  0  1

接下来的测试 PHP 就不参与了。

Go: ab -c 1000 -t 10

ab:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Server Software:
Server Hostname:        10.*.*.*
Server Port:            9999

Document Path:          /cars/2d11a75ba3e84e31baaf1a998159328c
Document Length:        512 bytes

Concurrency Level:      1000
Time taken for tests:   10.001 seconds
Complete requests:      9420
Failed requests:        0
Write errors:           0
Total transferred:      5993028 bytes
HTML transferred:       4824576 bytes
Requests per second:    941.95 [#/sec] (mean)
Time per request:       1061.626 [ms] (mean)
Time per request:       1.062 [ms] (mean, across all concurrent requests)
Transfer rate:          585.23 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        1  250 631.0      1    3039
Processing:    70  726 188.4    689    2350
Waiting:        9  725 188.3    689    2350
Total:         71  976 669.1    732    4453

Percentage of the requests served within a certain time (ms)
  50%    732
  66%    834
  75%    960
  80%   1155
  90%   1728
  95%   2014
  98%   3767
  99%   3832
 100%   4453 (longest request)

vmstat:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
1  0      0 268840 553908 1379060    0    0     0     5    1    1  2  1 97  0  1
0  0      0 268920 553908 1379004    0    0     0     0  567  818  2  1 97  0  0
2  0      0 262484 553908 1379692    0    0     0     6 12628 1451 29 15 51  0  5
2  0      0 248940 553908 1381012    0    0     0     0 24177 1171 60 33  0  0  8
6  0      0 248364 553908 1381712    0    0     0     6 26423 1407 63 35  0  0  2
2  0      0 246628 553908 1382684    0    0     0     0 26805 1246 65 33  0  0  2
7  0      0 246296 553908 1383092    0    0     0     0 28097 1352 65 33  1  0  1
0  0      0 250588 553908 1381952    0    0     0    18 18602 1422 47 23 30  0  0

Go: ab -c 2000 -t 10

ab:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Server Software:
Server Hostname:        10.*.*.*
Server Port:            9999

Document Path:          /cars/2d11a75ba3e84e31baaf1a998159328c
Document Length:        512 bytes

Concurrency Level:      2000
Time taken for tests:   10.001 seconds
Complete requests:      9287
Failed requests:        0
Write errors:           0
Total transferred:      5907168 bytes
HTML transferred:       4755456 bytes
Requests per second:    928.62 [#/sec] (mean)
Time per request:       2153.734 [ms] (mean)
Time per request:       1.077 [ms] (mean, across all concurrent requests)
Transfer rate:          576.82 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        1  320 969.5      1    7019
Processing:   149 1158 606.9    972    7059
Waiting:       10 1158 606.9    971    7059
Total:        151 1478 1158.4   1088    9818

Percentage of the requests served within a certain time (ms)
  50%   1088
  66%   1421
  75%   1712
  80%   1893
  90%   2562
  95%   3781
  98%   4759
  99%   7853
 100%   9818 (longest request)

vmstat:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
1  0      0 255276 553916 1377432    0    0     0     5    1    1  2  1 97  0  1
3  0      0 251624 553916 1379996    0    0     0     6 5694 1353 16  7 74  0  2
5  0      0 229832 553916 1380964    0    0     0     0 27782 1264 63 34  0  0  3
2  0      0 229920 553916 1381388    0    0     0     6 26797 1342 65 33  0  0  1
2  0      0 229228 553916 1381388    0    0     0    14 26937 1249 66 32  0  0  2
2  0      0 230024 553916 1381188    0    0     0     0 25927 1326 63 33  1  0  2
3  0      0 228696 553916 1381328    0    0     0     6 27574 1325 66 33  0  0  0
0  0      0 233880 553916 1378344    0    0     0     0 2659  864  9  5 83  0  2

Go: ab -c 3000 -t 10

ab:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Server Software:
Server Hostname:        10.*.*.*
Server Port:            9999

Document Path:          /cars/2d11a75ba3e84e31baaf1a998159328c
Document Length:        512 bytes

Concurrency Level:      3000
Time taken for tests:   10.000 seconds
Complete requests:      8606
Failed requests:        0
Write errors:           0
Total transferred:      5473416 bytes
HTML transferred:       4406272 bytes
Requests per second:    860.57 [#/sec] (mean)
Time per request:       3486.051 [ms] (mean)
Time per request:       1.162 [ms] (mean, across all concurrent requests)
Transfer rate:          534.50 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        1  238 782.4      1    7020
Processing:   224 2194 1399.7   1743    9445
Waiting:        9 2193 1399.7   1743    9445
Total:        225 2432 1595.4   1951    9611

Percentage of the requests served within a certain time (ms)
  50%   1951
  66%   2566
  75%   3047
  80%   3417
  90%   4628
  95%   5739
  98%   7404
  99%   8209
 100%   9611 (longest request)

vmstat:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0      0 189716 553948 1376020    0    0     0     5    1    1  2  1 97  0  1
 4  0      0 178172 553948 1382220    0    0     0     0 8153 1510 24 10 66  0  0
 5  0      0 173428 553948 1383760    0    0     0    30 28276 1243 66 33  0  0  0
 3  0      0 168212 553948 1384200    0    0     0     0 26184 1204 66 33  0  0  2
 3  0      0 166308 553948 1384388    0    0     0     6 26741 1177 61 32  0  0  7
 6  0      0 163104 553948 1384352    0    0     0     0 24991 1344 64 32  0  0  4
 2  0      0 164008 553948 1384828    0    0     0     0 26093 1347 61 32  0  0  7
 0  0      0 173996 553948 1377480    0    0     0     6 11534 1093 30 16 50  0  3

总结

gnuplotApache Ab 对 Go 和 PHP 的测试结果数据做了个图表:

PHP:
PHP性能测试结果

Go:
Go性能测试结果