关于性能优化:记录一次快速注册接口的优化

39次阅读

共计 5988 个字符,预计需要花费 15 分钟才能阅读完成。

1 新旧版本实现

旧版本:PHP 语言实现, 应用屡次 DB 查问来生成一个用户名,再 DB 查问用户表,确保用户名未反复

新版本:PHP 语言实现, 从 Redis 间接获取一个可用用户名,注册时候再判断是否未反复

Go 版本:Go 语言实现,其余统一

这里的优化次要有两点:

1. 次要是采纳发号器的思维,预生成可用的账号,而不是每次用的时候才生成

2. 把判断是否未反复滞后,真正实现注册的那一步才去判断,因为很多用户下载了游戏,未必走到注册那一步,大略只有 80% 出头的激活注册率

2 压测体现

ab 压测各情景下均屡次压测,取较两头值的一次数据

2.1 旧版本

terence@k8s-master:~$ ab -c 10 -n 50 -p post http://dksdk_api.test/api/v7/user/registerone
ab: Could not open POST data file (post): No such file or directory
terence@k8s-master:~$ ab -c 10 -n 50 -p a.txt http://dksdk_api.test/api/v7/user/registerone
This is ApacheBench, Version 2.3 <$Revision: 1807734 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking dksdk_api.test (be patient).....done


Server Software:        nginx
Server Hostname:        dksdk_api.test
Server Port:            80

Document Path:          /api/v7/user/registerone
Document Length:        175 bytes

Concurrency Level:      10
Time taken for tests:   25.667 seconds
Complete requests:      50
Failed requests:        40
   (Connect: 0, Receive: 0, Length: 40, Exceptions: 0)
Total transferred:      29886 bytes
Total body sent:        7950
HTML transferred:       8636 bytes
Requests per second:    1.95 [#/sec] (mean)
Time per request:       5133.476 [ms] (mean)
Time per request:       513.348 [ms] (mean, across all concurrent requests)
Transfer rate:          1.14 [Kbytes/sec] received
                        0.30 kb/s sent
                        1.44 kb/s total

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.2      0       1
Processing:  2091 4931 1898.5   4688   10802
Waiting:     2091 4931 1898.5   4688   10802
Total:       2091 4931 1898.6   4688   10802

Percentage of the requests served within a certain time (ms)
  50%   4688
  66%   6068
  75%   6105
  80%   6189
  90%   7721
  95%   8355
  98%  10802
  99%  10802
 100%  10802 (longest request)

均匀耗时 513ms, 其中Lumen 框架启动大略 30~100ms 以内,DB查问视状况是否现实大略 500ms 左右,非对称加密个位数级别若干毫秒可忽略不计

2.2 新版本

terence@k8s-master:~$ ab -c 10 -n 50 -p a.txt http://dksdk_api_v2.test/api/v7/user/registerone
This is ApacheBench, Version 2.3 <$Revision: 1807734 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking dksdk_api_v2.test (be patient).....done


Server Software:        nginx
Server Hostname:        dksdk_api_v2.test
Server Port:            80

Document Path:          /api/v7/user/registerone
Document Length:        346 bytes

Concurrency Level:      10
Time taken for tests:   1.715 seconds
Complete requests:      50
Failed requests:        40
   (Connect: 0, Receive: 0, Length: 40, Exceptions: 0)
Total transferred:      26941 bytes
Total body sent:        8100
HTML transferred:       17241 bytes
Requests per second:    29.15 [#/sec] (mean)
Time per request:       343.076 [ms] (mean)
Time per request:       34.308 [ms] (mean, across all concurrent requests)
Transfer rate:          15.34 [Kbytes/sec] received
                        4.61 kb/s sent
                        19.95 kb/s total

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.2      0       1
Processing:    80  336 133.9    343     621
Waiting:       80  336 133.9    343     621
Total:         81  337 133.8    343     621

Percentage of the requests served within a certain time (ms)
  50%    343
  66%    375
  75%    410
  80%    474
  90%    521
  95%    547
  98%    621
  99%    621
 100%    621 (longest request)

均匀耗时 34ms, 只有旧版本的6.6%,其中Lumen 框架启动大略 30~100ms 以内,DB查问大略 200ms 左右,非对称加密个位数级别若干毫秒可忽略不计

2.3 旧版本 开启 Opcache

terence@k8s-master:~$ ab -c 10 -n 50 -p a.txt http://dksdk_api.test/api/v7/user/registerone
This is ApacheBench, Version 2.3 <$Revision: 1807734 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking dksdk_api.test (be patient).....done


Server Software:        nginx
Server Hostname:        dksdk_api.test
Server Port:            80

Document Path:          /api/v7/user/registerone
Document Length:        420 bytes

Concurrency Level:      10
Time taken for tests:   19.284 seconds
Complete requests:      50
Failed requests:        27
   (Connect: 0, Receive: 0, Length: 27, Exceptions: 0)
Total transferred:      42057 bytes
Total body sent:        7950
HTML transferred:       20807 bytes
Requests per second:    2.59 [#/sec] (mean)
Time per request:       3856.747 [ms] (mean)
Time per request:       385.675 [ms] (mean, across all concurrent requests)
Transfer rate:          2.13 [Kbytes/sec] received
                        0.40 kb/s sent
                        2.53 kb/s total

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       0
Processing:  1830 3689 1213.6   3527    7273
Waiting:     1830 3689 1213.6   3527    7273
Total:       1830 3690 1213.6   3527    7273

Percentage of the requests served within a certain time (ms)
  50%   3527
  66%   3911
  75%   4534
  80%   4907
  90%   5604
  95%   5639
  98%   7273
  99%   7273
 100%   7273 (longest request)

均匀耗时 385ms, 相当于只节俭了Lumen 框架启动所需的工夫,耗时大头的 DB 查问还是原样

2.4 新版本 开启 Opcache

terence@k8s-master:~$ ab -c 10 -n 50 -p a.txt http://dksdk_api_v2.test/api/v7/user/registerone
This is ApacheBench, Version 2.3 <$Revision: 1807734 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking dksdk_api_v2.test (be patient).....done


Server Software:        nginx
Server Hostname:        dksdk_api_v2.test
Server Port:            80

Document Path:          /api/v7/user/registerone
Document Length:        345 bytes

Concurrency Level:      10
Time taken for tests:   0.143 seconds
Complete requests:      50
Failed requests:        26
   (Connect: 0, Receive: 0, Length: 26, Exceptions: 0)
Total transferred:      26951 bytes
Total body sent:        8100
HTML transferred:       17251 bytes
Requests per second:    349.37 [#/sec] (mean)
Time per request:       28.623 [ms] (mean)
Time per request:       2.862 [ms] (mean, across all concurrent requests)
Transfer rate:          183.91 [Kbytes/sec] received
                        55.27 kb/s sent
                        239.18 kb/s total

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       0
Processing:     4   27  18.4     22      89
Waiting:        4   27  18.4     22      89
Total:          4   27  18.4     22      90

Percentage of the requests served within a certain time (ms)
  50%     22
  66%     31
  75%     36
  80%     42
  90%     52
  95%     56
  98%     90
  99%     90
 100%     90 (longest request)

均匀耗时 2.86ms, 节俭了Lumen 框架启动所需的工夫,没有 DB 查问,只有一次 Redis 查问, 毫秒级别能就搞定了

2.5 新版本 Go 语言

terence@k8s-master:~$ ab -c 10 -n 50 -p a.txt http://192.168.10.83:8080/registerone
This is ApacheBench, Version 2.3 <$Revision: 1807734 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 192.168.10.83 (be patient).....done


Server Software:        
Server Hostname:        192.168.10.83
Server Port:            8080

Document Path:          /registerone
Document Length:        0 bytes

Concurrency Level:      10
Time taken for tests:   0.050 seconds
Complete requests:      50
Failed requests:        0
Total transferred:      3750 bytes
Total body sent:        7550
HTML transferred:       0 bytes
Requests per second:    1007.03 [#/sec] (mean)
Time per request:       9.930 [ms] (mean)
Time per request:       0.993 [ms] (mean, across all concurrent requests)
Transfer rate:          73.76 [Kbytes/sec] received
                        148.50 kb/s sent
                        222.25 kb/s total

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    4   3.0      3      10
Processing:     0    5   3.3      6      17
Waiting:        0    4   3.1      5      10
Total:          5    9   4.4      8      20

Percentage of the requests served within a certain time (ms)
  50%      8
  66%      9
  75%     10
  80%     11
  90%     19
  95%     19
  98%     20
  99%     20
 100%     20 (longest request)

均匀耗时0.993ms,真香

正文完
 0