Gradle与Gatling脚本集成

Gatling作为次时代的性能测试工具,由于其API简洁明了、性能出众,越来越受欢迎。但是运行Gatling脚本却有诸多不便,其提供的默认方式不是很方便。考虑到Gatling脚本本质上是Scala类,运行的时候还是使用的是java虚拟机,我们可以将其脚本的运行与Gradle结合起来。这样子就可以通过Gradle来运行Gatling脚本了。

废话少说,接下来就讲述下如何来进行配置。

创建一个标准的maven结构的工程目录,如下图所示。

conf目录存放Gatling的基本配置文件。 Gatling的脚本文件存放在src/test/scala/simulations包里面。可以自行在此包下对脚本文件再分类。

在build.gradle文件中引入scala插件。

1
apply plugin: 'scala'

然后引入有gatling库的maven repo。

1
2
3
4
5
6
repositories {
    mavenCentral ()
    maven {
        url 'http://repository.excilys.com/content/groups/public'
    }
}

再加入scala和gatling的依赖项。

1
2
3
4
dependencies {
    compile 'org.scala-lang:scala-library:2.10.1'
    testCompile 'io.gatling.highcharts:gatling-charts-highcharts:2.0.0-M3a'
}

把conf文件夹作为test的source文件。

1
2
3
4
5
6
7
sourceSets {
    test {
        resources {
            srcDir 'conf'
        }
    }
}

创建一个名为gatling的task,目的是运行所有的gatling脚本。

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
task gatling (dependsOn: 'compileTestScala') << {

    logger.lifecycle (" ---- Executing all Gatling scenarios from: ${sourceSets.test.output.classesDir} ----")

    sourceSets.test.output.classesDir.eachFileRecurse { file ->
        if (file.isFile ()) {

            def gatlingScenarioClass = (file.getPath () - (sourceSets.test.output.classesDir.getPath () + File.separator) - '.class')
                    .replace (File.separator, '.')

            javaexec {
                main = 'io.gatling.app.Gatling'
                classpath = sourceSets.test.output + sourceSets.test.runtimeClasspath
                args '-sbf',
                        sourceSets.test.output.classesDir,
                        '-s',
                        gatlingScenarioClass,
                        '-rf',
                        'build/reports/gatling'
            }
        }

    }

    logger.lifecycle (" ---- Done executing all Gatling scenarios ----")
}

这是借助于Gatling的command line运行功能来实现的。具体参数指定官网上有,这里贴出原文。

Command Line Options # Gatling can be started with several options listed below:

  • -nr (–no-reports): Runs simulation but does not generate reports
  • -ro (–reports-only ): Generates the reports for the simulation log file located in /results/
  • -df (–data-folder ): Uses as the folder where feeders are stored
  • -rf (–results-folder ): Uses as the folder where results are stored
  • -bf (–request-bodies-folder ): Uses as the folder where request bodies are stored
  • -sf (–simulations-folder ): Uses as the folder where simulations are stored
  • -sbf (–simulations-binaries-folder ): Uses as the folder where simulation binaries are stored
  • -s (–simulation ): Uses as the name of the simulation to be run
  • -sd (–simulation-description ): Uses as simulation description

我在github上创建了一个示例项目,请参见https://github.com/huangbowen521/gatling-gradle

posted @   黄博文  阅读(2246)  评论(1编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示