tornado + aiomysql 異步查詢mysql

負(fù)載均衡
http {

# ... 省略其它配置

upstream art {
       server localhost:8281;
       server localhost:8282;
       server 10.27.250.248:8284;
 }

server {
   listen 8088;
   location / {
      proxy_pass http://art;
  }
}

# ... 省略其它配置

}

壓力測(cè)試
ab -n 10000 -c 100 -T application/x-www-form-urlencoded -p post.txt http://127.0.0.1:8088/art/api/action

post.txt

 p=%7B%7D&method=markers&modular=reports&pageSize=1&pageNo=1

阻塞情況
直接使用使用sqlalchemy

zshdeMacBook-Air:Desktop zsh$ ab -n 10000 -c 100 -T application/x-www-form-urlencoded -p post.txt  http://10.27.250.248:8284/art/api/action

Finished 10000 requests


Server Software:        TornadoServer/5.1.1
Server Hostname:        10.27.250.248
Server Port:            8284

Document Path:          /art/api/action
Document Length:        479 bytes

Concurrency Level:      100
Time taken for tests:   171.431 seconds
Complete requests:      10000
Failed requests:        0
Total transferred:      6320000 bytes
Total body sent:        2410000
HTML transferred:       4790000 bytes
Requests per second:    58.33 [#/sec] (mean)
Time per request:       1714.306 [ms] (mean)
Time per request:       17.143 [ms] (mean, across all concurrent requests)
Transfer rate:          36.00 [Kbytes/sec] received
                       13.73 kb/s sent
                       49.73 kb/s total

非阻塞
使用aiomysql

ab -n 10000 -c 100 -T application/x-www-form-urlencoded -p post.txt  http://10.27.250.248:8284/art/api/action

Finished 10000 requests


Server Software:        TornadoServer/5.1.1
Server Hostname:        10.27.250.248
Server Port:            8284

Document Path:          /art/api/action
Document Length:        138 bytes

Concurrency Level:      100
Time taken for tests:   22.589 seconds
Complete requests:      10000
Failed requests:        0
Total transferred:      2910000 bytes
Total body sent:        2410000
HTML transferred:       1380000 bytes
Requests per second:    442.70 [#/sec] (mean)
Time per request:       225.886 [ms] (mean)
Time per request:       2.259 [ms] (mean, across all concurrent requests)
Transfer rate:          125.81 [Kbytes/sec] received
                        104.19 kb/s sent
                        230.00 kb/s total

總結(jié)

將數(shù)據(jù)庫(kù)查詢異步后,并發(fā)時(shí),請(qǐng)求時(shí)間縮短的還是很明顯的,使用tornado框架時(shí),除了讓請(qǐng)求異步非阻塞,相關(guān)的redis、mysql、mongodb等數(shù)據(jù)庫(kù)查詢也要異步才能達(dá)到相應(yīng)的效果。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容