在本章,咱們將使用gatling來加載測試一個雲託管web服務器,並向你介紹DSL基本元素。css
安裝gatling:將gatling解壓至指定文件夾便可html
提示:Windows用戶:咱們建議您不要將Gatling放在程序文件夾中,由於可能存在權限問題。java
爲了運行Gatling,您須要安裝JDK。 Gatling須要JDK8,但咱們建議您使用最新版本。ios
有關操做系統(OS)的安裝和調整的全部詳細信息,請參閱操做部分。git
提示:github
Gatling launch scripts and Gatling maven plugin honor JAVA_HOME
env var if it’s set. OS, like OSX, have their own tricky way of discovering which version of Java to run, so you might end up running a different version than the one java -version
tells you. If you get strange errors such as Unsupported major.minor version 51.0
and you were expecting to run a JDK8, you might want to explicitly set JAVA_HOME
.web
編碼:瀏覽器
Gatling的默認編碼爲UTF-8。 若是你想使用其餘的,你必須:服務器
在使用記錄器時選擇正確的編碼
在gatling.conf文件中配置正確的編碼。
它將用於編譯您的模擬,構建您的請求和您的響應。
確保您的文本編輯器編碼已正確配置爲匹配。app
關於scala:
Gatling模擬腳本是用Scala編寫的,但不要驚慌! 您可使用Gatling的全部基本功能,而沒必要了解Scala。 在大多數狀況下,DSL將覆蓋您的大部分需求,您將可以構建您的方案。
若是您有興趣瞭解Scala的更多信息,那麼咱們建議您看看Twitter的Scala School。
測試用例
本頁將引導您瞭解大部分Gatling HTTP功能。 您將瞭解模擬,情景,饋線,錄音機,循環等。
測試應用
在本教程中,咱們將使用名爲Computer-Database的應用程序部署在URL:http://computer-database.gatling.io。
此應用程序是用於管理計算機模型的簡單CRUD應用程序,而且是2.3版以前的Play Framework示例。
腳本
爲了測試此應用程序的性能,咱們將建立表明用戶瀏覽時真正發生的狀況。
這是咱們認爲真正的用戶對應用程序的見解:
用戶到達應用程序。
用戶搜索「macbook」。
用戶打開相關型號之一。
用戶回到主頁。
用戶遍歷頁面。
用戶建立一個新模型。
Basics
Using the Recorder
To ease the creation of the scenario, we will use the Recorder, a tool provided with Gatling that allows you to record your actions on a web application and export them as a Gatling scenario.
基本
使用錄製器
爲了簡單的建立場景,咱們使用錄製器,Gatling提供的一個工具,能夠在Web應用程序中記錄您的操做,並將其做爲Gatling場景導出。
This tool is launched with a script located in the bin directory:
On Linux/Unix:
$GATLING_HOME/bin/recorder.sh
On Windows:
%GATLING_HOME%\bin\recorder.bat
Once launched, the following GUI lets you configure how requests and responses will be recorded.
設置如下選項:
存儲位置
BasicSimulation名稱
關注重定向?檢查
自動參考檢查
黑名單首選過濾策略
黑色列表過濾器中的*。css,。* \。js和。* \ ico
配置錄製器後,您只需啓動錄製器並配置瀏覽器便可使用Gatling Recorder的代理。
有關錄製器和瀏覽器配置的更多信息,請查看錄製器參考頁面。
錄製場景
輸入「搜索」標籤。
訪問網站:http://computer-database.gatling.io
搜索名爲「macbook」的模型。
選擇'Macbook pro'。
輸入「瀏覽」標籤。
回到主頁。
經過單擊下一步按鈕迭代模型頁面幾回。
輸入「編輯」標籤。
單擊添加新計算機。
填表格。
單擊建立此計算機。
嘗試充當真正的用戶,不要當即從一個頁面跳到另外一個頁面,而不用花時間閱讀。 這將使您的場景更貼近實際用戶的行爲。
完成播放場景後,單擊記錄器界面中的中止。
模擬將在您的Gatling安裝的文件夾user-files / simulations / computerdatabase中以名稱BasicSimulation.scala生成。
Gatling場景解釋
這裏是生產的產出:
package computerdatabase // 1 import io.gatling.core.Predef._ // 2 import io.gatling.http.Predef._ import scala.concurrent.duration._ class BasicSimulation extends Simulation { // 3 val httpConf = http // 4 .baseURL("http://computer-database.gatling.io") // 5 .acceptHeader("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8") // 6 .doNotTrackHeader("1") .acceptLanguageHeader("en-US,en;q=0.5") .acceptEncodingHeader("gzip, deflate") .userAgentHeader("Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0") val scn = scenario("BasicSimulation") // 7 .exec(http("request_1") // 8 .get("/")) // 9 .pause(5) // 10 setUp( // 11 scn.inject(atOnceUsers(1)) // 12 ).protocols(httpConf) // 13 }
What does it mean?
Simulation
.val是用於定義常量值的關鍵字。 類型未定義,並由Scala編譯器推斷。
持續時間單位默認爲秒,例如 pause(5)至關於暫停(5秒)。
Note
For more details regarding Simulation structure, please check out Simulation reference page.
Launch the second script located in the bin directory:
On Linux/Unix:
$GATLING_HOME/bin/gatling.sh
On Windows:
%GATLING_HOME%\bin\gatling.bat
You should see a menu with the simulation examples:
Choose a simulation number: [0] computerdatabase.BasicSimulation
When the simulation is done, the console will display a link to the HTML reports.
Note
If Gatling doesn’t work as expected, see our FAQ or ask on our Google Group.
When you’re ready to go further, please check out the Advanced Tutorial.