負載,效能測試工具-Gatling

前言
Gatling
Gatling是一款功能強大的負載測試工具,它為易於使用,高可維護性和高效能而設計。
開箱即用,Gatling由於對HTTP協議的出色支援,使其成為負載測試任何HTTP伺服器的首選工具。由於核心引擎實際上是協議不可知的,因此完全可以實現對其他協議的支援。例如,Gatling目前還提供JMS支援。
程式碼自定義 並且 場景資源有效 是Gatling的兩個基礎。並且擁有富有表現力的 ofollow,noindex" target="_blank"> DSL ,自我解釋的場景,易於維護,可以儲存在版本控制系統中的優點。
只要底層協議(如HTTP)可以以非阻塞方式實現,Gatling的體系結構就是非同步的。這種架構允許我們將虛擬使用者實現為訊息而不是專用執行緒,這使得硬體要求不需要很苛刻。因此,執行數千個併發虛擬使用者不是問題。
使用理由
-
避免崩潰 :Gatling可以幫助您預測緩慢的響應時間和奔潰。
-
縮短產品上市時間 :Gatling 在開發週期的早期檢測到效能問題和錯誤
-
增強使用者體驗 :Gatling可以準確描述您最慢的使用者體驗
-
提升您的業務 :加特林可以防止您的業務成為自身成功的犧牲品
效能測試
效能測試是什麼意思?
Web應用程式 的效能測試包括:
-
模擬大量 具有 複雜行為的使用者 ;
-
收集 和彙總 所有請求的響應時間 ;
-
建立報告 和 分析資料 ;
編寫測試場景,自動化測試
Gatling的 類似程式碼的指令碼 使您可以 輕鬆維護測試場景 ,並在持續交付管道中輕鬆實現自動化。
我們開發了自己的 領域特定語言(DSL) ,以便讓 每個人都能輕鬆閱讀 您的場景。
分析並調查您的應用程式的瓶頸
Gatling是一個 功能強大的工具 :只需幾臺計算機,您就可以 在Web應用程式上模擬 每秒數十萬個請求, 並獲得 高精度指標 。
在測試結束時,Gatling會自動生成 詳盡,動態 且 豐富多彩的報告 。
平均值和平均資料是 不夠的 :使用Gatling,您可以得到 適當 的 響應時間 百分位數 。 不要讓最慢的使用者落後!
安裝
準備工作
Java版本
Gatling主要針對Oracle提供的JDK8軟體包進行測試。Gatling需要 JDK8 (因此不支援JDK9 atm)。Gatling應該適用於任何JDK8更新,但我們建議您使用最新版本。
IPv4 vs IPv6
發現IPv6(預設情況下在Java上啟用)有時會導致一些效能問題,因此啟動指令碼會使用以下選項禁用它:
-Djava.net.preferIPv4Stack=true -Djava.net.preferIPv6Addresses=false
如果您確實需要優先選擇IPv6,請編輯啟動指令碼。
作業系統調整
以下說明摘自偉大的Riak文件。 有關更多詳細資訊或OS X的說明,請參閱“ basho/basho_docs/blob/master/content/riak/kv/2.2.3/using/performance/open-files-limit.md/" rel="nofollow,noindex" target="_blank"> 開啟檔案限制” 和“ 核心和網路調整” 部分。
在正常操作期間,Gatling會消耗大量的開啟檔案控制代碼。通常,作業系統會限制此數量,因此您可能必須在所選作業系統中調整一些選項,以便可以開啟 許多 新套接字並實現高負載。
開啟檔案限制
大多數作業系統都可以使用該命令更改開啟檔案限制。例: ulimit -n
$ ulimit -n 65536
但是,這隻會更改當前shell會話的限制。在系統範圍內,永久性地更改限制因系統而異。
要 為系統的所有使用者 永久設定軟值和硬值 , 以允許最多65536個開啟檔案; 編輯 /etc/security/limits.conf
並附加以下兩行:
*softnofile65535 *hardnofile65535
儲存檔案。啟動新會話以使限制生效。您現在可以驗證是否正確設定了限制。 ulimit -a
對於Debian和Ubuntu,您應該啟用PAM使用者限制。為此,請新增: session required pam_limits.so
-
/etc/pam.d/common-session
-
/etc/pam.d/common-session-noninteractive
如果檔案存在 -
/etc/pam.d/sshd
如果您通過SSH訪問該計算機
另外,如果訪問通過SSH的機器,一定要具有在 UseLogin yes``/etc/ssh/sshd_config
要進行更多調整,您可能需要執行以下操作:
# more ports for testing sudo sysctl -w net.ipv4.ip_local_port_range="1025 65535" # increase the maximum number of possible open file descriptors: echo 300000 | sudo tee /proc/sys/fs/nr_open echo 300000 | sudo tee /proc/sys/fs/file-max
核心和網路調優
考慮調整核心和網路,並在/etc/sysctl.conf中新增以下這些設定:
net.ipv4.tcp_max_syn_backlog = 40000 net.core.somaxconn = 40000 net.core.wmem_default = 8388608 net.core.rmem_default = 8388608 net.ipv4.tcp_sack = 1 net.ipv4.tcp_window_scaling = 1 net.ipv4.tcp_fin_timeout = 15 net.ipv4.tcp_keepalive_intvl = 30 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_moderate_rcvbuf = 1 net.core.rmem_max = 134217728 net.core.wmem_max = 134217728 net.ipv4.tcp_mem= 134217728 134217728 134217728 net.ipv4.tcp_rmem = 4096 277750 134217728 net.ipv4.tcp_wmem = 4096 277750 134217728 net.core.netdev_max_backlog = 300000
離線下載
您可以離線下載Gatling。然後,您只需要一個文字編輯器(Scala語法高亮)來編輯模擬,您就可以從命令列啟動Gatling。
檢視我們的 下載頁面 以獲取下載連結。
將下載的包解壓縮到您選擇的資料夾中。使用 bin
目錄中的指令碼啟動Gatling和Recorder。
Windows使用者:我們建議您不要將Gatling放在“ Programs”資料夾中,因為可能存在許可權和路徑問題。
要執行Gatling,您需要安裝JDK。加特林至少需要 JDK8 。然後,我們建議您使用最新版本。
-
Gatling啟動指令碼和Gatling maven外掛會在
JAVA_HOME
env var中設定它。與OSX一樣,作業系統有自己的棘手方法來發現要執行的Java版本,因此最終可能會執行與您告訴您的版本不同的版本。如果您遇到奇怪的錯誤,並且您希望執行JDK8,則可能需要明確設定
JAVA_HOME
。java -version``Unsupported major.minor version 51.0
使用構建工具
Maven
Gatling提供了一個名為gatling-maven-plugin的官方maven 外掛。這個外掛允許您編譯Scala程式碼並啟動Gatling模擬。
有了這個外掛,Gatling可以在構建專案時啟動,例如使用您最喜歡的CI解決方案。
Versions
檢視 Maven Central 上的可用版本。
請注意,OSS使用者未記錄里程碑(M版本),僅針對專業版客戶釋出。
Setup
在你的 pom.xml
,新增:
<dependencies> <dependency> <groupId>io.gatling.highcharts</groupId> <artifactId>gatling-charts-highcharts</artifactId> <version>MANUALLY_REPLACE_WITH_LATEST_VERSION</version> <scope>test</scope> </dependency> </dependencies> <plugin> <groupId>io.gatling</groupId> <artifactId>gatling-maven-plugin</artifactId> <version>MANUALLY_REPLACE_WITH_LATEST_VERSION</version> </plugin>
演示樣本
您可以在Gatling的Github組織中找到 gatling-sbt-plugin-demo 的 示例專案 。
您還可以使用 gatling-highcharts-maven-archetype 來引導專案。
Usage
您可以直接啟動gatling-maven-plugin,其 test
目標是:
mvn gatling:test
它預設繫結到 integration-test
階段。
Configuration
下面的例子顯示了預設值(所以不要指定你沒有覆蓋的選項!!!):
<configuration> <simulationClass>foo.Bar</simulationClass><!-- the name of the single Simulation class to run --> <runMultipleSimulations>false</runMultipleSimulations><!-- if the plugin should run multiple simulations sequentially --> <includes><!-- include filters, see dedicated section below --> <include></include> </includes> <excludes><!-- exclude filters, see dedicated section below --> <exclude></exclude> </excludes> <noReports>false</noReports><!-- to disable generating HTML reports --> <reportsOnly></reportsOnly><!-- to only trigger generating HTML reports from the log file contained in folder parameter --> <runDescription>This-is-the-run-description</runDescription><!-- short text that will be displayed in the HTML reports --> <skip>false</skip><!-- skip executing this plugin --> <failOnError>true</failOnError><!-- report failure in case of assertion failure, typically to fail CI pipeline --> <continueOnAssertionFailure>false</continueOnAssertionFailure><!-- keep on executing multiple simulations even if one fails --> <useOldJenkinsJUnitSupport>false</useOldJenkinsJUnitSupport><!-- report results to Jenkins JUnit support (workaround until we manage to get Gatling support into Jenkins) --> <jvmArgs> <jvmArg>-DmyExtraParam=foo</jvmArg><!-- pass extra parameters to the Gatling JVM --> </jvmArgs> <overrideJvmArgs>false</overrideJvmArgs><!-- if above option should override the defaults instead of replacing them --> <propagateSystemProperties>true</propagateSystemProperties><!-- if System properties from the maven JVM should be propagated to the Gatling forked one --> <compilerJvmArgs> <compilerJvmArg>-DmyExtraParam=foo</compilerJvmArg><!-- pass extra parameters to the Compiler JVM --> </compilerJvmArgs> <overrideCompilerJvmArgs>false</overrideCompilerJvmArgs><!-- if above option should override the defaults instead of replacing them --> <extraScalacOptions><!-- extra options to be passed to scalac --> <extraScalacOption></extraScalacOption> </extraScalacOptions> <disableCompiler>false</disableCompiler><!-- if compiler should be disabled, typically because another plugin has already compiled sources --> <simulationsFolder>${project.basedir}/src/test/scala</simulationsFolder> <!-- where the simulations to be compiled are located --> <resourcesFolder>${project.basedir}/src/test/resources</resourcesFolder> <!-- where the test resources are located --> <resultsFolder>${project.basedir}/target/gatling</resultsFolder><!-- where the simulation log and the HTML reports will be generated --> </configuration>
包含/排除過濾器
執行多個模擬時,您可以使用 includes
和 excludes
過濾器控制哪些模擬將被觸發。那些使用ant模式語法並與類名匹配。另請注意,這些過濾器僅適用於從設定外掛的專案中的源編譯的類。
<configuration> <!--...--> <runMultipleSimulations>true</runMultipleSimulations> <includes> <include>my.package.*</include> </includes> <excludes> <exclude>my.package.IgnoredSimulation</exclude> </excludes> </configuration>
注意:過濾器的順序對執行順序沒有影響,模擬將按字母順序按類名排序。
禁用編譯器
預設情況下,gatling-maven-plugin負責編譯Scala程式碼,因此您可以直接執行。 mvn gatling:execute
然後,由於某種原因,你可能想要另一個外掛,如 scala-maven-plugin 或 scalor-maven-plugin ,負責編譯。然後,您可以禁用Gatling編譯器,這樣就不會編譯兩次:
<configuration> <disableCompiler>true</disableCompiler> </configuration>
覆蓋logback.xml檔案
您可以 logback-test.xml
擁有優先於嵌入 logback.xml
檔案的優先順序,也可以新增JVM選項 -Dlogback.configurationFile=myFilePath
。
使用IDE
您可以使用任何Scala語法高亮的文字編輯器編輯Simulation類。但如果您是開發人員,您很可能希望將自己喜歡的IDE與Gatling一起使用。
Gatling正式支援IntelliJ IDEA和eclipse。
IntelliJ IDEA
您需要安裝社群版中提供的Scala外掛。然後,您就可以使用Scala源直接匯入常規maven或sbt專案。
Eclipse
您必須安裝 ScalaIDE ,最好是最新版本,甚至是里程碑。
遺憾的是,截至今天,ScalaIDE僅適用於Eclipse 4.7(Oxygen),並且與更現代的版本不相容。
請注意,Eclipse 4.7不支援Java 9+,因此如果您的計算機上安裝了多個Java,則可能必須強制使用JVM eclipse.ini
,例如:
-vm /Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home/bin/java
SBT
如果你正在使用sbt,你可以使用 sbteclipse 來生成eclipse專案配置。
Maven
如果您正在使用maven,則可以將 scala-maven-plugin 與 m2eclipse-scala 結合使用。前者將編譯Scala程式碼,後者將執行ScalaIDE和m2e(maven的eclipse外掛)之間的整合。
您必須在pom.xml中新增以下部分:
<build> <sourceDirectory>src/main/scala</sourceDirectory> <testSourceDirectory>src/test/scala</testSourceDirectory> <plugins> <plugin> <groupId>net.alchim31.maven</groupId> <artifactId>scala-maven-plugin</artifactId> <version>MANUALLY_REPLACE_WITH_LATEST_VERSION</version> <configuration> <args> <arg>-target:jvm-1.8</arg> <arg>-deprecation</arg> <arg>-feature</arg> <arg>-unchecked</arg> <arg>-language:implicitConversions</arg> <arg>-language:postfixOps</arg> </args> </configuration> <executions> <execution> <goals> <goal>compile</goal> <goal>testCompile</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
您也可以使用 scalor-maven-plugin 。
您必須在pom.xml中新增以下部分:
<build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <skip>true</skip> <skipMain>true</skipMain> </configuration> </plugin> <plugin> <groupId>com.carrotgarden.maven</groupId> <artifactId>scalor-maven-plugin_2.12</artifactId> <version>MANUALLY_REPLACE_WITH_LATEST_VERSION</version> <configuration> <zincOptionsScala> -target:jvm-1.8 -deprecation -feature -unchecked -language:implicitConversions -language:postfixOps </zincOptionsScala> </configuration> <executions> <execution> <goals> <goal>eclipse-config</goal> <goal>eclipse-format</goal> <goal>eclipse-restart</goal> <goal>eclipse-prescomp</goal> <!-- <goal>register-main</goal> --> <!-- uncomment if you have some Scala code to compile in src/main/scala --> <goal>register-test</goal> <!-- <goal>compile-main</goal> --><!-- uncomment if you have some Scala code to compile in src/main/scala --> <goal>compile-test</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
從IDE啟動加特林
在 gatling-highcharts-maven-archetype 生成,您可以使用從您的IDE手動啟動加特林和記錄一些輔助類。可以完美複製 這3個類 ( Engine
, Recorder
並 IDEPathHelper
在自己的專案)。
以上參考官網介紹:
初次接觸Gatling
檔案目錄介紹
Gatling_Home ├── bin 執行程式 ├── gatling.bat -- 啟動入口(windows) ├── gatling.sh -- 啟動入口(linux) ├── recorder.bat -- UI記錄入口(windows) ├── recorder.sh -- UI記錄入口(linux) ├── conf 配置資訊 ├── gatling.conf -- 加特林配置檔案 ├── gatling-akka.conf -- 加特林akka配置檔案 ├── logback.xml -- 日誌配置檔案 ├── recorder.conf -- recorder配置檔案 ├── lib 加特林依賴jar包 ├── results 測試報告 ├── target 編譯類 ├── user-files 使用者檔案 ├── LICENSE 許可證
How to run it
進入bin目錄可以看到下面兩個檔案(.bat屬於windows下面的可執行指令碼)
gatling.sh 啟動入口 依次輸入以下三個引數
-
執行的指令碼序號
-
本次測試Id,用作測試報告命名字首,不能包含空格,特殊字元,中文等
-
本次測試描述(非必須),會顯示在報告頭部
record.sh
UI操作,它主要用於將瀏覽器配置代理後記錄用戶操作然後生成測試指令碼,更多的用於使用者行為模擬測試。
測試執行
首先肯定是要下載它
解壓並進入到bin目錄下執行sh gatling.sh(linux環境下)
上面列出的就是官方自帶的測試指令碼樣例,我們試著跑下。
輸入0.就能看到視窗開始跳動位元組了!
顯示上面的資訊表示測試程式已經執行完成,最後一行顯示的是本次的測試報告,可以開啟瞅瞅,風騷的報告自己體會吧,首次介紹就先到這裡了。
部分報告截圖如下所示:
其他
關於負載,效能測試工具-Gatling詳解到這裡就結束了。
原創不易,如果感覺不錯,希望給個推薦!您的支援是我寫作的最大動力!
版權宣告:
作者:穆書偉
部落格園出處: https://www.cnblogs.com/sanshengshui
github出處: https://github.com/sanshengshui
個人部落格出處: https://sanshengshui.github.io/