|
| 1 | +/** |
| 2 | + * Gatling simulation script for load testing a WebSocket-based chatbot application. |
| 3 | + * |
| 4 | + * @module basicSimulation.gatling |
| 5 | + */ |
| 6 | + |
1 | 7 | import {
|
2 | 8 | simulation,
|
3 |
| - scenario, |
4 | 9 | constantUsersPerSec,
|
5 |
| - global, |
| 10 | + scenario, |
| 11 | + feed, |
| 12 | + pause, |
| 13 | + exec, |
| 14 | + repeat, |
| 15 | + regex, |
| 16 | + csv, |
6 | 17 | getParameter,
|
7 | 18 | } from "@gatling.io/core";
|
8 |
| -import { http } from "@gatling.io/http"; |
9 |
| - |
10 |
| -const toNumber = (value: string, fallback: number): number => { |
11 |
| - const parsed = Number(value); |
12 |
| - return Number.isFinite(parsed) ? parsed : fallback; |
13 |
| -}; |
| 19 | +import { http, ws } from "@gatling.io/http"; |
14 | 20 |
|
| 21 | +/** |
| 22 | + * Main Gatling simulation definition. |
| 23 | + */ |
15 | 24 | export default simulation((setUp) => {
|
16 |
| - const baseUrl = getParameter("baseUrl", "https://api-ecomm.gatling.io"); |
17 |
| - const usersPerSec = toNumber(getParameter("usersPerSec", "1"), 1); |
18 |
| - const durationSeconds = toNumber(getParameter("durationSeconds", "1"), 1); |
19 |
| - |
| 25 | + |
| 26 | + const baseUrl = getParameter("baseUrl", "http://localhost:3000"); |
| 27 | + const wsBaseUrl = getParameter("wsBaseUrl", "ws://localhost:3000"); |
| 28 | + const usersPerSec = parseInt(getParameter("usersPerSec", "2")); |
| 29 | + const durationSeconds = parseInt(getParameter("durationSeconds", "15")); |
| 30 | + const questionsFeeder = csv("resources/health_insurance_chatbot_questions.csv").random(); |
20 | 31 | const httpProtocol = http
|
21 | 32 | .baseUrl(baseUrl)
|
22 |
| - .acceptHeader("application/json") |
23 |
| - .userAgentHeader( |
24 |
| - "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36", |
25 |
| - ); |
26 |
| - |
27 |
| - const scn = scenario("Scenario").exec(http("Session").get("/session")); |
28 |
| - |
29 |
| - const assertion = global().failedRequests().count().lt(1.0); |
| 33 | + .acceptHeader("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8") |
| 34 | + .doNotTrackHeader("1") |
| 35 | + .acceptLanguageHeader("en-US,en;q=0.5") |
| 36 | + .acceptEncodingHeader("gzip, deflate") |
| 37 | + .userAgentHeader("Gatling2") |
| 38 | + .wsBaseUrl(wsBaseUrl); |
30 | 39 |
|
| 40 | + /** |
| 41 | + * @constant {ScenarioBuilder} scn - Scenario definition for WebSocket interaction. |
| 42 | + */ |
| 43 | + const scn = scenario("WebSocket") |
| 44 | + .exec( |
| 45 | + /** |
| 46 | + * Sends an HTTP GET request to the home page. |
| 47 | + */ |
| 48 | + http("Home").get("/"), |
| 49 | + pause(1), |
| 50 | + /** |
| 51 | + * Sets a unique user ID in the session. |
| 52 | + * |
| 53 | + * @param {Session} session - The Gatling session object. |
| 54 | + * @returns {Session} The updated session. |
| 55 | + */ |
| 56 | + exec((session) => session.set("id", "Gatling" + session.userId())), |
| 57 | + |
| 58 | + ws("Connect WS").connect("/"), |
| 59 | + |
| 60 | + pause(1), |
| 61 | + /** |
| 62 | + * Determines a random number of customer questions for this session. |
| 63 | + * |
| 64 | + * @param {Session} session - The Gatling session object. |
| 65 | + * @returns {Session} The updated session with a random question count. |
| 66 | + */ |
| 67 | + exec((session) => |
| 68 | + session.set("maxQuestions", Math.floor(Math.random() * 10) + 1), |
| 69 | + ), |
| 70 | + /** |
| 71 | + * Repeats sending customer questions and awaiting chatbot responses. |
| 72 | + * |
| 73 | + * @param {number} i - The iteration index. |
| 74 | + */ |
| 75 | + repeat((session) => session.get("maxQuestions"), "i").on( |
| 76 | + feed(questionsFeeder), |
| 77 | + /** |
| 78 | + * Sends a user question over WebSocket and awaits a response. |
| 79 | + */ |
| 80 | + ws("Customer Question") |
| 81 | + .sendText((session) => session.get("user_question")) |
| 82 | + .await(30).on( |
| 83 | + /** |
| 84 | + * Checks for a chatbot response message matching any text. |
| 85 | + */ |
| 86 | + ws.checkTextMessage("Chatbot Response").check(regex("(.*)")), |
| 87 | + ), |
| 88 | + ), |
| 89 | + |
| 90 | + pause(1), |
| 91 | + |
| 92 | + ws("Close WS").close(), |
| 93 | + ); |
| 94 | + /** |
| 95 | + * Sets up the scenario with the specified injection profile and protocol. |
| 96 | + */ |
31 | 97 | setUp(
|
32 | 98 | scn.injectOpen(constantUsersPerSec(usersPerSec).during(durationSeconds)),
|
33 |
| - ) |
34 |
| - .assertions(assertion) |
35 |
| - .protocols(httpProtocol); |
| 99 | + ).protocols(httpProtocol); |
36 | 100 | });
|
0 commit comments