On Windows:
%GATLING_HOME%\bin\gatling.bat
4.2 啟動並執行時候會把所有的能啟動並執行情境(simulation)都列出來,選擇想要啟動並執行即可。
5 測試結果
Gatling測試報表基於HTML,並且在測試過程中業已產生,所以開啟速度很快。而且,當把滑鼠移動到不同資料軸上時,都會有彈出框顯示這個點上詳細的測試資料資訊。這種動態顯示資料的方式非常方便查看和分析資料。考慮到項目真實資料的不便,我將通過Gatling官方網站給出的樣本報表進行說明。
Gatling的報表分為兩類:GLOBAL和DETAILS,其中GLOBAL主要是請求相關的統計資料,比如每秒請求數,請求成功與失敗數等;其中DETAILS主要是請求時間相關的統計資料,比如請求回應時間,請求響應延遲時間等。
圖4 每秒請求數,
當滑鼠放到圖中任何一個點的時候,對應時間點上請求的詳細資料就會以圖中白色的彈出框的方式進行顯示。在下面的請求響應延遲時間圖裡面也有同樣的功能。
圖5 請求響應延遲時間,
6 進階
指令碼解析:
package computerdatabase // 1 包名
import io.gatling.core.Predef._ // 2必須匯入的
import io.gatling.http.Predef._
import scala.concurrent.duration._
class BasicSimulation extends Simulation { // 3 類聲明,必須繼承Simulation
val httpConf = http // 4 所有Http請求普遍配置
.baseURL("http://computer-database.gatling.io") // 5 base URL
.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 http請求名稱 request_1,這個名稱最後會顯示在報告中
.get("/")) // 9 get要求方法
.pause(5) // 10 暫停/考慮時間 5s
setUp( // 11 建立情境
scn.inject(atOnceUsers(1)) // 12 聲明注入一個使用者
).protocols(httpConf) // 13 之前聲明的Http請求配置
}
6.1 分離情境:分離操作:如瀏覽、搜尋、編輯操作
object Search{valsearch=exec(http("Home")// let's give proper names, as they are displayed in the reports
.get("/")).pause(7).exec(http("Search").get("/computers?f=macbook")).pause(2).exec(http("Select").get("/computers/6")).pause(3)}objectBrowse{valbrowse=???}objectEdit{valedit=???}
We can now rewrite our scenario using these reusable business processes:
valscn=scenario("Scenario Name").exec(Search.search,Browse.browse,Edit.edit)
6.2 設定常規使用者和管理員賬戶
val users=scenario("Users").exec(Search.search,Browse.browse)
val admins=scenario("Admins").exec(Search.search,Browse.browse,Edit.edit)
6.3 設定虛擬使用者
setUp(users.inject(atOnceUsers(10)).protocols(httpConf))
6.4 設定上升壓力,即每隔一段時間啟動多少使用者
setUp(users.inject(rampUsers(10)over(10seconds)),admins.inject(rampUsers(2)over(10seconds))).protocols(httpConf)
10s 內啟動10個users和2個admin
6.5 參數化 or 動態資料
6.5.1 建立檔案:在user-files/data檔案夾下建立cvs檔案,如:search.csv ,內容如下:
searchCriterion,searchComputerName
Macbook,MacBook Pro
eee,ASUS Eee PC 1005PE
6.5.2 使用檔案:
objectSearch{
valfeeder=csv("search.csv").random// 1, 2
valsearch=exec(http("Home").get("/")).pause(1).feed(feeder)// 3
.exec(http("Search").get("/computers?f=${searchCriterion}")// 4
.check(css("a:contains('${searchComputerName}')","href").saveAs("computerURL")))// 5
.pause(1).exec(http("Select").get("${computerURL}"))// 6
.pause(1)}
6.5.3 解釋:
Explanations: