Gatling 自动化压测实践

bger 8年前

Gatling是一款基于Scala 开发的高性能服务器性能测试工具,它主要用于对服务器进行负载等测试;想使用Gatling进行压测的原因之一是想体验一下Scala编程的感觉,玩一下;第二,工作上也确实有这样的需求;

压测工作简单来说就是利用压测应用,来测试一下服务器的响应性能参数;然后把这些工作全部自动化,集成到jenkins中来运行。

整个工作的子任务分解可以由下图来表示:

压测使用的是一个常见的web应用,该web应用的具体使用的业务场景如下:

针对该应用的压测Scala源代码如下:
文件名:performance.scala

package performance    import 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)  }

写完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压测 ####################

停止tomcat

kill_tomcat7

启动tomcat

start_tomcat startup_non.sh

mvn gatling:execute -Dgatling.simulationClass=performance.Performance

#################### 完毕 ####################

参考截图如下:

在完成调试改bug工作之后,可以尝试运行一下,得到压测结果 Gatling report ,然后可以进行相关的数据分析,这里不再赘述。
Jenkins 集成 Gatling Report参考截图如下:

来自:https://testerhome.com/topics/3342