性能测试工具Gatling介绍

1 介绍

Gatling是一款基于Scala 开发的高性能服务器性能测试工具,它主要用于对服务器进行负载等测试,并分析和测量服务器的各种性能指标。Gatling主要用于测量基于HTTP的服务器,比如Web应用程序,RESTful服务等,除此之外它拥有以下特点:

  • 支持Akka Actors 和 Async IO,从而能达到很高的性能
  • 支持实时生成Html动态轻量报表,从而使报表更易阅读和进行数据分析
  • 支持DSL脚本,从而使测试脚本更易开发与维护
  • 支持录制并生成测试脚本,从而可以方便的生成测试脚本
  • 支持导入HAR(Http Archive)并生成测试脚本
  • 支持Maven,Eclipse,IntelliJ等,以便于开发
  • 支持Jenkins,以便于进行持续集成
  • 支持插件,从而可以扩展其功能,比如可以扩展对其他协议的支持
  • 开源免费

Gatling适用的场景包括:测试需求经常改变,测试脚本需要经常维护;测试环境的客户机性能不强,但又希望发挥硬件的极限性能;能对测试脚本进行很好的版本管理,并通过CI进行持续的性能测试;希望测试结果轻量易读等。

2 与Jmeter对比:

图1和图2分别展现了二者在并发性能方面的表现。

图1,JMeter 2.8

图2,Gatling 1.3.2

3 使用、

3.1 下载:http://gatling.io/#/download,解压即可使用

3.2 文件目录介绍

  • bin目录下有2个脚本,gatling和recorder, gatling用来运行测试, recorder用来启动录制脚本的UI的(不推荐使用),
  • conf目录是关于Gatling自身的一些配置。
  • lib目录是Gatling自身依赖的库文件。
  • results目录用来存放测试报告的。
  • user-files目录是用来存放测试脚本的。

当运行gating脚本的时候,其会扫描user-files目录下的所有文件,列出其中所有的Simulation(一个测试类,里面可以包含任意多个测试场景)。选择其中一个Simulation,然后填写Simulation ID和运行描述,这个都是为报告描述服务的。

3.3 录制-

3.3.1 启动录制:

  • On Linux/Unix:

    $GATLING_HOME/bin/recorder.sh

  • On Windows:

    %GATLING_HOME%\bin\recorder.

3.3.2 界面:

3.3.3 录制配置

需要配置Internet的代理才能录制成功。录制完成之后代码保存在user-files/simulations/ 目录下

4 运行:

4.1 运行脚本

  • On Linux/Unix:

    $GATLING_HOME/bin/gatling.sh

  • On Windows:

    %GATLING_HOME%\bin\gatling.bat

4.2 运行的时候会把所有的能运行的场景(simulation)都列出来,选择想要运行的即可。

5 测试结果

Gatling测试报表基于HTML,并且在测试过程中业已生成,所以打开速度很快。而且,当把鼠标移动到不同数据轴上时,都会有弹出框显示这个点上详细的测试数据信息。这种动态显示数据的方式非常方便查看和分析数据。考虑到项目真实数据的不便,我将通过Gatling官方网站给出的示例报表进行说明。

Gatling的报表分为两类:GLOBAL和DETAILS,其中GLOBAL主要是请求相关的统计数据,比如每秒请求数,请求成功与失败数等;其中DETAILS主要是请求时间相关的统计数据,比如请求响应时间,请求响应延迟时间等。

图4 每秒请求数,

当鼠标放到图中任何一个点的时候,对应时间点上请求的详细数据就会以图中白色的弹出框的方式进行显示。在下面的请求响应延迟时间图里面也有同样的功能。

图5 请求响应延迟时间,

6 进阶

  脚本解析

package computerdatabase
// 1 包名

import io.gatling.core.Predef._
// 2必须导入的

import io.gatling.http.Predef._

import scala.concurrent.duration._

class
BasicSimulation
extends Simulation {
// 3 类声明,必须继承Simulation

val httpConf
= http // 4 所有Http请求普遍配置

.baseURL("http://computer-database.gatling.io")
// 5 base URL

.acceptHeader("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
// 6 请求头

.doNotTrackHeader("1")

.acceptLanguageHeader("en-US,en;q=0.5")

.acceptEncodingHeader("gzip, deflate")

.userAgentHeader("Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0")

val scn
= scenario("BasicSimulation")
// 7 定义场景

.exec(http("request_1") 
// 8 http请求名称 request_1,这个名称最后会显示在报告中

.get("/"))
// 9 get请求方法

.pause(5)
// 10 暂停/思考时间 5s

setUp( // 11 建立场景

scn.inject(atOnceUsers(1))
// 12 声明注入一个用户

).protocols(httpConf) // 13 之前声明的Http请求配置

}

6.1 分离场景:分离操作:如浏览、搜索、编辑操作

object Search{valsearch=exec(http("Home")//
let‘s give proper names, as they are displayed in the reports

.get("/")).pause(7).exec(http("Search").get("/computers?f=macbook")).pause(2).exec(http("Select").get("/computers/6")).pause(3)}objectBrowse{valbrowse=???}objectEdit{valedit=???}

We can now rewrite our scenario using these reusable business processes:

valscn=scenario("Scenario Name").exec(Search.search,Browse.browse,Edit.edit)

6.2  设置常规用户和管理员账户

val users=scenario("Users").exec(Search.search,Browse.browse)

val admins=scenario("Admins").exec(Search.search,Browse.browse,Edit.edit)

6.3  设置虚拟用户

setUp(users.inject(atOnceUsers(10)).protocols(httpConf))

6.4 设置上升压力,即每隔一段时间启动多少用户

setUp(users.inject(rampUsers(10)over(10seconds)),admins.inject(rampUsers(2)over(10seconds))).protocols(httpConf)

10s 内启动10个users和2个admin

6.5 参数化 or 动态数据

6.5.1 创建文件:在user-files/data文件夹下创建cvs文件,如:search.csv ,内容如下:

searchCriterion,searchComputerName

Macbook,MacBook Pro

eee,ASUS Eee PC 1005PE

6.5.2 使用文件:

objectSearch{

valfeeder=csv("search.csv").random// 1, 2

valsearch=exec(http("Home").get("/")).pause(1).feed(feeder)//
3

.exec(http("Search").get("/computers?f=${searchCriterion}")// 4

.check(css("a:contains(‘${searchComputerName}‘)","href").saveAs("computerURL")))//
5

.pause(1).exec(http("Select").get("${computerURL}"))//
6

.pause(1)}

6.5.3 解释:

Explanations:

  1. First we create a feeder from a csv file with the following columns: searchCriterionsearchComputerName.
  2. As the default feeder strategy is queue, we will use the random strategy for this test to avoid feeder starvation.
  3. Every time a user reaches the feed step, it picks a random record from the feeder. This user has two new session attributes named searchCriterionsearchComputerName.
  4. We use session data through Gatling’s EL to parametrize the search.
  5. We use a CSS selector with an EL to capture a part of the HTML response, here a hyperlink, and save it in the user session with the name computerURL. Note how Scala triple quotes are handy: you don’t have to escape
    double quotes inside the regex with backslashes.
  6. We use the previously saved hyperlink to get a specific page.

6.6 循环调用

In the browse process we have a lot of repetition when iterating through the pages. We have four times the same request with a different query param value. Can we change this to not violate the DRY principle?

First we will extract the repeated exec block to a function. Indeed, Simulation‘s are plain Scala classes so we can use all the
power of the language if needed:

objectBrowse{defgotoPage(page:Int)=exec(http("Page
"+page).get("/computers?p="+page)).pause(1)valbrowse=exec(gotoPage(0),gotoPage(1),gotoPage(2),gotoPage(3),gotoPage(4))}

We can now call this function and pass the desired page number. But we still have repetition, it’s time to introduce another builtin structure:

objectBrowse{valbrowse=repeat(5,"n"){//
1exec(http("Page ${n}").get("/computers?p=${n}"))// 2.pause(1)}}

Explanations:

  1. The repeat builtin is a loop resolved at runtime. It takes the number of repetitions and, optionally, the name of the counter that’s stored in the user’s Session.
  2. As we force the counter name we can use it in Gatling EL and access the nth page

6.7 结果校验:()

importjava.util.concurrent.ThreadLocalRandom// 1

valedit=exec(http("Form").get("/computers/new")).pause(1).exec(http("Post").post("/computers").check(status.is(session=>200+ThreadLocalRandom.current.nextInt(2))))//
2

Explanations:

  1. First we import ThreadLocalRandom, to generate random values.
  2. We do a check on a condition that’s been customized with a lambda. It will be evaluated every time a user executes the request and randomly return 200 or 201. As response status is 200, the check will fail
    randomly.

To handle this random failure we use the tryMax and exitHereIfFailed constructs as follow:

valtryMaxEdit=tryMax(2){// 1exec(edit)}.exitHereIfFailed//
2

Explanations:

  1. tryMax tries a given block up to n times. Here we try a maximum of two times.
  2. If all tries failed, the user exits the whole scenario due to exitHereIfFailed.

7 其他

7.1 可以与CI集成

7.2 实时监控: 支持用户数和请求数的实时监控

7.3 服务器监控功能(System  under test metrics)。   http://gatling.io/docs/2.1.7/realtime_monitoring/index.html

参考链接:

1)13年文章:新一代服务性能测试工具Gatling:http://www.infoq.com/cn/articles/new-generation-server-testing-tool-gatling/

2) 官网介绍:http://gatling.io

3) 官网文档地址:http://gatling.io/#/docs

时间: 2024-08-24 01:10:55

性能测试工具Gatling介绍的相关文章

负载,性能测试工具-Gatling

前言 Gatling Gatling是一款功能强大的负载测试工具,它为易于使用,高可维护性和高性能而设计. 开箱即用,Gatling由于对HTTP协议的出色支持,使其成为负载测试任何HTTP服务器的首选工具.由于核心引擎实际上是协议不可知的,因此完全可以实现对其他协议的支持.例如,Gatling目前还提供JMS支持. 代码自定义并且场景资源有效是Gatling的两个基础.并且拥有富有表现力的DSL,自我解释的场景,易于维护,可以保存在版本控制系统中的优点. 只要底层协议(如HTTP)可以以非阻塞

网络性能测试工具Iperf介绍

[概要]Iperf是一款网络性能测试工具,可以方便的用它进行SDN网络带宽和网络质量的测试,Iperf支持协议.定时.缓冲区等参数的配置调整,报告TCP/UDP最大带宽.延迟抖动.数据包丢失等统计信息. 1 Iperf安装 Iperf安装方法有多种,可以下载源码编译安装,也可以直接使用编译好的二进制版本,在ubuntu下安装使用iperf尤为简单,apt-get install iperf 即可,值得一提的是Mininet自带Iperf,在SDN网络上测试比较便捷. 2 工作原理 使用Iperf

前端性能测试工具hiper介绍

对前端性能测试工具还不了解,在请教了旁边的前端同事后学习到了简单的工具,在这里总结下. 前端的性能测试测什么? 以下是复制: 渲染引擎工作流 dom树构建:从html标签的解析开始,将各种标签解析为dom树中的各个节点,标签和dom树的中的节点是一一对应关系. 渲染树构建:将CSS和style标签中的样式信息解析为渲染树,渲染树由一些包含有颜色和大小等属性的矩形组成,它们将被按照正确的顺序显示到屏幕上. 渲染树布局和绘制:渲染树确定各个dom节点在屏幕中单确切位置,根据渲染树中的颜色等信息绘制出

Android性能测试工具Emmagee介绍

Emmagee介绍 Emmagee是监控指定被测应用在使用过程中占用机器的CPU.内存.流量资源的性能测试小工具.该工具的优势在于如同windows系统性能监视器类似,它提供的是数据采集的功能,而行为则基于用户真实的应用操作开源地址:https://github.com/NetEase/Emmagee/releases支持SDK:Android2.2以及以上版本 为什么使用Emmagee?1.开源2.使用方便3.可以监控单个应用性能4.浮窗显示实时展示数据5.CSV格式保存性能数据,方便转换为其

性能测试工具Gatling - 设置Recorder

Gatling自带的Recorder,可以大大节省我们书写scenario的时间. 用法和selenium的IDE类似,作为一个代理服务器在browser和application之间做桥梁作用,从而将操作过程直接转换为scenario. 1. 运行 直接执行bin目录下的recorder.bat或者recorder.sh文件即可 2. GUI界面 需要配置的有 Listening Port, HTTP一个,HTTPS一个,默认都为localhost.如果需要一个远程的代理,那么就在Outgoin

Jdk内置性能测试工具的介绍

(一) JConsole JConsole使用JVM的可扩展性Java管理扩展(JMX)工具来提供关于运行于Java平台的应用程序的性能和资源消耗的信息. 在J2SE 5.0软件中,你需要启动使用-Dcom.sun.management.jmxremote选项监控的应用程序.注意:在Java SE 6软件中,不再有这一要求.当启动该应用程序时,不需要特定的命令行选项. 在生产系统中的应用 JConsole启动一个在被观察的Java程序的JVM内部的JMX代理.运行另外一部分代码仅有一点极微弱的影

web性能测试的新利器 - Gatling 介绍

转载:http://www.51testing.com/html/10/26810-852956.html 最近发现了一个新的性能测试工具Gatling,貌似比Jmeter还好玩.这几天就先简单介绍一下. 该开源项目的地址是:https://github.com/excilys/gatling/wiki 第一步,让我们看看简单的入门 1. 下载 你可以选择下载 .tar.gz 或者 .zip 包:https://github.com/excilys/gatling/wiki/Downloads

Linux 性能测试工具Lmbench详解

Linux 性能测试工具Lmbench详解 2010-06-04 16:07 佚名 评测中心 字号:T | T Lmbench 是一套简易可移植的,符合ANSI/C 标准为UNIX/POSIX 而制定的微型测评工具.一般来说,它衡量两个关键特征:反应时间和带宽.Lmbench 旨在使系统开发者深入了解关键操作的基础成本. AD:2014WOT全球软件技术峰会北京站 课程视频发布 Linux 性能测试工具Lmbench 是一套简易可移植的,符合ANSI/C 标准为UNIX/POSIX 而制定的微型

Android性能测试 一些适用于Android Studio的代码审查和性能测试工具

导言: Android应用在CPU占用,内存消耗方面的性能指标是影响产品质量的重要因素,由于QQ管家,360手机助手等应用都提供直观的内存消耗,流量监控功能,致使用户比以往更加关注软件的性能,并以此进行软件选用的决策. 目前,已经有很多可以监控android app 性能的工具可以供开发人员使用,如:基于Eclipse插件体系的MAT,其通过生成.hprof文件对内存泄露情况进行排查:内存检测工具APT:提供CPU利用率实时曲线图,方便对比测试内存泄露问题[图0-1] 图 0-1  通过APT实