QPS、TPS、RT、併發數、吞吐量理解和性能優化深入思考

{"type":"doc","content":[{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"吞吐量"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在瞭解qps、tps、rt、併發數之前,首先我們應該明確一個系統的吞吐量到底代表什麼含義,一般來說,系統吞吐量指的是系統的抗壓、負載能力,代表一個系統每秒鐘能承受的最大用戶訪問量。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一個系統的吞吐量通常由qps(tps)、併發數來決定,每個系統對這兩個值都有一個相對極限值,只要某一項達到最大值,系統的吞吐量就上不去了。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"QPS"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Queries Per Second,每秒查詢數,即是每秒能夠響應的查詢次數,注意這裏的查詢是指用戶發出請求到服務器做出響應成功的次數,簡單理解可以認爲查詢=請求request。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"qps=每秒鐘request數量"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"TPS"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Transactions Per Second 的縮寫,每秒處理的事務數。一個事務是指一個客戶機向服務器發送請求然後服務器做出反應的過程。客戶機在發送請求時開始計時,收到服務器響應後結束計時,以此來計算使用的時間和完成的事務個數。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"針對單接口而言,TPS可以認爲是等價於QPS的,比如訪問一個頁面/index.html,是一個TPS,而訪問/index.html頁面可能請求了3次服務器比如css、js、index接口,產生了3個QPS。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"tps=每秒鐘事務數量"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"RT"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Response Time縮寫,簡單理解爲系統從輸入到輸出的時間間隔,寬泛的來說,他代表從客戶端發起請求到服務端接受到請求並響應所有數據的時間差。一般取平均響應時間。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"併發數"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"簡而言之,系統能同時處理的請求/事務數量。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"計算方式"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"QPS=併發數/RT 或者 併發數=QPS*RT"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"舉個栗子:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"假設公司每天早上9點到10點1個小時內都有員工要上廁所,公司有3600個員工,平均每個員工上廁所時間爲10分鐘,我們來計算一下。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"QPS = 3600/60*60 1"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"RT = 10*60 600秒"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"併發數 = 1 * 600 600"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這樣就意味着如果想達到最好的蹲坑體驗,公司需要600個坑位來滿足員工需求,否則的話上廁所就要排隊等待了。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"性能思考"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"按照QPS=併發數/RT公式,假設我們現在是單線程的場景,那麼QPS公式應該是這樣:QPS=1/RT,實際上RT應該=CPU time + CPU wait time,如果將線程數提高到2,那麼QPS=2/(CPU time + CPU wait time),那麼是否意味着我們只要單純提高線程數就能提高QPS呢?"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"最佳線程數計算"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"假設CPU time是49ms,CPU wait time是200ms,那麼QPS=1000ms/249ms=4.01,這裏200ms的wait時間我們可以認爲CPU一直處於等待狀態啥也沒幹,理論上來說200ms還可以接受200/49≈4個請求,不考慮上下文切換和其他開銷的話,可以認爲總線程數=(200+49)/49=5,如果再考慮上CPU多核和利用率的問題,我們大致可以認爲:"},{"type":"text","marks":[{"type":"strong"}],"text":"最佳線程數=RT/CPUTime * CPU核心數 * CPU利用率"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"那麼最大QPS公式推導爲:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"最大QPS=最佳線程數*單線程QPS=("},{"type":"text","marks":[{"type":"strong"}],"text":"RT/CPU Time * CPU核心數 * CPU利用率)*(1/RT) = CPU核心數*CPU利用率/CPUTime"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"那麼這樣是否意味着我們只要不停增加CPU核心數就能無限提高QPS呢?"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"阿姆達爾定律Amdahl"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/c8/c802b58a85e14a352dadf6a9a2050e47.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"G.M.Amdahl在1967年提出了Amdahl’s law,針對並行處理的scalability給出了一個模型,指出使用並行處理的提速由問題的可並行的部分所決定。我們可以簡單理解爲程序通過額外的計算資源,理論上能獲得的加速值。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"par爲並行計算所佔的比例,p爲並行處理節點個數"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"假設你想從望京去順義,坐一輛車需要3小時,雖然現在有3輛車,你也不能1小時就到。這裏無法並行,所有Par=0%,p=3,加速比還是等於1,並沒有提高速度。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"古斯塔夫森定律Gustafson"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/41/412d5750dea63c2fdd7639f03b5e59b9.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"斯塔夫森定律又被稱爲擴展的加速比(scaled speedup),他說明處理器個數、串行比例和加速比之間的關係,只是和阿姆達爾定律側重角度有所不同。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"按照阿姆達爾定律和QPS計算公式,在CPUtime 和 CPU利用率不變的情況下,增加CPU核心數就能增加最大QPS,在par不爲0即並行的時候,增加並行數量p就能提升效率,但是實際上隨着請求數量的增加,帶來大量的上下文的切換、gc和鎖變化。qps更高,產生對象越多,gc越頻繁,cpu time和利用率都受到影響,尤其在串行的時候,鎖自旋、自適應、偏向等等也成爲影響par的因素。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"總結,爲了提升達到最好的性能,我們需要不斷的進行性能測試,調整小城池大小,找到最合適的參數來達到提高性能的目的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"參考:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"http://javahao123.com/?p=772","title":null},"content":[{"type":"text","text":"http://javahao123.com/?p=772"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://cloud.tencent.com/developer/article/1106559","title":null},"content":[{"type":"text","text":"https://cloud.tencent.com/developer/article/1106559"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://www.cnblogs.com/caishunzhe/p/13056105.html","title":null},"content":[{"type":"text","text":"https://www.cnblogs.com/caishunzhe/p/13056105.html"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://www.jianshu.com/p/8532ac88ce72","title":null},"content":[{"type":"text","text":"https://www.jianshu.com/p/8532ac88ce72"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://zhuanlan.zhihu.com/p/66929848","title":null},"content":[{"type":"text","text":"https://zhuanlan.zhihu.com/p/66929848"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://www.cnblogs.com/lupeng2010/p/12705795.html","title":null},"content":[{"type":"text","text":"https://www.cnblogs.com/lupeng2010/p/12705795.html"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/e3/e3e2b1d9d8cc8edce05f39dc757198f3.jpeg","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章