Gatling是一款基于Scala 开发的高性能服务器性能测试工具,它主要用于对服务器进行负载等测试,并分析和测量服务器的各种性能指标。
- 除此之外它拥有以下特点:
- 支持Akka Actors 和 Async IO,从而能达到很高的性能
- 支持实时生成Html动态轻量报表,从而使报表更易阅读和进行数据分析
- 支持DSL脚本,从而使测试脚本更易开发与维护
- 支持录制并生成测试脚本,从而可以方便的生成测试脚本
- 支持导入HAR(Http Archive)并生成测试脚本
- 支持Maven,Eclipse,IntelliJ等,以便于开发
- 支持Jenkins,以便于进行持续集成
- 支持插件,从而可以扩展其功能,比如可以扩展对其他协议的支持
- 开源免费,开箱即用
下载【 https://gatling.io/open-source/start-testing/ 】
- bin 目录下有2个脚本,gatling和recorder, gatling用来运行测试,
- conf Gatling自身的一些配置。
- lib Gatling自身依赖的库文件
- results 存放测试报告
- target 你启动运行组件后,gatling会为你编译好所有的.scala脚本,而编译后的class文件就会在这里
- user-files 脚本存放位置 user-files/simulations ,默认下载好的包会有几个官方的示例测试Demo
当运行gating脚本的时候,其会扫描user-files目录下的所有文件,列出其中所有的Simulation(一个测试类,里面可以包含任意多个测试场景)。选择其中一个Simulation,然后填写Simulation ID和运行描述,脚本列表如下:
- 挑选 AdvancedSimulationStep01.scala 脚本查看如下:
/*
* Copyright 2011-2021 GatlingCorp (https://gatling.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package computerdatabase.advanced
import scala.concurrent.duration._
import io.gatling.core.Predef._
import io.gatling.http.Predef._
class AdvancedSimulationStep01 extends Simulation {
// Let's split this big scenario into composable business processes, like one would do with PageObject pattern with Selenium
// object are native Scala singletons
object Search {
val search = exec(
http("Home") // let's give proper names, they are displayed in the reports, and used as keys
.get("/")
).pause(1) // let's set the pauses to 1 sec for demo purpose
.exec(
http("Search")
.get("/computers?f=macbook")
)
.pause(1)
.exec(
http("Select")
.get("/computers/6")
)
.pause(1)
}
object Browse {
val browse = exec(
http("Home")
.get("/")
).pause(2)
.exec(
http("Page 1")
.get("/computers?p=1")
)
.pause(670.milliseconds)
.exec(
http("Page 2")
.get("/computers?p=2")
)
.pause(629.milliseconds)
.exec(
http("Page 3")
.get("/computers?p=3")
)
.pause(734.milliseconds)
.exec(
http("Page 4")
.get("/computers?p=4")
)
.pause(5)
}
object Edit {
val edit = exec(
http("Form")
.get("/computers/new")
).pause(1)
.exec(
http("Post")
.post("/computers")
.formParam("name", "Beautiful Computer")
.formParam("introduced", "2012-05-30")
.formParam("discontinued", "")
.formParam("company", "37")
)
}
val httpProtocol = http
.baseUrl("http://computer-database.gatling.io")
.acceptHeader("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
.doNotTrackHeader("1")
.acceptLanguageHeader("en-US,en;q=0.5")
.acceptEncodingHeader("gzip, deflate")
.userAgentHeader("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:16.0) Gecko/20100101 Firefox/16.0")
// Now, we can write the scenario as a composition
val scn = scenario("Scenario Name").exec(Search.search, Browse.browse, Edit.edit)
setUp(scn.inject(atOnceUsers(1)).protocols(httpProtocol))
}
进入bin目录执行:
./gatling.sh
结果如下:
liwen bin % ./gatling.sh #Gatling 会遍历user-files/simulations,列出所有的Simulation
GATLING_HOME is set to /Users/liwen/Downloads/gatling-charts-highcharts-bundle-3.5.1
Choose a simulation number:
[0] computerdatabase.BasicSimulation
[1] computerdatabase.advanced.AdvancedSimulationStep01
[2] computerdatabase.advanced.AdvancedSimulationStep02
[3] computerdatabase.advanced.AdvancedSimulationStep03
[4] computerdatabase.advanced.AdvancedSimulationStep04
[5] computerdatabase.advanced.AdvancedSimulationStep05
1 # 这个是我选择运行的测试用例ID - 代表选择AdvancedSimulationStep01执行,
Select run description (optional)
runtest # 填入测试用例的描述,可以直接回车跳过
Simulation computerdatabase.advanced.AdvancedSimulationStep01 started...
================================================================================
2021-04-23 21:48:31 5s elapsed
---- Requests ------------------------------------------------------------------
> Global (OK=6 KO=0 )
> Home (OK=2 KO=0 )
> Home Redirect 1 (OK=2 KO=0 )
> Search (OK=1 KO=0 )
> Select (OK=1 KO=0 )
---- Scenario Name -------------------------------------------------------------
[--------------------------------------------------------------------------] 0%
waiting: 0 / active: 1 / done: 0
================================================================================
================================================================================
2021-04-23 21:48:36 10s elapsed
---- Requests ------------------------------------------------------------------
> Global (OK=9 KO=0 )
> Home (OK=2 KO=0 )
> Home Redirect 1 (OK=2 KO=0 )
> Search (OK=1 KO=0 )
> Select (OK=1 KO=0 )
> Page 1 (OK=1 KO=0 )
> Page 2 (OK=1 KO=0 )
> Page 3 (OK=1 KO=0 )
---- Scenario Name -------------------------------------------------------------
[--------------------------------------------------------------------------] 0%
waiting: 0 / active: 1 / done: 0
================================================================================
================================================================================
2021-04-23 21:48:41 15s elapsed
---- Requests ------------------------------------------------------------------
> Global (OK=10 KO=0 )
> Home (OK=2 KO=0 )
> Home Redirect 1 (OK=2 KO=0 )
> Search (OK=1 KO=0 )
> Select (OK=1 KO=0 )
> Page 1 (OK=1 KO=0 )
> Page 2 (OK=1 KO=0 )
> Page 3 (OK=1 KO=0 )
> Page 4 (OK=1 KO=0 )
---- Scenario Name -------------------------------------------------------------
[--------------------------------------------------------------------------] 0%
waiting: 0 / active: 1 / done: 0
================================================================================
================================================================================
2021-04-23 21:48:43 16s elapsed
---- Requests ------------------------------------------------------------------
> Global (OK=13 KO=0 )
> Home (OK=2 KO=0 )
> Home Redirect 1 (OK=2 KO=0 )
> Search (OK=1 KO=0 )
> Select (OK=1 KO=0 )
> Page 1 (OK=1 KO=0 )
> Page 2 (OK=1 KO=0 )
> Page 3 (OK=1 KO=0 )
> Page 4 (OK=1 KO=0 )
> Form (OK=1 KO=0 )
> Post (OK=1 KO=0 )
> Post Redirect 1 (OK=1 KO=0 )
---- Scenario Name -------------------------------------------------------------
[##########################################################################]100%
waiting: 0 / active: 0 / done: 1
================================================================================
Simulation computerdatabase.advanced.AdvancedSimulationStep01 completed in 16 seconds
Parsing log file(s)...
Parsing log file(s) done
Generating reports...
================================================================================
---- Global Information --------------------------------------------------------
> request count 13 (OK=13 KO=0 )
> min response time 265 (OK=265 KO=- )
> max response time 547 (OK=547 KO=- )
> mean response time 289 (OK=289 KO=- )
> std deviation 75 (OK=75 KO=- )
> response time 50th percentile 267 (OK=267 KO=- )
> response time 75th percentile 268 (OK=268 KO=- )
> response time 95th percentile 380 (OK=380 KO=- )
> response time 99th percentile 514 (OK=514 KO=- )
> mean requests/sec 0.765 (OK=0.765 KO=- )
---- Response Time Distribution ------------------------------------------------
> t < 800 ms 13 (100%)
> 800 ms < t < 1200 ms 0 ( 0%)
> t > 1200 ms 0 ( 0%)
> failed 0 ( 0%)
================================================================================
# result目录找到报告的html文件
Reports generated in 0s.
Please open the following file: /Users/liwen/Downloads/gatling-charts-highcharts-bundle-3.5.1/results/advancedsimulationstep01/index.html
打开指定路径报告如下:
其他图略;
Gatling