文章目錄
- 一、前言
- 二、安裝插件
- 三、新建工程
- 四、運行結果
- 五、總結
一、前言
撰寫 Gatling 腳本需要搭建腳本開發環境,下面演示使用 IDEA 開發環境搭建腳本開發環境,
二、安裝插件
打開 IDEA,安裝 scala 插件,首次使用,隨便創建一個工程,進入idea頁面,按:Flie >Settings> Plugins,搜索 Scala,點擊“install”即可,install 為灰色即為已安裝,install 為綠色即為安裝可用(未安裝)狀態,安裝之后,restart 一下 IDEA 即可,
三、新建工程
-
創建新工程 Create New Project
-
選擇 maven,Create form archetype,Add Archetype
-
輸入內容

輸入內容:
GroupId : io.gatling.highcharts
ArtifactId : gatling-highcharts-maven-archetype
Version : 填入你想使用的版本號
查找版本號如:

之后的步驟就下一步下一步就行了,
工程目錄如下:

注意:中間 dunsanTest02 是自己寫的
目錄結構說明:
data:存放需要輸入的資料
scala:存放你撰寫的測驗腳本
Engine:右鍵運行跟運行
Recorder:右鍵運行會彈出錄制程式(不建議使用,建議手動開發腳本)
target:運行測驗之后,測驗報告存放在此
參考 Demo 代碼:
import io.gatling.core.scenario.Simulation
import io.gatling.core.Predef._
import io.gatling.http.Predef._
/**
* @author 7d
* @Date: 2021-04-27 20:08
* @Description: 簡單例子
* @version 1.0
*/
class dunsanTest02 extends Simulation {
object HomeIndex {
val home = exec(http("home_index") //設定請求名稱,可隨意定義
.get("http://localhost:8080/") //請求資源路徑
.check(status.is(200)) //判斷http status
)
}
// scenario里的引數:scenario name
//exec()里的引數就是我們的執行動作,http("本次請求的名稱").get("本次http get請求的地址")
val page = scenario("性能測驗,").exec(HomeIndex.home)
setUp(
//設定執行緒數 運行10秒 during 默認單位秒,如果要用微秒 during(10 millisecond)
page.inject(constantUsersPerSec(2) during (10))
// constantUsersPerSec 立馬啟動的用戶數,可以理解為并發數
)
}
//repeat(times,counterName)
// times:回圈次數
// counterName:計數器名稱,可選引數,可以用來當當前回圈下標值使用,從0開始
val scn = scenario("BaiduSimulation").repeat(100){
exec(http("baidu_home").get("/"))
}
//during(duration, counterName, exitASAP)
// duration:時長,默認單位秒,可以加單位milliseconds,表示毫秒
// counterName:計數器名稱,可選,很少使用
// exitASAP:默認為true,簡單的可以認為當這個為false的時候回圈直接跳出,可在回圈中進行控制是否繼續
/*
運行100秒 during 默認單位秒,如果要用微秒 during(100 millisecond)
*/
val scn = scenario("BaiduSimulation").during(100){
exec(http("baidu_home").get("/"))
}
四、運行結果
run 起來看看:

控制臺顯示:

運行結果如下:

打開 html 報告:

驗證寫的腳本是否可以放入 gatling 專案運行,效果如下:
liwen@ bin % ./gatling.sh
GATLING_HOME is set to /Users/liwen/Downloads/gatling-charts-highcharts-bundle-3.5.1
Choose a simulation number:
[0] computerdatabase.BasicSimulation
[1] computerdatabase.advanced.AdvancedSimulationStep01
[2] computerdatabase.advanced.AdvancedSimulationStep02
[3] computerdatabase.advanced.AdvancedSimulationStep03
[4] computerdatabase.advanced.AdvancedSimulationStep04
[5] computerdatabase.advanced.AdvancedSimulationStep05
[6] dunsanTest02
6
Select run description (optional)
tiaoshibiadu
Simulation dunsanTest02 started...
================================================================================
2021-04-27 18:58:28 0s elapsed
---- Requests ------------------------------------------------------------------
> Global (OK=2 KO=0 )
> home_index (OK=2 KO=0 )
---- 性能測驗,---------------------------------------------------------------------
[##########################################################################]100%
waiting: 0 / active: 0 / done: 2
================================================================================
Simulation dunsanTest02 completed in 0 seconds
Parsing log file(s)...
Parsing log file(s) done
Generating reports...
================================================================================
---- Global Information --------------------------------------------------------
> request count 2 (OK=2 KO=0 )
> min response time 379 (OK=379 KO=- )
> max response time 506 (OK=506 KO=- )
> mean response time 443 (OK=443 KO=- )
> std deviation 64 (OK=64 KO=- )
> response time 50th percentile 443 (OK=443 KO=- )
> response time 75th percentile 474 (OK=474 KO=- )
> response time 95th percentile 500 (OK=500 KO=- )
> response time 99th percentile 505 (OK=505 KO=- )
> mean requests/sec 2 (OK=2 KO=- )
---- Response Time Distribution ------------------------------------------------
> t < 800 ms 2 (100%)
> 800 ms < t < 1200 ms 0 ( 0%)
> t > 1200 ms 0 ( 0%)
> failed 0 ( 0%)
================================================================================
Reports generated in 0s.
Please open the following file: /Users/liwen/Downloads/gatling-charts-highcharts-bundle-3.5.1/results/dunsantest02-20210427105826635/index.html
五、總結
大家觀察出來,Gatling 在運行的時候不像 JMeter 一樣,在運行的時候輸入時間與加載方式,Gatling 控制都在腳本中撰寫,
更多操作請參考官方檔案:https://gatling.io/docs/current/
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/293152.html
標籤:其他
上一篇:前端常用工具大全
