負載,性能測試工具-Gatling

 

前言

Gatling

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

開箱即用,Gatling因爲對HTTP協議的出色支持,使其成爲負載測試任何HTTP服務器的首選工具。因爲核心引擎其實是協議不可知的,所以徹底能夠實現對其餘協議的支持。例如,Gatling目前還提供JMS支持。java

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

只要底層協議(如HTTP)能夠以非阻塞方式實現,Gatling的體系結構就是異步的。這種架構容許咱們將虛擬用戶實現爲消息而不是專用線程,這使得硬件要求不須要很苛刻。所以,運行數千個併發虛擬用戶不是問題。git

使用理由

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

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

  • 加強用戶體驗:Gatling能夠準確描述您最慢的用戶體驗windows

  • 提高您的業務:加特林能夠防止您的業務成爲自身成功的犧牲品瀏覽器

性能測試

性能測試是什麼意思?

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

  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/

相關文章
相關標籤/搜索