ガトリング初めての経験

 

 

主な手順:

1.簡単なサービスを書くspringboot使用jdktest

仮想マシン内の2 jdktest使用のドッキングウィンドウの開始

3.提供されたスクリプトガトリングパフォーマンスを書くためにDSLを使用して、Scalaのプロジェクトを作成します。

4.実装と表示レポート

 

jdktestサービスの調製

 

インターフェイス名:/共通/チェック

1つの パラメータ:Userオブジェクト
 2  
。3  フォーマット:JSON
 。4  
。5  応答:
7 30歳以下で、結果:{ "コード":200、 "MSG": "成功"、 "データ":{ "名前": "こんにちは" 、 "年齢":18である}}
 8。 30よりも古い、結果:{ "コード" 400、 "MSG": " より古い30"、 "データ":{ "名前": "こんにちは"、 "年齢":50 }}

 

2.展開サービス

jdktestのjarパッケージを標識し、サーバーにアップロード(私はここにある仮想マシンで、ドッキングウィンドウが既にインストールされている)、jarファイルが同じディレクトリに作成されDockerfile

FROM primetoninc / JDK:1.8 
MAINTAINER 3404924705 @ qq.com 
jdktest ADD -0.0.2-SNAPSHOT.jarには/ usr / local / jdktest / 
RUNのMKDIRは/ usr / local / jdktest / ログ
RUNのはchmod -R 755は/ usr / local / jdktest 
WORKDIRは/ usr / local / jdktest 
EXPOSE 19801 
ENTRYPOINTのjavaの -jar jdktest-0.0.2-SNAPSHOT.jarに

次に、ミラーを作成するコンテナを起動し、サービスを実行するためのパスDockerfileで次のコマンドを実行します

(「」パスの後ろに、以下の画像の名前はjdktestであることに注意)ミラーを作成します
ドッカー構築する -t jdktest:1.2 
ファイル名を指定して実行(コンテナ名は以下のcjdktestで、ログは、ホスト・パス上にあるは/ usr / local /私/ )ログ
ドッカーRUNはは/ usr / local /ログインjdktest --name cjdktest jdktest /:1.2:8081 -vは/ usr / local /マイ/ログ-p 8081 -d 
 
 
ログファイルは、アクセス権の問題が表示されない場合、考えられる原因のcentos7権限を禁止するSELinuxのセキュリティモジュールは、
次のコマンドを使用することができます
ドッカーRUNはは/ usr / local / jdktest / =ログ--privileged:-vは/ usr / local /マイ/ログは8081:-p 8081 -d trueに --nameのcjdktestをjdktest:1.2

3.(スクリプトを実行するためにガトリングアクチュエータを使用した場合、その後、あなたはScalaの開発環境をインストールスキップすることができます)スクリプトのパフォーマンスを書きます

3.1開発環境のScalaをインストール

この実施形態は、Scalaの2.12.8に使用されています

 参考:

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

3.2 IDEAの設定、およびScalaのプロジェクトを作成します

Scalaのプラグインのインストール、インストール後に以下のスクリーンショットは完了です。

 

Scalaのプロジェクトを作成します。

プロジェクトを右クリックし、FrameWorkerサポート追加]を選択します
選択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的

其它的代表响应时间

 

おすすめ

転載: www.cnblogs.com/moonpool/p/11334609.html