Token导航 LogoToken导航TokenDH.com
开发规范权限需确认github未标认证来源可访问许可证需确认审计未展示

gatling-best-practices加特林最佳实践

Agent Skill

用于辅助测试设计、自动化测试、用例整理和回归验证。它适合让 Agent 编写单元测试、端到端测试、测试计划或根据失败日志定位问题。使用时需要确认项目测试框架、运行命令和夹具数据,避免为了通过测试而改坏真实逻辑;涉及浏览器或外部服务时,应区分本地模拟、测试环境和生产环境。

总安装

539

周安装

22

GitHub Stars

5

下载量

172
CodexClaudeCursorGemini CLI

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

GitHub

来源数

2

许可证

unknown

最后核验

2026-05-01

来源状态

来源可访问

安装方式

通过对话安装

复制提示词发给支持本地命令或 Skills 的 AI 助手,先确认命令和权限,再让它执行。

请帮我安装这个 Agent Skill:gatling-best-practices(加特林最佳实践)
来源仓库:https://github.com/rcampos09/performance-testing-skills
仓库路径:skills/gatling-best-practices
安装命令:
npx skills add https://github.com/rcampos09/performance-testing-skills --skill gatling-best-practices
安装前请先检查当前环境是否支持对应 CLI,并向我确认将要执行的命令、安装目录、联网范围和文件读写权限;确认后再执行。

命令行安装

复制命令到本机终端执行。该命令会通过 npx skills 从第三方来源获取 Skill;本站只展示命令,不托管安装包,也不自动执行。

skills.shnpx skills
npx skills add https://github.com/rcampos09/performance-testing-skills --skill gatling-best-practices

简介

用于辅助测试设计和自动化用例整理。gatling-best-practices 属于开发规范类 Skill,可作为该场景下的辅助能力补充。

  • 适合编写单元测试、端到端测试或测试计划。
  • 使用时需确认项目测试框架和运行命令,避免破坏逻辑。
  • 涉及浏览器或外部服务时应区分模拟与生产环境。
  • 建议优先使用已有夹具数据,确保测试有效性。

SKILL.md

Gatling Scenario Builder

Enforces a consistent, production-ready pattern for Gatling simulations across all five officially supported languages: Java, Kotlin, Scala, JavaScript, and TypeScript.

Output Format

When producing or fixing a simulation, always deliver three things:

  1. A complete, runnable simulation file for the chosen language — never a partial snippet. The user should be able to copy it and run it immediately.
  2. The exact run command with the environment parameters needed (baseUrl, users, etc.).
  3. A one-line explanation of the injection profile chosen and why it fits the stated load goal.

Step 1 — Gather context

Ask only what is unknown. Typical questions:

  • Language: Java (most common) · Kotlin · Scala · JavaScript · TypeScript
  • Build tool: Maven · Gradle · npm (JS/TS only)
  • Protocol: HTTP/REST · WebSocket · MQTT · JMS
  • Goal: New project from scratch · Fix existing simulation · Specific DSL question

Only load references/PROTOCOLS.md when the user mentions WebSocket, MQTT, or JMS. Contains dependency declarations and DSL for those protocols — non-obvious setup steps the user will likely miss.

Only load references/DESIGN-PATTERNS.md when the user asks about folder structure, project architecture, separation of concerns, or how to scale beyond a single simulation file. Contains the modular layered pattern (Config → Requests → Scenarios → Simulations) with examples in Java, Scala, and TypeScript.

Step 2 — New project? Use the scaffold script

The scaffold script is interactive — it requires keyboard input and cannot be run non-interactively. Tell the user to run it themselves in their terminal; do NOT attempt to execute it with the Bash tool:

# macOS / Linux
bash scripts/scaffold.sh

# Windows
.\scripts\scaffold.ps1

If the user asks you to generate the project directly (without running the script), skip to Step 3 and create the files manually following the 5-block pattern.

Kotlin + Gradle — minimum required files:

build.gradle.kts — three omissions cause build failures every time:

plugins {
    kotlin("jvm") version "2.0.21"              // ① must come BEFORE the Gatling plugin
    id("io.gatling.gradle") version "3.14.5"
}

group = "perf"
version = "1.0.0-SNAPSHOT"

repositories {
    mavenCentral()                               // ② required — often forgotten
}

gatling {
    jvmArgs = listOf(
        "-Xms512m", "-Xmx2g",
        "--add-opens=java.base/java.lang=ALL-UNNAMED"  // ③ Java 21+ module restriction
    )
}

dependencies {
    gatling("io.gatling.highcharts:gatling-charts-highcharts:3.14.5")
}

Source directories (Gatling Gradle plugin — not src/test/):

  • Kotlin simulations → src/gatling/kotlin/<package>/
  • Resources (gatling.conf, feeders) → src/gatling/resources/

For JS/TS projects, the minimum required files are:

  • package.json with "type": "module" and deps @gatling.io/cli, @gatling.io/core, @gatling.io/http
  • src/<SimulationName>.gatling.js (or .gatling.ts) — file must be in src/ directly with the .gatling.js / .gatling.ts suffix (required by the CLI)
  • tsconfig.jsonTypeScript only, required by esbuild-plugin-tsc; omitting it causes failed to open 'undefined' at bundle time

After scaffolding — or whenever the user shares an existing Gatling project — execute the validator with the Bash tool (do not read it, only the output consumes tokens):

bash scripts/validate.sh [project-dir]

Report the results. The script catches the five most common configuration problems before the user wastes time on a broken run.

Step 3 — Apply the 5-block pattern

Every simulation must have these five blocks in this order. Generate the complete skeleton for the chosen language first, then fill in the details — starting from a partial file leads to structural errors.

Block 1 → Protocol    baseUrl, headers, connection settings
Block 2 → Feeders     test data injected per virtual user
Block 3 → Scenario    ordered chain of requests with pauses and checks
Block 4 → Injection   how many users, at what rate, for how long
Block 5 → Assertions  pass/fail thresholds (success rate, response time p95)

Common Mistakes — Check Every Simulation for These

These five errors appear in almost every first-draft Gatling simulation. Scan for them before delivering any code.

1. Missing pause() between requests

Without think time, all requests fire at the maximum possible rate, generating 10–100× more load than real users would. This makes results meaningless and can crash the system under test.

// Wrong
scenario("Flow").exec(http("A").get("/a")).exec(http("B").get("/b"))

// Correct — add realistic think time between actions
scenario("Flow").exec(http("A").get("/a")).pause(1, 3).exec(http("B").get("/b"))

2. Hardcoded dynamic tokens

Hardcoded tokens mean every virtual user sends the same session — the server sees one user repeated, not many distinct users. CSRF tokens and JWTs are server-side validated; they must come from the actual login response.

// Wrong — static token shared across all users
.header("Authorization", "Bearer eyJhbGciOiJIUzI1NiJ9.abc123")

// Correct — extract per user from the login response
.exec(http("Login").post("/auth/login")
    .check(jsonPath("$.token").saveAs("token")))
.exec(http("API Call").get("/data")
    .header("Authorization", "Bearer #{token}"))

3. atOnceUsers for load tests

atOnceUsers fires all users simultaneously. It is only appropriate for smoke tests (2–5 users). Using it for real load tests generates an unrealistic spike that tells you nothing about capacity.

// Wrong — not a load test, just a spike
setUp(scn.injectOpen(atOnceUsers(100)))

// Correct — ramp up, then hold to measure steady-state capacity
setUp(scn.injectOpen(
    rampUsers(100).during(60),
    constantUsersPerSec(10).during(120)
))

4. No assertions

Without assertions, Gatling exits with code 0 (success) even if every request returns 500. This means CI/CD pipelines never catch performance regressions. Define what "passing" looks like before the test runs.

// Wrong — always exits 0 regardless of results
setUp(scn.inject(...).protocols(httpProtocol))

// Correct — fail the build if thresholds are breached
setUp(scn.inject(...).protocols(httpProtocol))
    .assertions(
        global().successfulRequests().percent().gt(99.0),
        global().responseTime().percentile(95).lt(1000)
    )

5. .queue() feeder strategy for long tests

.queue() consumes each CSV record once, in order. When the file runs out, the test fails mid-run. Use .circular() for any test that may run longer than the number of records allows.

// Wrong — crashes when file is exhausted
FeederBuilder<String> f = csv("data/users.csv").queue()

// Correct for sustained tests — loops back to the start
FeederBuilder<String> f = csv("data/users.csv").circular()

// Use .queue() only when each record must be unique (e.g., user registration)

6. Check failure marks the request as FAILED

A .check() that doesn't find its target fails the entire request — even if the server responded 200. This silently inflates error rates and hides the real problem: the field was absent or the path was wrong.

// Wrong — if $.token is absent (e.g., login failed), request is marked FAILED
.check(jsonPath("$.token").saveAs("token"))

// Correct — validate existence first so the error message is meaningful
.check(status().is(200))
.check(jsonPath("$.token").exists())
.check(jsonPath("$.token").saveAs("token"))

// When the field is genuinely optional — use .optional() to avoid false failures
.check(jsonPath("$.refreshToken").optional().saveAs("refreshToken"))

7. Missing Content-Type when sending a request body

Forgetting Content-Type on POST/PUT requests causes the server to reject with 415 Unsupported Media Type. Use .asJson() — it sets both Content-Type and Accept headers in one call.

// Wrong — server returns 415
.post("/api/users").body(StringBody("""{"name":"#{name}"}"""))

// Correct — use .asJson() shorthand (Java / Kotlin only)
.post("/api/users").body(StringBody("""{"name":"#{name}"}""")).asJson()

// Equivalent explicit form
.post("/api/users")
    .header("Content-Type", "application/json")
    .body(StringBody("""{"name":"#{name}"}"""))

Scala note: .asJson() does not chain after .body() in Scala — it causes a compile error. Set Content-Type on the protocol instead:

// Scala — set contentTypeHeader on the protocol, not per-request
val httpProtocol = http
  .baseUrl(sys.props.getOrElse("baseUrl", "https://api.example.com"))
  .acceptHeader("application/json")
  .contentTypeHeader("application/json")  // applies to all requests

// Then just use .body() without .asJson():
http("POST Login").post("/api/auth/login")
  .body(StringBody("""{"email":"#{email}","password":"#{password}"}"""))
  .check(status.is(200))

The 5-Block Pattern — Reference

Block 1: Protocol

// Java / Kotlin
HttpProtocolBuilder httpProtocol = http
    .baseUrl(System.getProperty("baseUrl", "https://api.example.com"))
    .acceptHeader("application/json")
    .contentTypeHeader("application/json");
// Scala
val httpProtocol = http
  .baseUrl(sys.props.getOrElse("baseUrl", "https://api.example.com"))
  .acceptHeader("application/json")
// TypeScript / JavaScript
// File must be named *.gatling.ts / *.gatling.js and placed directly in src/
import {
  simulation, scenario, rampUsers, csv, global,
  StringBody, jsonPath, getEnvironmentVariable,  // ← jsonPath and StringBody live here, NOT in @gatling.io/http
} from "@gatling.io/core";
import { http, status } from "@gatling.io/http";  // ← do NOT import jsonPath from here — it is not exported

// process.env is NOT available at GraalVM runtime — use getEnvironmentVariable instead
const httpProtocol = http
  .baseUrl(getEnvironmentVariable("BASE_URL", "https://api.example.com"))
  .acceptHeader("application/json");

Block 2: Feeders

csv("data/users.csv").circular()  // sustained tests: loops forever (recommended)
csv("data/users.csv").random()    // picks records randomly, allows repeats
csv("data/users.csv").queue()     // each record used once — only for unique data
csv("data/users.csv").shuffle()   // random order, each used once

// Programmatic feeder — when each user needs a unique generated value
Iterator<Map<String, Object>> feeder =
    Stream.generate(() -> Map.<String, Object>of("id", UUID.randomUUID().toString()))
          .iterator();

Block 3: Scenario

// Java
ScenarioBuilder scn = scenario("My Flow")
    .feed(userFeeder)
    .exec(http("POST Login")
        .post("/auth/login")
        .body(StringBody("""{"username":"#{username}","password":"#{password}"}"""))
        .check(status().is(200))
        .check(jsonPath("$.token").saveAs("token")))   // extract token for reuse
    .pause(1, 3)                                        // think time
    .exec(http("GET Data")
        .get("/data")
        .header("Authorization", "Bearer #{token}")    // inject extracted token
        .check(status().is(200))
        .check(jsonPath("$.id").saveAs("resourceId")))
    .pause(1)
    .exec(http("POST Action")
        .post("/actions")
        .header("Authorization", "Bearer #{token}")
        .body(StringBody("""{"resourceId":"#{resourceId}"}"""))
        .check(status().is(201)));
// TypeScript
const scn = scenario("My Flow")
  .feed(userFeeder)
  .exec(http("POST Login").post("/auth/login")
    .body(StringBody('{"username":"#{username}","password":"#{password}"}'))  // ← StringBody required; raw string causes NullPointerException
    .asJson()                                                                   // ← sets Content-Type: application/json
    .check(status().is(200))
    .check(jsonPath("$.token").exists())
    .check(jsonPath("$.token").saveAs("token")))
  .pause(1, 3)
  .exec(http("GET Data").get("/data")
    .header("Authorization", "Bearer #{token}")
    .check(status().is(200)));

Loops and conditionals:

repeat(3).on(exec(http("Poll").get("/status")))                    // fixed iterations
during(Duration.ofSeconds(30)).on(                                  // time-based loop
    exec(http("Ping").get("/ping")).pause(5))
doIf("#{isPremium}").then(exec(http("VIP").get("/vip")))           // conditional branch
randomSwitch().on(                                                   // weighted paths
    percent(60.0).exec(http("Browse").get("/products")),
    percent(40.0).exec(http("Search").get("/search")))
group("Checkout Flow").on(                                          // group for cleaner reports
    exec(http("Cart").get("/cart"))
        .exec(http("Pay").post("/pay")))

Block 4: Injection Profiles

Choose the profile that matches the test goal — using the wrong one produces misleading results.

injectOpen — controls *arrival rate* (new users/second). Default for web APIs and stateless services.

ProfileCommandWhen to use
Spike (smoke only)atOnceUsers(5)Verify the test runs — not a load test
RamprampUsers(100).during(60)Standard load test
Steady rateconstantUsersPerSec(20).during(120)Capacity / soak test
AcceleratingrampUsersPerSec(5).to(50).during(60)Finding the breaking point
Stress peakstressPeakUsers(500).during(30)Stress test
StairsincrementUsersPerSec(5).times(5).eachLevelLasting(30)Progressive capacity

injectClosed — controls *concurrent count* (users active simultaneously). Use for systems with connection pools, queues, or session limits.

ProfileCommandWhen to use
Constant concurrentconstantConcurrentUsers(50).during(120)Fixed connection pool size
Ramp concurrentrampConcurrentUsers(10).to(50).during(60)Gradual concurrency increase
Stairs concurrentincrementConcurrentUsers(5).times(5).eachLevelLasting(30)Progressive capacity (closed)

Scala note: use .inject(...) — Scala has no injectOpen/injectClosed distinction at the call site; the step type determines the model.

Throttling — cap RPS regardless of user count:

Use .throttle() when the goal is to test at a fixed request rate rather than a fixed user count. It overrides injection and is useful for SLA compliance tests.

setUp(scn.injectOpen(constantUsersPerSec(50).during(Duration.ofMinutes(10))))
    .throttle(
        reachRps(100).in(Duration.ofSeconds(10)),  // ramp to 100 RPS over 10s
        holdFor(Duration.ofMinutes(5))              // hold at 100 RPS for 5 min
    )
    .protocols(httpProtocol);

Pause distributions — choose based on realism needed:

.pause(1, 3)                                    // uniform: between 1-3s (default)
.pause(Duration.ofSeconds(2),
       PauseType.EXPONENTIAL)                   // exponential: closer to real user behavior
.pace(Duration.ofSeconds(5))                    // cadence: fixed cycle regardless of response time

Block 5: Assertions

Assertions turn the test into a pass/fail gate. Without them, the test is just an observation. Include at minimum the first line; add per-endpoint assertions for critical paths.

.assertions(
    global().failedRequests().count().lt(1L),           // minimum: zero errors
    global().successfulRequests().percent().gt(99.0),   // success rate
    global().responseTime().percentile(95).lt(1000),    // p95 < 1s
    global().responseTime().percentile(99).lt(2000),    // p99 < 2s
    global().requestsPerSec().gt(50.0),                 // throughput floor
    details("POST Login").responseTime().percentile(99).lt(500)  // per-endpoint
)

Use percentile(95) and percentile(99), not mean(). Mean hides the tail: a p99 of 10 seconds is invisible when mean is 200ms.

Kotlin note: percentile() requires a Double argument — write .percentile(95.0).lt(1000), not .percentile(95).lt(1000) (Int causes a compile error in Kotlin even though Java accepts it via widening).

Scala note: assertion methods (global, responseTime, successfulRequests, percent) are zero-arg methods with an implicit GatlingConfiguration parameter. Call them without parenthesesglobal() is a compile error in Scala:

// Wrong — compile error: "not enough arguments for method global"
global().successfulRequests().percent().gt(99.0)

// Correct — no () on zero-arg methods with implicit params
global.successfulRequests.percent.gt(99.0)
global.responseTime.percentile(95).lt(800)
details("POST Login").responseTime.percentile(99).lt(300)

Run Commands

# Maven
mvn gatling:test -Dgatling.simulationClass=perf.MySimulation \
                 -DbaseUrl=https://staging.example.com -Dusers=50

# Gradle — plugin 3.14+ supports Gradle 9; use ./gradlew (or gradle) directly
./gradlew gatlingRun                              # runs all simulations
./gradlew gatlingRun-perf.MySimulation            # specific class (plugin ≥ 3.14 only)

# TypeScript / JavaScript  (use simulation name, not file path)
BASE_URL=https://staging.example.com USERS=50 \
  npx gatling run --simulation MySimulation

Reports open at: target/gatling/<simulation>-<timestamp>/index.html


References

适合场景

01

用户想查找某类 Agent Skill 时

02

需要根据任务场景推荐可安装能力包时

03

需要对比不同来源的安装命令和来源信息时

能力概览

能力 1

按任务关键词查找相关 Skills

能力 2

展示可复制的安装命令

能力 3

保留来源站点、仓库和原始说明,方便继续核验

安装后应在对应宿主中按原始 README 的触发条件使用;具体调用方式请以来源页面和 README 为准。

平台分布

Codex

38.8%
按下载量换算67

Claude

32.18%
按下载量换算55

Cursor

17.13%
按下载量换算29

Gemini CLI

8.48%
按下载量换算15

安全审计

暂无安全审计结果可展示。

权限和风险

权限需确认

当前来源未能明确判断权限范围,默认进入异常复核队列。

安装前确认

本站仅展示第三方公开信息,不托管安装包,不提供自动安装或运行环境。安装前应自行审查源码、依赖和命令行为。当前只有一个来源,正式发布前建议补源仓库或其他目录站核验。

来源信息

继续浏览同类 Skills