主要步骤:
1. 利用springboot编写了一个简单的服务jdktest
2.将jdktest利用docker在虚拟机中启动
3.创建一个scala工程,利用gatling提供的DSL编写性能脚本
4.执行并查看报告
1.编写jdktest服务
接口名称:/common/check
1 参数:一个User对象
2
3 格式: json
4
5 响应:
7 年龄小于等于30,结果:{"code":200,"msg":"success","data":{"name":"hello","age":18}}
8 年龄大于30,结果:{"code":400,"msg":"年龄大于30","data":{"name":"hello","age":50}}
2.部署服务
将jdktest打成jar包,并上传到服务器(我这里是虚拟机,并且已经安装了docker)上,在jar同级目录下创建Dockerfile
FROM primetoninc/jdk:1.8
MAINTAINER 3404924705@qq.com
ADD jdktest-0.0.2-SNAPSHOT.jar /usr/local/jdktest/
RUN mkdir /usr/local/jdktest/log
RUN chmod -R 755 /usr/local/jdktest
WORKDIR /usr/local/jdktest
EXPOSE 19801
ENTRYPOINT java -jar jdktest-0.0.2-SNAPSHOT.jar
然后在Dockerfile所在路径执行下面的命令,来创建镜像、启动容器以及运行服务
创建镜像(注意后面的路径“.”,下面的镜像名称是jdktest)
docker build -t jdktest:1.2 .
运行容器(下面的容器名称是cjdktest,宿主机上的日志路径是/usr/local/my/log)
docker run -d -p 8081:8081 -v /usr/local/my/log:/usr/local/jdktest/log --name cjdktest jdktest:1.2
如果出现log文件没有权限的问题,可能原因centos7中安全模块selinux把权限禁掉了
可以使用下面的命令
docker run -d -p 8081:8081 -v /usr/local/my/log:/usr/local/jdktest/log --privileged=true --name cjdktest jdktest:1.2
3.编写性能脚本(如果使用Gatling执行器来执行脚本,那么就可以跳过安装scala的开发环境)
3.1 安装scala的开发环境
本例中使用的是scala 2.12.8
参考:
https://www.runoob.com/scala/scala-install.html
3.2 配置IDEA,并创建scala工程
安装scala插件,下面安装完成后的截图。
创建scala项目
右击项目,选择Add FrameWorker support
选择maven
修改pom.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <project xmlns="http://maven.apache.org/POM/4.0.0"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5 <modelVersion>4.0.0</modelVersion>
6
7 <groupId>groupId</groupId>
8 <artifactId>gatlingtest</artifactId>
9 <version>1.0-SNAPSHOT</version>
10 <properties>
11 <maven.compiler.source>1.8</maven.compiler.source>
12 <maven.compiler.target>1.8</maven.compiler.target>
13 <encoding>UTF-8</encoding>
14
15 <gatling.version>3.0.3</gatling.version>
16 <gatling-maven-plugin.version>3.0.1</gatling-maven-plugin.version>
17 </properties>
18
19 <dependencies>
20 <dependency>
21 <groupId>io.gatling.highcharts</groupId>
22 <artifactId>gatling-charts-highcharts</artifactId>
23 <version>${gatling.version}</version>
24 </dependency>
25 <dependency>
26 <groupId>io.gatling</groupId>
27 <artifactId>gatling-app</artifactId>
28 <version>${gatling.version}</version>
29 </dependency>
30 <dependency>
31 <groupId>io.gatling</groupId>
32 <artifactId>gatling-recorder</artifactId>
33 <version>${gatling.version}</version>
34 </dependency>
35 </dependencies>
36
37
38 <build>
39 <plugins>
40 <plugin>
41 <groupId>org.scala-tools</groupId>
42 <artifactId>maven-scala-plugin</artifactId>
43 <version>2.15.2</version>
44 <executions>
45 <execution>
46 <goals>
47 <goal>compile</goal>
48 <goal>testCompile</goal>
49 </goals>
50 </execution>
51 </executions>
52 </plugin>
53 <plugin>
54 <groupId>io.gatling</groupId>
55 <artifactId>gatling-maven-plugin</artifactId>
56 <version>3.0.1</version>
57 <configuration>
58 <!-- 测试脚本 -->
59 <simulationClass>computerdatabase.ApiJdkTest</simulationClass>
60 <!-- 结果输出地址 -->
61 <resultsFolder>D:\z_softinstall\intellijidea\IdeaProjects\gatlingtest\report</resultsFolder>
62 </configuration>
63 </plugin>
64 </plugins>
65 </build>
66
67 </project>
注意修改gatling-maven-plugin的配置,simulationClass代表执行的是哪个脚本,resultsFolder表示报告的存放目录
将src目录下的文件和文件夹删除
选中src目录右击 选中MarkDirectory as 再选中Sources root
在src下面创建一个package 命名为computerdatabase(这个包名是gatling例子中的名称,同时也是gatling执行器中例子脚本存放的目录名称)
在该目录下创建一个scala文件 ApiJdkTest(这个名称要和pom.xml中simulationClass的配置对应起来,不然执行时会报找不到测试脚本)
编写脚本
1 package computerdatabase
2
3 import io.gatling.core.Predef._
4 import io.gatling.http.Predef._
5 import scala.concurrent.duration._
6
7 class ApiJdkTest extends Simulation {
8
9 //给年龄字段添加一个随机数Feeder
10 //使用Feeder的原因:按照gatling的官方文档的解释,由于DSL会预编译,在整个执行过程中是静态的。因此Random在运行过程中就已经静态化了,不会再执行。
11 //参考:https://www.jianshu.com/p/7f7a57a8c2bb
12 val randomIdFeeder =
13 Iterator.continually(Map("age" ->
14 (scala.util.Random.nextInt(50))))
15
16 //设置请求的根路径
17 //这里是在虚拟机中jdktest的服务地址
18 val httpConf = http.baseUrl("http://192.168.1.3:19801")
19
20 /*
21 运行秒数 during 默认单位秒,如果要用微秒 during(100 millisecond)
22 下面内容可以参考:
23 脚本结果:https://gatling.io/docs/current/general/concepts/
24 post请求:https://gatling.io/docs/current/http/http_request/
25 check:https://gatling.io/docs/current/http/http_check/#http-check
26 */
27 val scn = scenario("JdkTest")
28 .during(100) {
29 forever(
30 feed(randomIdFeeder)
31 .exec(http("UserCheck")
32 .post("/common/check")
33 .header("Content-Type", "application/json")
34 .body(StringBody(s"""{"name":"hello","age":""" + "${age}" +"""}""")).asJson
35 .check(status.is(200))
36 .check(jsonPath("$.code").is("200"))
37 .check(jsonPath("$.msg").is("success"))
38 )
39 )
40 }
41 /*
42 设置并发
43 参考
44 https://gatling.io/docs/current/general/simulation_setup/
45 */
46 setUp(
47 scn.inject(
48 //在20秒内以线性斜坡方式完成注入20的用户,不是一下子注入20个,而是逐步增加(线性)。可以从报告看出。
49 rampUsers(20) during (20 seconds)
50 ).protocols(httpConf)
51 )
52 }
执行脚本
View-》Tool Windows-》Maven Projects 选择gatling:test
执行完成后,会提示报告存放路径
查看报告
下面是成功和失败(这里只包含check结果)的统计结果
下面是响应
橙色的线表示用户数,可以看到从左侧开始,是逐步将用户数增加到20的
其它的代表响应时间
来源:oschina
链接:https://my.oschina.net/u/4317177/blog/3433754