負載,性能測試工具-Gatling


 

前言

Gatling

Gatling是一款功能強大的負載測試工具,它為易於使用,高可維護性和高性能而設計。

開箱即用,Gatling由於對HTTP協議的出色支持,使其成為負載測試任何HTTP服務器的首選工具。由於核心引擎實際上是協議不可知的,因此完全可以實現對其他協議的支持。例如,Gatling目前還提供JMS支持。

代碼自定義並且場景資源有效是Gatling的兩個基礎。並且擁有富有表現力的DSL,自我解釋的場景,易於維護,可以保存在版本控制系統中的優點。

只要底層協議(如HTTP)可以以非阻塞方式實現,Gatling的體系結構就是異步的。這種架構允許我們將虛擬用戶實現為消息而不是專用線程,這使得硬件要求不需要很苛刻。因此,運行數千個並發虛擬用戶不是問題。

使用理由

  • 避免崩潰:Gatling可以幫助您預測緩慢的響應時間和奔潰。

  • 縮短產品上市時間:Gatling 在開發周期的早期檢測到性能問題和錯誤

  • 增強用戶體驗:Gatling可以准確描述您最慢的用戶體驗

  • 提升您的業務:加特林可以防止您的業務成為自身成功的犧牲品

性能測試

性能測試是什么意思?

Web應用程序的性能測試包括:

  1. 模擬大量具有復雜行為的用戶;

  2. 收集和匯總所有請求的響應時間;

  3. 創建報告分析數據;

 

編寫測試場景,自動化測試

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的說明,請參閱“ 打開文件限制” 和“ 內核和網絡調整”部分。

在正常操作期間,Gatling會消耗大量的打開文件句柄。通常,操作系統會限制此數量,因此您可能必須在所選操作系統中調整一些選項,以便可以打開許多新套接字並實現高負載。

打開文件限制

大多數操作系統都可以使用該命令更改打開文件限制。例:ulimit -n

  $ ulimit -n 65536

但是,這只會更改當前shell會話的限制。在系統范圍內,永久性地更改限制因系統而異。

為系統的所有用戶永久設置軟值和硬值以允許最多65536個打開文件; 編輯/etc/security/limits.conf並附加以下兩行:

  *       soft    nofile  65535
  *       hard    nofile  65535

 

保存文件。啟動新會話以使限制生效。您現在可以驗證是否正確設置了限制。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_HOMEenv var中設置它。

    與OSX一樣,操作系統有自己的棘手方法來發現要運行的Java版本,因此最終可能會運行與您告訴您的版本不同的版本。如果您遇到奇怪的錯誤,並且您希望運行JDK8,則可能需要明確設置JAVA_HOMEjava -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>

 

包含/排除過濾器

運行多個模擬時,您可以使用includesexcludes過濾器控制哪些模擬將被觸發。那些使用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-pluginscalor-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-pluginm2eclipse-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個類EngineRecorderIDEPathHelper在自己的項目)。

 

以上參考官網介紹:

https://gatling.io/

 

初次接觸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操作,它主要用於將瀏覽器配置代理后記錄用戶操作然后生成測試腳本,更多的用於用戶行為模擬測試。

測試運行

首先肯定是要下載它

http://gatling.io/#/download

解壓並進入到bin目錄下運行sh gatling.sh(linux環境下)

 

 

上面列出的就是官方自帶的測試腳本樣例,我們試着跑下。

輸入0.就能看到窗口開始跳動字節了!

顯示上面的信息表示測試程序已經運行完成,最后一行顯示的是本次的測試報告,可以打開瞅瞅,風騷的報告自己體會吧,首次介紹就先到這里了。

部分報告截圖如下所示:

 

其他

關於負載,性能測試工具-Gatling詳解到這里就結束了。

原創不易,如果感覺不錯,希望給個推薦!您的支持是我寫作的最大動力!

版權聲明:

作者:穆書偉

博客園出處:https://www.cnblogs.com/sanshengshui

github出處:https://github.com/sanshengshui    

個人博客出處:https://sanshengshui.github.io/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM