写点什么

Gatling 发布全新 Java DSL,Java 与 Kotlin 齐飞

作者:Johan Janssen

  • 2023-10-05
    北京
  • 本文字数:2795 字

    阅读完需:约 9 分钟

大小:481.68K时长:02:44
Gatling发布全新Java DSL,Java与Kotlin齐飞

负载测试工具Gatling是为易用性、可维护性和高性能而设计的。它最初提供了 Scala DSL 来编写测试场景。后来,它发布了 Java DSL,可以使用 Java 或 Kotlin 编写测试场景。


Gatling 的快速入门文档中有一段专门介绍了如何选择正确的语言, 建议已经在使用 Scala 或 Kotlin 的开发人员使用这些语言编写测试,但如果还没有在使用这些语言,推荐使用 Java,因为它广为人知,需要较少的 CPU 进行编译,并且更容易在 Maven 或 Gradle 中配置。


Java, Kotlin or Scala: Which Gatling Flavor is Right for You?”这篇文章发表于 Java DSL 发布一年之后,文中显示,有 35%的用户在使用 Java DSL。在文章中,Gatling 明确指出,尽管 Java DSL 迅速流行起来,但他们计划继续支持 Scala DSL,用户可以自由选择 Java、Scala 和 Kotlin 来编写测试。


假设有一个使用 Scala 编写的测试场景,其中有 8 个用户在 10 秒内启动,0 秒后 0 个用户,5 秒后 4 个用户,10 秒后 8 个用户。然后每个用户执行五次循环,验证 car 和 carpart 端点是否都返回 HTTP 200 状态码:


class BasicSimulationScala extends Simulation {    val httpProtocol = http        .baseUrl("http://localhost:8080");
val scn = scenario("BasicSimulation") .repeat(5){ exec(http("car").get("/car") .check(status.is(200))) .pause(1) .exec(http("carpart") .get("/carpart") .check(status.is(200))) }
setUp( scn.inject(rampUsers(8).during(10)) ).protocols(httpProtocol);}
复制代码


这个测试可以在 Linux/Unix 上使用 Gatling 脚本运行


$GATLING_HOME/bin/gatling.sh
复制代码


或者在 Windows 上:

 %GATLING_HOME%\bin\gatling.bat
复制代码


另外,也可以使用 Maven 等构建工具来运行测试,方法是在testSourceDirectory中指定测试场景的目录,并配置Scala Maven插件Gatling Maven插件


<build>    <testSourceDirectory>src/test/scala</testSourceDirectory>    <plugins>        <plugin>            <groupId>net.alchim31.maven</groupId>            <artifactId>scala-maven-plugin</artifactId>            <version>${scala-maven-plugin.version}</version>            <executions>                <execution>                    <goals>                        <goal>testCompile</goal>                    </goals>                    <configuration>                        <jvmArgs>                            <jvmArg>-Xss100M</jvmArg>                        </jvmArgs>                        <args>                            <arg>-deprecation</arg>                            <arg>-feature</arg>                            <arg>-unchecked</arg>                            <arg>-language:implicitConversions</arg>                            <arg>-language:postfixOps</arg>                        </args>                    </configuration>                </execution>            </executions>        </plugin>        <plugin>            <groupId>io.gatling</groupId>            <artifactId>gatling-maven-plugin</artifactId>            <version>${gatling-maven-plugin.version}</version>        </plugin>    </plugins></build>
复制代码


最后执行测试:

mvn gatling:test
复制代码


同样的场景可以用 Java DSL 来表达: 用Duration.ofSeconds(10)替代10,用status()替代status,用repeat(5).on(...)替代repeat(5){...}


public class BasicSimulationJava extends Simulation {
HttpProtocolBuilder httpProtocol = http .baseUrl("http://localhost:8080");
ScenarioBuilder scn = scenario("BasicSimulation") .repeat(5).on( exec(http("car").get("/car") .check(status().is(200))) .pause(1) .exec(http("carpart") .get("/carpart") .check(status().is(200))) );
{ setUp( scn.injectOpen(rampUsers(8).during(Duration.ofSeconds(10))) ).protocols(httpProtocol); }}
复制代码


虽然 Scala DSL 和 Java DSL 之间的这些变化看起来相对较小,但对用户来说最大的好处是与测试相关的所有自定义逻辑也可以用 Java 编写。


用户可以使用 Gatling 脚本来运行测试,或者可以使用构建工具。


最后一个示例是 Kotlin 的 Java DSL,其中最大的变化是使用status().shouldBe(200)替代 Java 示例中的status().200) :


class BasicSimulationKotlin : Simulation() {
val httpProtocol = http .baseUrl("http://localhost:8080");
val scn = scenario("BasicSimulation") .repeat(5).on( exec(http("car").get("/car") .check(status().shouldBe(200))) .pause(1) .exec(http("carpart") .get("/carpart") .check(status().shouldBe(200))) );
init { setUp( scn.injectOpen(rampUsers(8).during(Duration.ofSeconds(10))) ).protocols(httpProtocol); }}
复制代码


用户可以使用 Gatling 脚本来运行测试。另外,也可以使用像Kotlin Maven Plugin这样的构建插件,并在testSourceDirectory中指定测试场景文件的位置后:


<build>    <testSourceDirectory>src/test/kotlin</testSourceDirectory>    <plugins>        <plugin>            <groupId>org.jetbrains.kotlin</groupId>            <artifactId>kotlin-maven-plugin</artifactId>            <version>${kotlin.version}</version>
<executions> <execution> <id>compile</id> <goals> <goal>compile</goal> </goals> </execution> <execution> <id>test-compile</id> <goals> <goal>test-compile</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>io.gatling</groupId> <artifactId>gatling-maven-plugin</artifactId> <version>${gatling-maven-plugin.version}</version> </plugin> </plugins></build>
复制代码


更多信息可以在文档中找到,文档为每个功能提供了 Scala、 Java 和 Kotlin 示例。


原文链接

https://www.infoq.com/news/2023/09/gatling-java-dsl/

2023-10-05 08:006869

评论

发布
暂无评论
发现更多内容

uni-app 在mac电脑连接安卓手机进行真机调试

达摩

uni-app app调试

从头开始(概率)学HMM:精讲第一课-隐马尔可夫模型定义

herosunly

AI 引航计划 内容合集

从头开始(概率)学HMM:精讲第二课-学习问题(BW算法)

herosunly

AI 引航计划 内容合集

924清退在即,你的币转入数字货币钱包了吗?

CECBC

7. 简单生成器函数,Pool 实现多进程程序,异常管理,浏览器版本帮助手册

梦想橡皮擦

10月月更

电商秒杀系统

feitian

Vue3 + TypeScript 开发实践总结

程序员海军

大前端 Vue3 引航计划

JavaScript 脚本优化的 10 个技巧

devpoint

JavaScript 性能优化 10月月更

(mode4)千万级学生管理系统考试试卷存储方案

消失的子弹

架构 云原生

linux之history使用技巧

入门小站

Linux

原创万字总结人工智能技术栈与学习路线 下集 进阶篇

cv君

AI 引航计划

自我提升:高效能人士的7个习惯学习笔记

程序员架构进阶

自我管理 自我提升 10月月更

模块九作业

VE

架构实战营

原创万字总结人工智能技术栈与学习路线 上集 基础篇

cv君

AI 引航计划

AI驱动!7款开发者必备生产力工具

Jackpop

(实战篇)漫游语音识别技术—带你走进语音识别技术的世界

攻城先森

深度学习 音视频 nlp 语音识别

如何评估一个软件的项目费用?

石云升

项目管理 管理 引航计划 内容合集 10月月更

后端的另一种打开方式-路由还能这么玩~

Bob

微服务 后端 网络 服务 引航计划

为什么赛博朋克里总少不了日本元素?

脑极体

数据库:B/B+树

正向成长

B+树 B树

推荐2个网站,牛x就完事了!

Jackpop

如虎添翼!6款备受欢迎的Edge浏览器插件

Jackpop

架构实战营 模块九 作业

脉醉

架构实战营

设计电商秒杀系统

gawaine

架构训练营

中国法定数字货币(DCEP)全面启航!全国普及势在必行

CECBC

在线HTTP请求/响应头转JSON工具

入门小站

工具

浅谈人工智能的历史

Nydia

安全逆向分析实战

网络安全学海

Linux 网络安全 信息安全 WEB安全 漏洞分析

引航计划|AI|优质合集手把手带你玩转AI

Nydia

AI 引航计划

手把手带你做好团队管理|引航计划|管理

石云升

团队管理 管理 引航计划 技术专题合集

手把手带你做好项目管理|引航计划|管理

石云升

项目管理 管理 引航计划 技术专题合集

Gatling发布全新Java DSL,Java与Kotlin齐飞_编程语言_InfoQ精选文章