写点什么

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:006819

评论

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

在线脑图思维导图生成工具

入门小站

工具

设计消息队列存储消息数据的 MySQL 表格

孙强

架构师实战营

HttpClient使用详解与实战一:普通的GET和POST请求

乌龟哥哥

4月月更

Linux驱动开发-内核定时器

DS小龙哥

4月月更

EasyRecovery15数据恢复软件

茶色酒

EasyRecovery15

泛型真的会让程序变慢吗?(Go1.18新特性)

蔡超

golang 编程 编程、 Go 语言

王者荣耀商城异地多活架构设计

Fingal

架构实战营

Android C++系列:C++最佳实践4多重继承与虚继承

轻口味

c++ android 4月月更

一文了解异步编程基础

宇宙之一粟

Python 异步编程 4月月更

15 张图 | 深入理解 OpenFeign 远程调用的架构原理

悟空聊架构

Feign 4月日更 悟空聊架构 openfeign 4月月更

关于OpenHarmony3.1,想随便聊一点

坚果

OpenHarmony 4月月更

深入解析JVM-Java对象头组成

janyxe

Java JVM Java内存布局 Java对象头 Java对象组成

2020年Mybatis常见面试题总结(附答案)

爱好编程进阶

Java 面试 后端开发

[Day17]-[动态规划]打家劫舍

方勇(gopher)

LeetCode 数据结构和算法

为什么选择学习 Sanic 框架

宇宙之一粟

4月月更 sanic

【PIMF】OpenHarmony啃论文俱乐部——“六脉神剑”详解

离北况归

OpenHarmony Openharmony啃论文俱乐部 六脉神剑

消息队列存储消息数据的 MySQL 表格

AragornYang

架构训练营 架构实战营

13W字!2021最新发布互联网大厂高频面试技术点!

爱好编程进阶

Java 面试 后端开发

2020年最具影响力的4种编程语言 平均薪资20K+

爱好编程进阶

Java 面试 后端开发

在线CSV转多行数据工具

入门小站

工具

Dio —— Flutter 网络请求之王者

岛上码农

flutter 移动端开发 4月月更 跨平台开发 安卓 ios

残酷春天里的中国科技(三):持续缠绕的科技封锁线

脑极体

AirServer2022苹果mac电脑投屏软件工具

茶色酒

AirServer

linux之read命令

入门小站

残酷春天里的中国科技(二):和全球供应链一起“仰卧起坐”

脑极体

下单流程解耦新方案-你知道Spring事件监听机制吗

越长大越悲伤

事件驱动 SpringBoot 2 实战

15 高可用网站的软件质量保证

爱好编程进阶

Java 面试 后端开发

如何从 Java 的 List 中删除第一个元素

HoneyMoose

学生管理系统详细架构设计文档

哈喽

「架构实战营」

Tuxera NTFS Mac2022磁盘读写工具

茶色酒

Tuxera NTFS Mac2022

10个经典场景带你玩转SQL优化

爱好编程进阶

Java 面试 后端开发

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