Gatling作為一款開(kāi)源免費(fèi)的性能測(cè)試工具越來(lái)越受到廣大程序員的歡迎。免費(fèi)當(dāng)然是好的,最缺錢(qián)的就是程序員了;開(kāi)源更好啊,缺啥功能、想做定制化的可以自己動(dòng)手,豐衣足食。其實(shí)我最喜歡的原因是其提供了簡(jiǎn)潔強(qiáng)大的API,原生支持命令行運(yùn)行,不像JMeter那樣需要在界面上點(diǎn)來(lái)點(diǎn)去。另外其出色的擴(kuò)展API和輕量級(jí)的HTML報(bào)表都值得讓人稱道。
參考資料:
http://gatling.io/docs/current/quickstart/#recording-the-scenario
https://segmentfault.com/a/1190000008254640
https://segmentfault.com/a/1190000005067519
https://segmentfault.com/a/1190000005069468
https://segmentfault.com/a/1190000005070213
Gatling版本
Gatling的的官方網(wǎng)站是http://gatling-tool.org/。目前Gatling有兩個(gè)主線版本,一個(gè)是Gatling 1,最新版本是1.5.3;一個(gè)是Gatling 2,最新版本是2.0.0-M3a。Gatling 2使用了JDK7進(jìn)行編譯,使用的Scala版本是2.10,它對(duì)Gatling的API進(jìn)行了一些重構(gòu)和重新設(shè)計(jì),內(nèi)部也有一些調(diào)整。 目前Gatling 2還在開(kāi)發(fā)階段,所以如果要在項(xiàng)目中使用的話最好還是使用Gatling 1.5.3。
安裝Gatling
其實(shí)Gatling是款綠色軟件,可以直接從https://github.com/excilys/gatling/wiki/Downloads下載指定的版本。下載下來(lái)解壓縮以后,我們可以看到其目錄結(jié)構(gòu)。
twer@bowen-huang:~/sourcecode/GatlingWorkShop/gatling-charts-highcharts-1.5.2$ tree -L 2
.
├── bin //可執(zhí)行文件目錄
│ ├── gatling.bat
│ ├── gatling.sh
│ ├── recorder.bat
│ └── recorder.sh
├── conf //配置目錄
│ ├── application.conf
│ ├── gatling.conf
│ └── logback.xml
├── lib //依賴的程序庫(kù)
│ ├── 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 //性能測(cè)試結(jié)果存放目錄
│ └── blogsimulation-20131218210445
├── target //性能測(cè)試腳本編譯結(jié)果
│ ├── cache
│ ├── classes
│ └── zincCache
└── user-files //性能測(cè)試腳本源文件
├── data
├── request-bodies
└── simulations
Gatling在user-files目錄中提供了幾個(gè)性能測(cè)試腳本的示例。不過(guò)由于腳本中被測(cè)試的網(wǎng)站已經(jīng)無(wú)法訪問(wèn),所以我自己寫(xiě)了幾個(gè)簡(jiǎn)單的測(cè)試腳本,已經(jīng)放置到了Github上,可以通過(guò)https://github.com/huangbowen521/GatlingWorkShop下載。
twer@bowen-huang:~/sourcecode/GatlingWorkShop/gatling-charts-highcharts-1.5.2/user-files$ tree simulations
simulations
└── blog
├── blog.scala
└── github.scala
directory, 2 files
可以看到在simulations目錄下有兩個(gè)文件,一個(gè)是我給自己的博客寫(xiě)的性能測(cè)試腳本,一個(gè)是給github寫(xiě)的一個(gè)性能測(cè)試腳本。
運(yùn)行Gatling
在命令行下運(yùn)行bin目錄下的Gatling.sh(如果是windows用戶,請(qǐng)運(yùn)行Gatling.bat)。Gatling會(huì)自動(dòng)列出當(dāng)前所有的測(cè)試腳本供自己選擇,然后會(huì)讓填寫(xiě)simulation id(模擬Id)以及run description(運(yùn)行描述)。輸入完畢后按回車鍵測(cè)試即可啟動(dòng)。
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
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
可以看到命令行中最后一行中標(biāo)示了性能測(cè)試報(bào)表的存放路徑。
查看測(cè)試報(bào)告
Gatling的測(cè)試報(bào)表其實(shí)就是一個(gè)html文件。Gatling使用了HighCharts這款JavaScript庫(kù)來(lái)進(jìn)行報(bào)表的展示。另外Gatling還提供了方便的接口用來(lái)自定義報(bào)告的展示。
以下是報(bào)表的部分截圖。
測(cè)試腳本示例
這是GithubSimulation的性能測(cè)試腳本。其實(shí)它就是Scala的一個(gè)類,繼承自Simulation。
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))
}
在這個(gè)類中定義了一個(gè)httpConf,指定了被測(cè)網(wǎng)站的根目錄。
var httpConf = httpConfig.
baseURL("https://github.com")
然后定義了一個(gè)測(cè)試場(chǎng)景,用戶先訪問(wèn)Github首頁(yè),檢查http返回狀態(tài)碼是否為200,然后暫停一段時(shí)間后再執(zhí)行一個(gè)查詢操作,查詢關(guān)鍵字是gatling,檢查http返回狀態(tài)碼是否為200。
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個(gè)用戶模擬該測(cè)試場(chǎng)景。500個(gè)用戶以每秒50個(gè)遞增,持續(xù)10秒。
setUp(scn.users(500).ramp(10).protocolConfig(httpConf))
技術(shù)棧
這里列出了Gatling的一些主要的技術(shù)棧。
Akka Actors.
Gatling 使用了Akka作為其并發(fā)編程的運(yùn)行時(shí)。Akka的Actors模式能夠有效的繞過(guò)JVM上多線程帶來(lái)的性能問(wèn)題。
Scala.
選擇Scala最大的原因是因?yàn)镾cala可以很好的集成Akka,另一原因是作為一款運(yùn)行在JVM上的語(yǔ)言,Scala更容易提供給用戶簡(jiǎn)潔強(qiáng)大的API設(shè)計(jì)。
Async Http Client.
使用這款開(kāi)源庫(kù)來(lái)是實(shí)現(xiàn)異步http通訊。并且使用了Netty調(diào)用http。
Highcharts及Highstock.
Gatling使用Highcarts和Highstock這兩款JavaScript庫(kù)來(lái)進(jìn)行測(cè)試結(jié)果報(bào)表的展示。