文章出處

Gatling作為一款開源免費的性能測試工具越來越受到廣大程序員的歡迎。免費當然是好的,最缺錢的就是程序員了;開源更好啊,缺啥功能、想做定制化的可以自己動手,豐衣足食。其實我最喜歡的原因是其提供了簡潔強大的API,原生支持命令行運行,不像JMeter那樣需要在界面上點來點去。另外其出色的擴展API和輕量級的HTML報表都值得讓人稱道。

Gatling版本

Gatling的的官方網站是http://gatling-tool.org/。目前Gatling有兩個主線版本,一個是Gatling 1,最新版本是1.5.3;一個是Gatling 2,最新版本是2.0.0-M3a。Gatling 2使用了JDK7進行編譯,使用的Scala版本是2.10,它對Gatling的API進行了一些重構和重新設計,內部也有一些調整。 目前Gatling 2還在開發階段,所以如果要在項目中使用的話最好還是使用Gatling 1.5.3。

安裝Gatling

其實Gatling是款綠色軟件,可以直接從https://github.com/excilys/gatling/wiki/Downloads下載指定的版本。下載下來解壓縮以后,我們可以看到其目錄結構。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
twer@bowen-huang:~/sourcecode/GatlingWorkShop/gatling-charts-highcharts-1.5.2$ tree -L 2
.
├── bin                             //可執行文件目錄
│   ├── gatling.bat
│   ├── gatling.sh
│   ├── recorder.bat
│   └── recorder.sh
├── conf                            //配置目錄
│   ├── application.conf
│   ├── gatling.conf
│   └── logback.xml
├── lib                             //依賴的程序庫
│   ├── akka-actor-2.0.4.jar
│   ├── async-http-client-1.7.18.20130621.jar
│   ├── commons-io-2.4.jar
│   ├── commons-lang-2.6.jar
│   ├── commons-math3-3.1.1.jar
│   ├── commons-pool-1.6.jar
│   ├── compiler-interface-0.12.3-sources.jar
│   ├── config-1.0.1.jar
│   ├── gatling-app-1.5.2.jar
│   ├── gatling-charts-1.5.2.jar
│   ├── gatling-charts-highcharts-1.5.2.jar
│   ├── gatling-core-1.5.2.jar
│   ├── gatling-http-1.5.2.jar
│   ├── gatling-jdbc-1.5.2.jar
│   ├── gatling-metrics-1.5.2.jar
│   ├── gatling-recorder-1.5.2.jar
│   ├── gatling-redis-1.5.2.jar
│   ├── grizzled-slf4j_2.9.2-0.6.10.jar
│   ├── incremental-compiler-0.12.3.jar
│   ├── jaxen-1.1.6.jar
│   ├── joda-convert-1.2.jar
│   ├── joda-time-2.2.jar
│   ├── jodd-core-3.4.4.jar
│   ├── jodd-lagarto-3.4.4.jar
│   ├── json-path-0.8.2.fix24.jar
│   ├── json-smart-1.1.1.jar
│   ├── jsoup-1.7.2.jar
│   ├── logback-classic-1.0.12.jar
│   ├── logback-core-1.0.12.jar
│   ├── netty-3.6.6.Final.jar
│   ├── opencsv-2.3.jar
│   ├── redisclient_2.9.2-2.10.jar
│   ├── scala-compiler-2.9.3.jar
│   ├── scala-library-2.9.3.jar
│   ├── scalate-core_2.9-1.6.1.jar
│   ├── scalate-util_2.9-1.6.1.jar
│   ├── scopt_2.9.2-2.1.0.jar
│   ├── slf4j-api-1.7.5.jar
│   ├── xercesImpl-2.11.0.jar
│   ├── xml-apis-1.4.01.jar
│   ├── xstream-1.4.3.jar
│   └── zinc-0.2.5.jar
├── results            //性能測試結果存放目錄
│   └── blogsimulation-20131218210445
├── target             //性能測試腳本編譯結果
│   ├── cache
│   ├── classes
│   └── zincCache
└── user-files     //性能測試腳本源文件
    ├── data
    ├── request-bodies
    └── simulations

Gatling在user-files目錄中提供了幾個性能測試腳本的示例。不過由于腳本中被測試的網站已經無法訪問,所以我自己寫了幾個簡單的測試腳本,已經放置到了Github上,可以通過https://github.com/huangbowen521/GatlingWorkShop下載。

1
2
3
4
5
6
7
twer@bowen-huang:~/sourcecode/GatlingWorkShop/gatling-charts-highcharts-1.5.2/user-files$ tree simulations
simulations
└── blog
    ├── blog.scala
    └── github.scala

1 directory, 2 files

可以看到在simulations目錄下有兩個文件,一個是我給自己的博客寫的性能測試腳本,一個是給github寫的一個性能測試腳本。

運行Gatling

在命令行下運行bin目錄下的Gatling.sh(如果是windows用戶,請運行Gatling.bat)。Gatling會自動列出當前所有的測試腳本供自己選擇,然后會讓填寫simulation id(模擬Id)以及run description(運行描述)。輸入完畢后按回車鍵測試即可啟動。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
twer@bowen-huang:~/sourcecode/GatlingWorkShop/gatling-charts-highcharts-1.5.2$ ./bin/gatling.sh
GATLING_HOME is set to /Users/twer/sourcecode/GatlingWorkShop/gatling-charts-highcharts-1.5.2
Choose a simulation number:
     [0] blog.BlogSimulation
     [1] blog.GithubSimulation
1
Select simulation id (default is 'githubsimulation'). Accepted characters are a-z, A-Z, 0-9, - and _
github
Select run description (optional)
testing github

Simulation blog.GithubSimulation started…

……
……
……

Simulation finished.
Simulation successful.
Generating reports...
Reports generated in 0s.
Please open the following file : /Users/twer/sourcecode/GatlingWorkShop/gatling-charts-highcharts-1.5.2/results/github-20131223214957/index.html

可以看到命令行中最后一行中標示了性能測試報表的存放路徑。

查看測試報告

Gatling的測試報表其實就是一個html文件。Gatling使用了HighCharts這款JavaScript庫來進行報表的展示。另外Gatling還提供了方便的接口用來自定義報告的展示。

以下是報表的部分截圖。

測試腳本示例

這是GithubSimulation的性能測試腳本。其實它就是Scala的一個類,繼承自Simulation。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package blog

import com.excilys.ebi.gatling.core.Predef._
import com.excilys.ebi.gatling.http.Predef._
import com.excilys.ebi.gatling.jdbc.Predef._
import com.excilys.ebi.gatling.http.Headers.Names._
import akka.util.duration._
import bootstrap._

class GithubSimulation extends Simulation {
     var httpConf = httpConfig.
     baseURL("https://github.com")

     var scn = scenario("search in github.com")
     .exec(
          http("home page")
          .get("/")
          .check(status.is(200)))
     .pause(0, 12)
     .exec(
          http("do search")
          .get("/search")
          .queryParam("q", "gatling")
          .check(status.is(200)))

     setUp(scn.users(500).ramp(10).protocolConfig(httpConf))
}

在這個類中定義了一個httpConf,指定了被測網站的根目錄。

1
2
     var httpConf = httpConfig.
     baseURL("https://github.com")

然后定義了一個測試場景,用戶先訪問Github首頁,檢查http返回狀態碼是否為200,然后暫停一段時間后再執行一個查詢操作,查詢關鍵字是gatling,檢查http返回狀態碼是否為200。

1
2
3
4
5
6
7
8
9
10
11
     var scn = scenario("search in github.com")
     .exec(
          http("home page")
          .get("/")
          .check(status.is(200)))
     .pause(0, 12)
     .exec(
          http("do search")
          .get("/search")
          .queryParam("q", "gatling")
          .check(status.is(200)))

最后指定500個用戶模擬該測試場景。500個用戶以每秒50個遞增,持續10秒。

1
     setUp(scn.users(500).ramp(10).protocolConfig(httpConf))

技術棧

這里列出了Gatling的一些主要的技術棧。

  • Akka Actors. Gatling 使用了Akka作為其并發編程的運行時。Akka的Actors模式能夠有效的繞過JVM上多線程帶來的性能問題。

  • Scala.選擇Scala最大的原因是因為Scala可以很好的集成Akka,另一原因是作為一款運行在JVM上的語言,Scala更容易提供給用戶簡潔強大的API設計。

  • Async Http Client. 使用這款開源庫來是實現異步http通訊。并且使用了Netty調用http。

  • Highcharts及Highstock. Gatling使用Highcarts和Highstock這兩款JavaScript庫來進行測試結果報表的展示。


其實Gatling并不完美,比如目前支持的協議并不多,不支持對數據庫的性能測試,不能進行分布式性能測試等。當然Gatling也在不斷的進步,看好你哦!


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()