一、环境搭建
Gatling环境通过IntelliJ IDEA 工具整合Gatling插件,IntelliJ IDEA安装这里就不赘述了
PS:我这里使用的IDEA版本是ideaIC-2018.2.8、需要MAVEN环境以及JDK1.8环境
1.启动Intellij IEDA后创建maven工程
File->new->project,选择Maven,勾选Create from archetype,点击Add Archetype
点击Project SDK右边的new按钮指定JDK 1.8,选中添加后的archetype点击next
输入GroupId、ArtifactId点击Next
选择maven以及setting.xml,点击Next
点击Finish
此时会进入项目页面,点击Import Changes自动导入变化,此时需要等待Import Changes运行完成。
2.修改pom.xml文件加入Gatling运行插件,完整的pom.xml文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>GatlingTestDemo</groupId>
<artifactId>GatlingTest</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<encoding>UTF-8</encoding>
<gatling.version>3.1.2</gatling.version>
<gatling-maven-plugin.version>3.0.1</gatling-maven-plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>io.gatling.highcharts</groupId>
<artifactId>gatling-charts-highcharts</artifactId>
<version>${gatling.version}</version>
</dependency>
<dependency>
<groupId>io.gatling</groupId>
<artifactId>gatling-app</artifactId>
<version>${gatling.version}</version>
</dependency>
<dependency>
<groupId>io.gatling</groupId>
<artifactId>gatling-recorder</artifactId>
<version>${gatling.version}</version>
</dependency>
</dependencies>
<build>
<testSourceDirectory>src/test/scala</testSourceDirectory>
<plugins>
<plugin>
<groupId>io.gatling</groupId>
<artifactId>gatling-maven-plugin</artifactId>
<version>${gatling-maven-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- the name of the single Simulation class to run -->
<simulationClass>gatlingTestScripts.RecordedSimulation</simulationClass>
<!-- if the plugin should run multiple simulations sequentially -->
<runMultipleSimulations>true</runMultipleSimulations>
<!-- include filters, see dedicated section below -->
<includes>
<include>gatlingTestScripts.RecordedSimulation</include>
</includes>
<!-- exclude filters, see dedicated section below -->
<excludes>
<exclude></exclude>
</excludes>
<!-- to disable generating HTML reports -->
<noReports>false</noReports>
<!-- to only trigger generating HTML reports from the log file contained in folder parameter -->
<reportsOnly></reportsOnly>
<!-- short text that will be displayed in the HTML reports -->
<runDescription>design level training demo</runDescription>
<!-- skip executing this plugin -->
<skip>false</skip>
<!-- report failure in case of assertion failure, typically to fail CI pipeline -->
<failOnError>true</failOnError>
<!-- keep on executing multiple simulations even if one fails -->
<continueOnAssertionFailure>false</continueOnAssertionFailure>
<!-- report results to Jenkins JUnit support -->
<useOldJenkinsJUnitSupport>false</useOldJenkinsJUnitSupport>
<jvmArgs>
<!-- pass extra parameters to the Gatling JVM -->
<jvmArg>-Dfile.encoding=UTF-8</jvmArg>
</jvmArgs>
<!-- if above option should override the defaults instead of replacing them -->
<overrideJvmArgs>false</overrideJvmArgs>
<!-- if System properties from the maven JVM should be propagated to the Gatling forked one -->
<propagateSystemProperties>true</propagateSystemProperties>
<compilerJvmArgs>
<!-- pass extra parameters to the Compiler JVM -->
<compilerJvmArg></compilerJvmArg>
</compilerJvmArgs>
<!-- if above option should override the defaults instead of replacing them -->
<overrideCompilerJvmArgs>false</overrideCompilerJvmArgs>
<!-- extra options to be passed to scalac -->
<extraScalacOptions>
<extraScalacOption></extraScalacOption>
</extraScalacOptions>
<!-- if compiler should be disabled, typically another plugin has already compiled sources -->
<disableCompiler>false</disableCompiler>
<!-- where the simulations to be compiled are located -->
<simulationsFolder>${project.basedir}/src/test/scala</simulationsFolder>
<!-- where the test resources are located -->
<resourcesFolder>${project.basedir}/src/test/resources</resourcesFolder>
<!-- where the simulation log and the HTML reports will be generated -->
<resultsFolder>${project.basedir}/target/gatling/results</resultsFolder>
</configuration>
</plugin>
</plugins>
</build>
</project>
然后再Import Changes.
3.配置Run Configurations
点击Add Configuration,点击+号,选择maven,输入name:gatling:test,Command Line:gatling:test然后OK
注意:这里的Command line,输入gatling后会自动提示,并且这里的test和pom.xml中test一致。
4.运行Run Configuration
菜单栏Run->gatling:test,构建后ERROR信息:No simulations to run,就说明环境配置已成功,这是因为没有指定需要构建的测试脚本,录制脚本并在pom.xml中配置后执行就不会报错了。
二、脚本录制以及回放
1.安装Scala插件
录制前需要安装scala插件,安装过程如下:
从IDE菜单:Files->Settings->Plugins进入插件安装界面
点击:Install JetBrains Plugin…
选择Scala插件,点击Install按钮安装
我这里已安装,界面显示如下:
安装完成后重启IDEA,项目显示如下:
注意图中红框内部分,当scala安装成功后,显示如上图。
2.录制脚本
选择Recorder,右键,选择Run ‘Recorder’,填写package和ClassName,Package首字母要小写,localhost HTTP/HTTPS这里需要保证和浏览器代理的端口保持一致,不然录制时采集不到数据,
点击start
操作浏览器后,在配置的package下会生成对应的脚本。
我们录制一个登陆百度的脚本如下图:
3.回放脚本
在pom.xml中配置刚刚录制的脚本,Run->gatling:test,即可回放,回访后console显示Build Success。
三.报告查看
执行成功后,报告会展示在如下目录(在pom.xml中配置):
打开index.html
报告部分术语解释:
total:总请求次数
OK:成功数
KO:失败数
Req/s:每秒请求次数即吞吐量
Response Time Distribution:响应时间分布
Response Time Percentiles over Time: 百分比响应时间分布
Number of responses per second: 每秒的响应数
Number of requests per second: 每秒的请求数
总结:本文主要讲述Gatling环境搭建,脚本录制、回放以及报告查看,后续会深入学习更新,敬请期待。