主要步驟:
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的
其它的代表響應時間