Gatling first experience

 

 

The main steps:

1. Using springboot write a simple service jdktest

2. jdktest use docker start in a virtual machine

3. Create a scala project, use DSL to write a script gatling performance provided

4. Implementation and view reports

 

1. Preparation of jdktest Service

 

Interface Name: / common / check

1  Parameters: a User object
 2  
. 3  format: JSON
 . 4  
. 5  Response: 
7 Age 30 or less, result: { "code": 200, "msg": "success", "data": { "name": "hello" , "Age": 18 is }}
 . 8 older than 30, the result: { "code": 400, "msg": " older than 30", "data": { "name": "hello", "age": 50 }}

 

2. Deployment Services

The jdktest labeled jar package and uploaded to the server (I have here is a virtual machine, and a docker already installed), the jar is created in the same directory 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

Then execute the following command in the path Dockerfile where to create the mirror, start the container and run the service

Create a mirror (path behind noted that the following image name is jdktest ".") 
Docker Build -t jdktest: 1.2 . 
Run (container name is below cjdktest, the log is on the host path / usr / local / my / log) 
Docker RUN -d -p 8081: 1.2: 8081 -v / usr / local / My / log: / usr / local / jdktest log --name cjdktest jdktest / 
 
 
if the log file does not appear permissions problem, possible causes centos7 selinux security module to ban the authority 
can use the following command 
Docker RUN -d -p 8081: 8081 -v / usr / local / My / log: / usr / local / jdktest / = log --privileged to true --name cjdktest jdktest: 1.2

3. Write a script performance (if using a Gatling actuator to execute the script, then you can skip installing the scala development environment)

3.1 development environment install scala

This embodiment is used in scala 2.12.8

 reference:

https://www.runoob.com/scala/scala-install.html

3.2 Configuring the IDEA, and create a scala project

Scala plug-in installation, the following screenshots after installation is complete.

 

Create a scala project

Right-click the project and select Add FrameWorker support
choice maven

Modify 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的

其它的代表响应时间

 

Guess you like

Origin www.cnblogs.com/moonpool/p/11334609.html