記錄一次快速註冊接口的優化

1 新舊版本實現

舊版本:PHP語言實現,使用屢次DB查詢來生成一個用戶名,再DB查詢用戶表,確保用戶名未重複nginx

新版本:PHP語言實現,從Redis直接獲取一個可用用戶名,註冊時候再判斷是否未重複shell

Go版本:Go語言實現,其餘一致apache

這裏的優化主要有兩點:api

1.主要是採用發號器的思想,預生成可用的帳號,而不是每次用的時候才生成

2.把判斷是否未重複滯後,真正完成註冊的那一步纔去判斷,由於不少用戶下載了遊戲,未必走到註冊那一步,大概只有80%出頭的激活註冊率框架

2 壓測表現

ab壓測各情形下均屢次壓測,取較中間值的一次數據post

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查詢仍是原樣.net

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查詢,毫秒級別能就搞定了code

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

相關文章
相關標籤/搜索