性能测试工具 Gatling 自动化压测实践

网球王子 · 2015年09月16日 · 最后由 郑郑 回复于 2020年06月09日 · 2946 次阅读
本帖已被设为精华帖!

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 参考截图如下:

共收到 14 条回复 时间 点赞

nice a.

Jenkins 的 Gatling 插件效果不错哦。。

为什么有些属性我这边都用不了,headers、formParam 都找不到,jar 都导了,官方给的 demo 导进去很多都报错,我这边版本是 2.1.7 的,是不是过时用别的方法替代了

#3 楼 @13651969749 我用的版本是 2.2.0

写的非常不错。

刚好公司要搞自动化集成,可以试试了

一般用什么 IDE 来编写?

#4 楼 @princeqjzh 2.2.0 在 eclipse 中没有适配的插件?

#7 楼 @tavisdxh 我当时就用普通文本编辑器写的

#8 楼 @tracy_ly 暂时还没发现 eclipse 中的 gatling 插件

/root/apache-tomcat-7.0.56/bin,请问楼主,这下面是放啥的

ylq 回复

是 tomcat 的可执行文件

请问楼主 有分布式的解决方案吗?单机施压的量很有限啊

楼主,你这个 report 是怎么集成的?能给个详细的方案说明吗?要装啥插件吗

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册