记录一次快速注册接口的优化

Posted tfzh

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了记录一次快速注册接口的优化相关的知识,希望对你有一定的参考价值。

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,真香

以上是关于记录一次快速注册接口的优化的主要内容,如果未能解决你的问题,请参考以下文章

使用策略模式优化代码实践,如何让项目快速起飞

记一次接口性能优化实践总结:优化接口性能的八个建议

记录一次代码优化

提效小技巧——记录那些不常用的代码片段

记录一次优化ifelse经历

快速排序