协作头脑风暴
brainstorming
在实现前澄清需求、探索设计选择,并形成清晰的开发方向。
gatling【开发与工程】Implements load testing strategies using Gatling for performance testing of web applications and microservices.
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
name: gatling description: Implements load testing strategies using Gatling for performance testing of web applications and microservices. license: MIT compatibility: opencode metadata: version: "1.1.1" domain: performance triggers: load testing, performance testing, Gatling, web applications, microservices archetypes: [implementation, evaluation] anti_triggers: [neglecting load test results, using outdated performance metrics] response_profile: verbosity: medium directive_strength: high abstraction_level: operational
role: implementation scope: implementation output-format: code
Load testing is critical for ensuring that applications can handle expected user loads without compromising performance. Gatling is a powerful tool for simulating a large number of requests and assessing application behavior under stress. Here are key practices for effective load testing with Gatling:
A sample scenario for load testing with Gatling might look like this:
import io.gatling.core.Predef._
import io.gatling.http.Predef._
val httpProtocol = http
.baseUrl("https://example.com")
.acceptHeader("application/json")
val scn = scenario("User Login")
.exec(http("Login Request")
.post("/login")
.body(StringBody("{\"username\":\"testuser\",\"password\":\"password123\"}"))
.check(status.is(200)))
setUp(
scn.inject(atOnceUsers(100)).protocols(httpProtocol)
)
Be sure to track key metrics such as peak response times, average response times, and error rates during load tests to evaluate performance.
By following structured load testing practices with Gatling, organizations can significantly enhance application reliability and performance under varying load conditions, thus assuring positive user experiences even during peak demand periods.
A production-grade simulation using CSV feeders, dynamic token extraction, multi-step user journeys, and realistic think time:
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import scala.concurrent.duration._
class AdvancedSimulation extends Simulation {
val httpProtocol = http
.baseUrl("https://api.example.com")
.acceptHeader("application/json")
.contentTypeHeader("application/json")
.userAgentHeader("Gatling-Performance-Test")
val userFeeder = csv("users.csv").circular
val productFeeder = Iterator.continually(Map(
"productId" -> (scala.util.Random.nextInt(1000) + 1)
))
val scn = scenario("Full User Journey")
.feed(userFeeder)
.exec(http("Login")
.post("/auth/login")
.body(StringBody("""{"username":"${username}","password":"${password}"}"""))
.check(
status.is(200),
jsonPath("$.token").saveAs("authToken")
))
.pause(2, 5) // realistic think time between 2-5 seconds
.exec(http("Browse Products")
.get("/products")
.header("Authorization", "Bearer ${authToken}")
.check(
status.is(200),
jsonPath("$.products[*]").count.gt(0)
))
.pause(1, 3)
.repeat(3) {
feed(productFeeder)
.exec(http("View Product Details")
.get("/products/${productId}")
.header("Authorization", "Bearer ${authToken}")
.check(
status.is(200),
jsonPath("$.name").exists
))
.pause(1, 2)
}
setUp(
scn.inject(
rampUsers(10).during(10.seconds),
constantUsersPerSec(5).during(60.seconds),
stressPeakUsers(50).during(30.seconds)
)
).protocols(httpProtocol)
}
pause), navigation paths, and conditional logicfeeders (CSV, JSON, Iterator) to parameterize test data and avoid cache-biased resultssetUp injection profiles: rampUsers, constantUsersPerSec, or stressPeakUsersreports and use Gatling's HTML dashboard to analyze response time percentiles, throughput, and errors--dry-run or a single-user runchecks on responses; every request should validate status, headers, or body to ensure correctness