Gatling 自動化壓測實踐
Gatling是一款基于Scala 開發的高性能服務器性能測試工具,它主要用于對服務器進行負載等測試;想使用Gatling進行壓測的原因之一是想體驗一下Scala編程的感覺,玩一下;第二,工作上也確實有這樣的需求;
壓測工作簡單來說就是利用壓測應用,來測試一下服務器的響應性能參數;然后把這些工作全部自動化,集成到jenkins中來運行。
整個工作的子任務分解可以由下圖來表示:
壓測使用的是一個常見的web應用,該web應用的具體使用的業務場景如下:
針對該應用的壓測Scala源代碼如下:
文件名:performance.scala
package performanceimport scala.concurrent.duration._
import io.gatling.core.Predef. import io.gatling.http.Predef. import io.gatling.jdbc.Predef._
class Performance extends Simulation {
//用戶名、餐館ID 存儲文件 val user = csv("/root/.jenkins/workspace/testGatling/src/test/scala/data/user.csv").random val res = csv("/root/.jenkins/workspace/testGatling/src/test/scala/data/restaurant.csv").random val ip = csv("/root/.jenkins/workspace/testGatling/src/test/scala/data/ip.csv").random
val httpProtocol = http .baseURL("http://${ip}:8180")
val headers_0 = Map("Accept" -> "text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8")
val scn = scenario("Emenu") //打開訂餐首頁 .exec(http("index") .get("/E_Menu/userlogin/login.jsp") .headers(headers_0))
//登錄 .pause(1 second,20 second) .feed(user) .feed(res) .exec(http("login") .post("/E_Menu/userlogin/login") .headers(headers_0) .formParam("username", "${username}") .formParam("password", "${password}") ) //選擇餐館 .pause(1 second,20 second) .exec(http("menu") .get("/E_Menu/menu.action?res_num=${rest_id}") .headers(headers_0) ) //點菜 .pause(1 second,20 second) .exec(addCookie(Cookie("username", "${username}"))) .exec(addCookie(Cookie("res_num", "${rest_id}"))) .exec(addCookie(Cookie("food_num0", "105"))) .exec(addCookie(Cookie("food_num1", "104"))) .exec(addCookie(Cookie("food_num2", "104"))) .exec(addCookie(Cookie("food_num3", "106"))) .exec(addCookie(Cookie("total", "52"))) .exec(http("list") //點完菜,開始訂 .get("/E_Menu/list.action") .headers(headers_0) ) //下單 .pause(1 second,20 second) .exec(http("order") .get("/E_Menu/order.action?people=5&time=2025-08-31") .headers(headers_0) ) // //回首頁 .pause(1 second,20 second) .exec(http("restaurant") .get("/E_Menu/restaurant.action") .headers(headers_0) ) //用戶信息 .pause(1 second,20 second) .exec(http("userinfo") .get("/E_Menu/userinfo?username=${username}") .headers(headers_0) ) //我的訂單 .pause(1 second,20 second) .exec(http("userorder") .get("/E_Menu/userorder.action?username=${username}") .headers(headers_0) ) //退出 .pause(1 second,20 second) .exec(http("exit") .get("/E_Menu/userlogin/login.jsp") .headers(headers_0))
setUp(scn.inject(atOnceUsers(100))).protocols(httpProtocol) }</pre>
寫完scala代碼,并且保證它在本地可以調試通過,下一步就是需要將代碼集成進jenkins;
先確保jenkins Gatling Plugin在jenkins上被安裝;
之后,寫入用來執行的shell調控代碼:#################### 環境準備 ####################
pwd
export IP="xx.xx.xx.xx"
export user="root"
export pwd="xxxxxxxxxx"
export tomcat_path="/root/apache-tomcat-7.0.56/bin"
export killtomcat="/root/killtomcat.sh"#################### 定義函數 ####################
kill_tomcat7(){
expect -c "
spawn ssh $user@$IP sh $killtomcat;
expect {
yes/no { send yes\r;exp_continue }
*password: { send $pwd\r }
};
expect eof;
"
}start_tomcat(){
expect -c "
spawn ssh $user@$IP sh $tomcat_path/$1;
expect {
yes/no { send yes\r;exp_continue }
*password: { send $pwd\r }
};
expect eof;
"
}############## tomcat壓測 #################### </p>
停止tomcat
kill_tomcat7
啟動tomcat
start_tomcat startup_non.sh
mvn gatling:execute -Dgatling.simulationClass=performance.Performance
#################### 完畢 ####################
參考截圖如下:
在完成調試改bug工作之后,可以嘗試運行一下,得到壓測結果 Gatling report ,然后可以進行相關的數據分析,這里不再贅述。
來自:https://testerhome.com/topics/3342
Jenkins 集成 Gatling Report參考截圖如下: