Tornado做鑑權服務效能實踐

2021-09-13 02:22:49 字數 1409 閱讀 7456

使用python的第三方jwt做鑑權服務, 生產token**:

def create_token(self, userid, product, level):

payload =

token = jwt.encode(payload, secretkey, algorithm='hs256')

return token

mongodb版本是3.6,資料庫操作使用了pymongo;使用了自定義的物件倉儲,對比直接運算元據格式本身,這一點肯定是拖效能後退的

鑑權控制代碼的實現:

class authhandle(requesthandler):

def post(self):

try:

body = json.loads(self.request.body)

user = userrepository().get(body['username'])

if not user:

user = userrepository().create_new(body['username'])

token = authenticationner().create_token(user.userid, user.product, user.level)

self.write()

except exception:

logger.error(traceback.format_exc())

tornado做web服務和路由**:

def __init__(self):

handlers = [

(r"/users/login", authhandle),

]if __name__ == "__main__":

tornado.ioloop.ioloop.instance().start()

使用cenos環境,雙核,8g記憶體,沒有反向**和快取的前提下,效能表現如下

使用jmeter做200併發壓力,結果如下:

最大時延4s,tps達到39.6/s,感覺還是很理想的

http_server.start(0) # 程序數量等於機器核數量

tornado.ioloop.ioloop.instance().start()

效能有明顯提公升:

最大時延484ms,tps達到了126

使用Postman做鑑權計算

postman是進行開發測試的常用工具。而在請求過程中,經常需要進行登入 加密鑑權。通過postman自帶的pre request script和tests功能,我們可以將過程 化,節約時間。postman的pre request script功能可以在請求前執行指令碼,進行鑑權的計算 通過環境變數...

loadrunner監控mysql服務效能

sitescope是惠普出的乙個簡單易用的監控工具,可以用來監控資料庫,系統資源等 開啟瀏覽器 複製mysql驅動 如何重啟sitescope服務呢?點選開始,搜尋services.msc即可開啟服務,找到hp sitescope,點選重啟服務即可 sitescope主頁面 新建乙個組 新建乙個監控...

給REST介面做鑑權認證 OAuth

本地儲存使用者名稱和密碼肯定是不合適的。參考了 如何設計好的restful api之安全性這裡寫鏈結內容 重新學習了oauth 發現原來oauth2.0還有四種模式可選 授權碼模式 authorization code 簡化模式 resource owner 密碼模式 password creden...