over 7 years ago
介紹一個壓力測試的好物 - Gatling
基於scala開發的高性能測試工具,主要使用了Akka Actors技術來達到很高的並發數
測試完成後產生漂亮的html報表,使用簡易的函式來寫腳本,不會寫沒關係還可以使用錄製的方式製作腳本
還可以與Jenkins整合
在build.sbt加上依賴庫
libraryDependencies += "io.gatling.highcharts" % "gatling-charts-highcharts" % "2.1.5"
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import io.gatling.jdbc.Predef._
//一定要繼承Simulation,格林機關槍才可以識別你的腳本
class AlertAdd extends Simulation {
val baseURL = "http://10.240.136.204:8080"
//測試資料來源為csv檔案,queue如果用完會出現Exception,如果你希望無限循環應該使用csv("alert_terms.csv").circular
val csvFeeder = csv("alert_terms.csv").queue
//基本的http設定
val httpConf = http
.baseURL(baseURL)
.acceptCharsetHeader("utf-8;q=0.7,*;qc=0.7")
.acceptHeader("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
.acceptEncodingHeader("gzip, deflate")
.acceptLanguageHeader("en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4")
.disableFollowRedirect
//綁定Content-Type
val header = Map(
"Content-Type" -> "application/json; charset=UTF-8")
//主要執行指令
//repeat表示我要執行的次數,這邊指定跟檔案一樣多筆才不會爆掉
val scn = scenario("alertAdd").repeat(csvFeeder.records.size) {
//feed為資料來源
feed(csvFeeder)
.exec(http("alertAdd")
.post("/springstock/alertterms")
//Http Body為Json格式
.body(StringBody( """{"cid": "${cid}","funcid": "${funcid}","tab": "${tab}","stk": "${stk}","val": "${val}","msgtype": "${msgtype}","expire": "${expire}","xcnts": "${xcnts}","cstamp": "${cstamp}","staff": "${staff}","idx": "${idx}"}""")).asJSON
.headers(header)
//check是成功條件
.check(status.is(200)))
}
//注入模擬單一使用者
setUp(scn.inject(atOnceUsers(1))).protocols(httpConf)
}
另一個範例
//這邊我要模擬一筆資料刪除後又新增,這時候就要用circular來循環測試
val csvAlert = csv("alert_terms.csv").circular
//during(持續測試時間,單位是秒)
val alert = scenario("Alert").during(60) {
feed(csvAlert)
//先執行新增
.exec(http("AlertAdd")
.post("/springstock/alertterms")
.body(StringBody( """{"cid": "${cid}","funcid": "${funcid}","tab": "${tab}","stk": "${stk}","val": "${val}","msgtype": "${msgtype}","expire": "${expire}","xcnts": "${xcnts}","cstamp": "${cstamp}","staff": "${staff}","idx": "${idx}"}""")).asJSON
.headers(header).check(status.is(200)))
//同一筆資料再執行刪除
.exec(http("AlertDel")
.delete("/springstock/alertterms?cid=${cid}&funcid=${funcid}&tab=${tab}&stk=${stk}&val=${val}&msgtype=${msgtype}")
.headers(header).check(status.is(200)))
}
//最後啟動任務,你有多個任務的話就這樣寫
setUp(alert.inject(atOnceUsers(1))
, scn_emerging.inject(atOnceUsers(3))
, scn_fut.inject(atOnceUsers(3))
, scn_opt.inject(atOnceUsers(3))
, scn_otc.inject(atOnceUsers(3))
, scn_tse.inject(atOnceUsers(3))
).protocols(httpConf)
說明一下你的資料格式是要含有表頭的喔,像這樣
cid,funcid,tab,stk,val,msgtype,expire,xcnts,cstamp,stamp,staff,idx
1,0,tse,2881,43.0000,1,2015-07-11 23:59:59,5,2014-07-11 12:38:53.793000000,2014-07-11 12:38:53.793000000,SL-USSDQUERY-9,-1
1,0,tse,2882,46.2000,1,2015-07-11 23:59:59,5,2014-07-11 12:38:24.440000000,2014-07-11 12:38:24.440000000,SL-USSDQUERY-9,-1
腳本寫好後,把官網下載的gatling-charts-highcharts-bundle-2.1.5-bundle.zip解開後
把你的腳本(scala)放到\user-files\simulations
ps.要包含package啊...不要只丟檔案
如果需要csv就把資料檔放到\user-files\data
如果csv檔太大記得先加上
//Windows
set JAVA_OPTS=-Xmx4096m
//Linux
JAVA_OPTS="-Xmx4096m"
之後執行\bin\gatling.bat
運行中
執行完成
到\results看你的測試報告
補充一個很特別的測試方式
.randomSwitch(
40d -> exec(http("Catégorie Poney").get("/")),
50d -> exec(http("Catégorie Licorne").get("/"))
)
這樣表示有40%的機率執行上面那一行
有50%機率執行下面那一行
剩下10%呢?.....我還沒研究XD
目前好像還沒有多台串連壓測啊,單一台還是有測試上限