jmeter壓測:html
工具準備:apache-jmeter-5.0.tgzjava
本地啓動jmeter: nohup sh apache-jmeter-5.0/bin/jmeter & 修改語言:option(選項) - choose language(選擇語言) 安裝插件:選項 - Plugins Manager - Available Plugins - 搜索:jpgc - Standard Setgit
機器性能監控: 下載ServerAgent-2.2.3.zip插件: 下載地址:https://github.com/undera/perfmon-agent/blob/master/README.md 修改startAgent.sh文件: 修改前:java -jar $(dirname $0)/CMDRunner.jar --tool PerfMonAgent "$@" 修改後:java -jar $(dirname $0)/CMDRunner.jar --tool PerfMonAgent --udp-port 6789 --tcp-port 6789 "$@" 在被壓機器上啓動agent: nohup sh startAgent.sh & 關閉agent: kill掉如下的進程: ps aux | grep startAgent.sh ps aux | grep CMDRunner
編寫測試計劃:github
添加'用戶定義的變量': target-server-addr 目標服務器 target-server-port 目標服務器端口 ... 測試計劃右鍵 - 線程(用戶) - 線程組 線程數:指定發起請求的線程數。 ramp-up period:在多長時間內啓動全部的線程。 循環次數:指定每一個線程循環調用的次數。 1>指定固定循環的次數。注:若調度器的持續時間設置的太小,則會致使沒法達到設定的循環的次數。 2>不設置固定的次數,即選'永遠'。注:並不會永遠執行,由於下面還設置了持續時間。 調度器: 持續時間:發請求持續的時間。 啓動延遲:延遲發出請求的時間。若不設置,則執行jmeter命令後當即發出請求。 在線程組上添加: 取樣器-HTTP請求:填寫 服務器地址+端口、請求方法+地址+請求參數 在HTTP請求上添加:配置元件-HTTP信息頭管理器,並配置頭信息。eg:Content-Type application/json 在線程組上 或 在HTTP請求上 添加: 定時器-Constant Throughput Timer(固定吞吐量定時器): Target throughput(in samples per minute):目標吞吐量。 說明:目標吞吐量爲10,則這裏應該填600。若是"循環次數"或"持續時間"過小,則頗有可能達不到這裏設置的吞吐量。 Calculate Throughput based on(計算吞吐量的方式): this thread only: each thread will try to maintain the target throughput. The overall throughput will be proportional to the number of active threads. 將每一個線程的吞吐量設爲Target throughput。故:總吞吐量 = Target Throughput * 線程數。 all active threads: the target throughput is divided amongst all the active threads in all Thread Groups. Each thread will delay as needed, based on when it last ran. In this case, each other Thread Group will need a Constant Throughput timer with the same settings. 將全部活躍線程的總吞吐量設爲Target throughput。 活躍線程指同一時刻同時運行的線程,每一個活躍線程在 上一次運行結束後 等待合理的時間後再次運行。全部的線程組都應該使用相同的配置。 all active threads in current thread group: the target throughput is divided amongst all the active threads in the group. Each thread will delay as needed, based on when it last ran. 將該線程組中活躍線程的總吞吐量設爲Target throughput。當測試計劃中只有一個線程組時,該選項和all active threads效果相同。 all active threads (shared): as above(all active threads); each thread is delayed based on when any thread last ran. 與all active threads相似,不一樣的是,每一個活躍線程會在 全部活躍線程上一次運行結束後 等待合理的時間後再次運行。 all active threads in current thread group (shared) : as above(all active threads in current thread group), but each thread is delayed based on when any thread in the group last ran. 與all active threads in current thread group相似,不一樣的是,每一個活躍線程會在 該線程組中全部活躍線程上一次運行結束後 等待合理的時間後再次運行。 在HTTP請求上添加: 後置處理器-JSON Extractor(JSON提取器) Names of created variables 變量名,後面的HTTP請求能夠經過${Variable names}獲取該變量的值。 JSON Path expressions json path表達式,用於獲取response中指定參數的值。eg:{"person":{"name":"jack"}} ==> $.person.name Match No.(0 for Random) 0表明隨機,1表明第一個,-1表明全部 Compute concatenation var(suffix_ALL) 是否將全部匹配到的值保存下來(格式:變量名=「變量名_ALL」、值=全部匹配到的值以逗號鏈接的字符串)。 Default Values 默認值,通常設爲NOT FOUND 在HTTP請求上添加: 後置處理器-BeanShell PostProcessor 1>引入jar包:apache-jmeter-5.0/lib/ext 下添加fastjson的jar包。 2>編寫Script: String response = prev.getResponseDataAsString(); // 獲取response的內容(json串),prev變量是jmeter提供的。 JSONObject responseObj = JSON.parseObject(response); // 使用fastjson來解析json串。 vars.put("key1", "根據response解析出來key1的value"); // 向vars集合中添加key-value對,後面的HTTP請求能夠經過${key}來獲取集合中的值,vars變量是jmeter提供的。 在線程組上添加: 邏輯控制器-若是(if)控制器 條件:條件表達式。eg:"${count}"=="0" 邏輯控制器-若是(ForEach)控制器 輸入變量前綴:變量的前綴。eg:level1_ ,則能夠遍歷全部以"level1_"開頭的變量,好比level1_aa、level1_bb等。 輸出變量名稱:遍歷時使用的變量。 說明:能夠當作 for (String 輸出變量名稱 : 輸入變量前綴) 在線程組上添加: 取樣器-Debug Sampler 在Debug Sampler上添加:後置處理器-BeanShell PreProcessor,咱們能夠經過BeanShell PreProcessor中的srcipt來初始化一些信息,即向vars集合中添加key-value對。 說明:Debug Sampler通常都在HTTP請求的前面,即先執行線程組中的Debug Sampler,而後再依次執行HTTP請求。 測試計劃右鍵 - 線程(用戶) - jp[@gc](https://my.oschina.net/gccx) - Ultimate Thread Group 說明:Ultimate Thread Group 是用來實現階梯式壓測的。 Start Threads Count 當前行啓動的線程數 Initial Delay, sec 延時啓動當前行的線程。單位秒 Startup Time, sec 啓動當前行全部線程達峯值所需時間,單位秒 Hold Load For, sec 當前行線程達到峯值後持續的時間 單位秒 Shutdown Time 中止當前行全部線程所需時間,單位秒
在施壓機器上以非GUI的方式來壓測: jmeter -n -t jmx文件 -l 結果文件路徑 -e -o Web報告保存路徑 參數: -n run JMeter in nongui mode -t the jmeter test(.jmx) file to run -l the file to log samples to -e generate report dashboard after load test -o output folder for report dashboard eg: 腳本啓動:start.sh 0808-getjob-200-10 # 日期-接口名-tps-壓測持續時間 start.sh mkdir -p ~/data/jmeter/$1 apache-jmeter-5.0/bin/jmeter -n -t ~/data/jmeter/plan/my-plan.jmx -l ~/data/jmeter/$1/log -e -o ~/data/jmeter/$1/my-web-reportweb
報告查看:~/data/jmeter/0807/my-web-report/index.html
常見問題: jmeter導入jmx文件報錯:missing class com.thoughtworks.xstream.converters.ConversionException 解決:jmeter插件下載地址:https://jmeter-plugins.org/downloads/all/ ,將下載的jmeter-plugins-manager-0.11.jar文件放置到jmeter目錄下\lib\ext目錄下,而後重啓JMeter便可。express
參考資料:https://www.cnblogs.com/richered/category/1151707.htmlapache
吞吐量 吞吐量是指單位時間內處理完請求的數量。json
qps: qps是指每秒處理完請求的數量 qps = 併發量 / 平均響應時間。服務器
經常使用的統計腳本:併發
echo "2019-07-25 22:57:32.117" | awk '{print $2}' | awk -F "." '{print$1}' | sort | uniq -c | awk '{print $1}' | sort -nr | head -1
echo "2019-07-25 22:57:32.117" | awk '{print $2}' | awk -F "." '{print$1}' | sort | uniq -c | grep qps的最大值
echo "2019-07-25 22:57:32.117" | awk '{print $2}' | awk -F ":" '{print $1":"$2}' | sort | uniq -c | awk '{print $1}' | sort -nr | head -1
minute_time=$(date -d '-1 minute' +"%Y-%m-%d %H:%M") grep "${now_date}" xxx.log | grep -c "請求uri"
split函數:The awk function split(s,a,sep) splits a string s into an awk array a using the delimiter sep 舉例:echo "2019-07-25" | awk '{len=split($0,a,"-");print "length="len; for(i=1;i<=len;i++)print a[i];}'