高效的性能測試工具-wrk

今天主要介紹一款高效的性能測試工具wrk。wrk的使用方式和apache bench這些工具也大體相似,通過命令行的方式即可發起。但是wrk比起apache bench更爲高效,因爲它支持多線程,更容易發揮多核CPU的能力,甚至可以壓滿CPU。wrk還支持Lua腳本來提供更多的參數定製、參數加密等需求,靈活度更高。


安裝

wrk支持大部分UNIX系統,不支持windows系統。安裝過程比較簡單,從github克隆項目到本地,再在項目路徑下make即可,在此就不詳述,具體可查看github文檔。


基礎使用

wrk -t12 -c400 -d30s http://127.0.0.1:80/index.html

以上命令行表示對本地80端口的index.html文件發起請求,壓測時間持續30秒,併發12線程,保持400個HTTP連接請求。

輸出結果:

Running 30s test @ http://127.0.0.1:80/index.html
  12 threads and 400 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    90.43ms  201.95ms   1.99s    88.34%
    Req/Sec     1.79k     1.80k   21.57k    89.17%
  577891 requests in 30.09s, 188.46MB read
  Socket errors: connect 0, read 0, write 0, timeout 37
  Non-2xx or 3xx responses: 577891
Requests/sec:  19202.50
Transfer/sec:      6.26MB

結果展示了本次壓測的請求平均延遲Latency、每秒每線程平均請求數Req/Sec、合計的每秒請求數Requests/sec和每秒吞吐量Transfer/sec等。這些數據即可反映出壓測服務器的大概響應情況,以對服務器性能做初步判斷。

貼一下請求的命令參數:

-c, --connections: total number of HTTP connections to keep open with
                   each thread handling N = connections/threads
-d, --duration:    duration of the test, e.g. 2s, 2m, 2h
-t, --threads:     total number of threads to use
-s, --script:      LuaJIT script, see SCRIPTING
-H, --header:      HTTP header to add to request, e.g. "User-Agent: wrk"
    --latency:     print detailed latency statistics
    --timeout:     record a timeout if a response is not received within
                   this amount of time.

關於長連接和短連接:apache bench默認短連接,wrk默認長連接。wrk如需測試短連接,可添加 -H “Connection:Close” 來關閉長連接。


Post請求

wrk不僅能發起對Get請求,也能通過Lua腳本發起Post請求。如編寫post.lua腳本:

wrk.method = "POST"wrk.headers["Content-Type"] = "application/x-www-form-urlencoded"wrk.body = "youbody&youset"

在腳本中定義了Post請求的headers和body。執行wrk請求時加上—script指定該腳本:

wrk -t4 -c2000 -d60s -T5s --script=post.lua --latency http://127.0.0.1:80/user/login

如此,便可輕鬆實現Post請求的壓測。


後續

wrk作爲一種高效的性能測試工具,可以作爲一種常用手段對待測url發起請求。本文只是初步介紹了引用Lua腳本執行Post請求,Lua腳本其實可以實現更爲豐富的測試需求,下一次我們再詳細看下Lua腳本如何爲wrk添翼。


睿江雲官網鏈接:https://www.eflycloud.com/home?from=RJ0035


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章