2025上半年,最新 AI实践都在这!20+ 应用案例,任听一场议题就值回票价 了解详情
写点什么

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

评论

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

Linux之bc命令

入门小站

Linux

vue入门:router路由简介与使用

小鲍侃java

8月日更

人工智能下的音频还能这样玩!!!!

Python研究者

8月日更

【Flutter 专题】61 图解基本 Button 按钮小结 (一)

阿策小和尚

Flutter 小菜 0 基础学习 Flutter Android 小菜鸟 8月日更

elasticsearch7.13.4 ik中文分词器安装

Rubble

8月日更

Go 语言, 一文彻底搞懂 iota 实现原理

微客鸟窝

Go 语言 8月日更

Ansible 变量

耳东@Erdong

变量 ansible 8月日更

Netty如何解决粘包以及拆包问题

慕枫技术笔记

后端 Netty

MySQL 系列教程之(十)索引原理:B+ 树与索引

若尘

MySQL 数据库 8月日更

博客升级之在线代码编辑器

devpoint

vscode 编辑器 8月日更

白手起家之注册中心zookeeper

卢卡多多

ZooKeeper原理 8月日更

手撸二叉树之根据二叉树创建字符串

HelloWorld杰少

数据结构与算法 8月日更

腾讯良心了?!!!

Jackpop

硬盘空间免费扩容了2TB!!!

Jackpop

Go,一文搞懂 string 实现原理

微客鸟窝

Go 语言 8月日更

ndk-build 脚本

Changing Lin

8月日更

管道(Channel)的读取与写入「让我们一起Golang」

Regan Yue

协程 Go 语言 8月日更 管道

服务发现机制SPI居然是破坏者!

4ye

Java 源码 后端 sping 8月日更

Go字符串拼接最佳实践

Rayjun

Go 语言

iOS开发:开发过程中单例模式的使用

三掌柜

8月日更 8月

【设计模式】访问者模式

Andy阿辉

C# 后端 设计模式 8月日更

在线HTML5,CSS3,VueJS,jQuery运行测试练习工具

入门小站

工具

帮小姐姐打分系统的模型创建,滚雪球学 Python 第三轮第 11 篇

梦想橡皮擦

8月日更

你可能不知道的css动画性能

云小梦

CSS layout requestAnimationFrame 重排和重绘 will-change

万企明道成立八周年,邀你书写留言!

明道云

【Vue2.x 源码学习】第四十二篇 - 组件部分 - 组件挂载流程简述

Brave

源码 vue2 8月日更

从0开始的TypeScriptの十二:装饰器

空城机

JavaScript typescript 大前端 8月日更

程序员必备!5款小众高效的开发神器

Jackpop

缓存函数的简单使用

编程三昧

JavaScript 8月日更

低品质的勤奋者

箭上有毒

8月日更

☕【Java技术指南】「并发原理专题」AQS的技术体系之CLH、MCS锁的原理及实现

码界西柚

AQS 8月日更 MCS锁 CLH锁

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