Skip to content

Commit b61537a

Browse files
committed
update simulation files
1 parent 52ec570 commit b61537a

File tree

2 files changed

+95
-95
lines changed

2 files changed

+95
-95
lines changed

articles/websocket-chatbot-js/gatling/javascript/src/chatbotSimulation.gatling.js

Lines changed: 8 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -18,67 +18,16 @@ import {
1818
} from "@gatling.io/core";
1919
import { http, ws } from "@gatling.io/http";
2020

21-
/**
22-
* Converts a value to a number, returning a fallback if conversion fails.
23-
*
24-
* @param {*} value - The value to convert.
25-
* @param {number} fallback - The fallback value if conversion fails.
26-
* @returns {number} The converted number or the fallback.
27-
*/
28-
const toNumber = (value, fallback) => {
29-
// ...
30-
};
31-
32-
/**
33-
* Derives a WebSocket URL from an HTTP URL, or returns a fallback if invalid.
34-
*
35-
* @param {string} httpUrl - The HTTP URL to convert.
36-
* @param {string} fallback - The fallback WebSocket URL.
37-
* @returns {string} The derived WebSocket URL or the fallback.
38-
*/
39-
const deriveWebSocketUrl = (httpUrl, fallback) => {
40-
// ...
41-
};
42-
4321
/**
4422
* Main Gatling simulation definition.
45-
*
46-
* @function
47-
* @param {Function} setUp - Gatling setup function for configuring injection profiles.
48-
* @returns {void}
4923
*/
5024
export default simulation((setUp) => {
51-
/**
52-
* @constant {string} baseUrl - The base HTTP URL for the application under test.
53-
*/
25+
5426
const baseUrl = getParameter("baseUrl", "http://localhost:3000");
55-
56-
/**
57-
* @constant {string} wsBaseUrl - The base WebSocket URL for the application under test.
58-
*/
59-
const wsBaseUrl = getParameter(
60-
"wsBaseUrl",
61-
deriveWebSocketUrl(baseUrl, "ws://localhost:3000"),
62-
);
63-
64-
/**
65-
* @constant {number} usersPerSec - Number of users per second to inject.
66-
*/
67-
const usersPerSec = toNumber(getParameter("usersPerSec", "2"), 2);
68-
69-
/**
70-
* @constant {number} durationSeconds - Duration of the test in seconds.
71-
*/
72-
const durationSeconds = toNumber(getParameter("durationSeconds", "15"), 15);
73-
74-
/**
75-
* @constant {Feeder} questionsFeeder - Feeder for user questions from a CSV file.
76-
*/
27+
const wsBaseUrl = getParameter("wsBaseUrl", "ws://localhost:3000");
28+
const usersPerSec = parseInt(getParameter("usersPerSec", "2"));
29+
const durationSeconds = parseInt(getParameter("durationSeconds", "15"));
7730
const questionsFeeder = csv("resources/health_insurance_chatbot_questions.csv").random();
78-
79-
/**
80-
* @constant {HttpProtocolBuilder} httpProtocol - HTTP protocol configuration for Gatling.
81-
*/
8231
const httpProtocol = http
8332
.baseUrl(baseUrl)
8433
.acceptHeader("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
@@ -97,9 +46,6 @@ export default simulation((setUp) => {
9746
* Sends an HTTP GET request to the home page.
9847
*/
9948
http("Home").get("/"),
100-
/**
101-
* Pauses for 1 second.
102-
*/
10349
pause(1),
10450
/**
10551
* Sets a unique user ID in the session.
@@ -108,13 +54,9 @@ export default simulation((setUp) => {
10854
* @returns {Session} The updated session.
10955
*/
11056
exec((session) => session.set("id", "Gatling" + session.userId())),
111-
/**
112-
* Opens a WebSocket connection.
113-
*/
57+
11458
ws("Connect WS").connect("/"),
115-
/**
116-
* Pauses for 1 second.
117-
*/
59+
11860
pause(1),
11961
/**
12062
* Determines a random number of customer questions for this session.
@@ -144,21 +86,15 @@ export default simulation((setUp) => {
14486
ws.checkTextMessage("Chatbot Response").check(regex("(.*)")),
14587
),
14688
),
147-
/**
148-
* Pauses for 1 second.
149-
*/
89+
15090
pause(1),
151-
/**
152-
* Closes the WebSocket connection.
153-
*/
91+
15492
ws("Close WS").close(),
15593
);
156-
15794
/**
15895
* Sets up the scenario with the specified injection profile and protocol.
15996
*/
16097
setUp(
16198
scn.injectOpen(constantUsersPerSec(usersPerSec).during(durationSeconds)),
16299
).protocols(httpProtocol);
163100
});
164-
Lines changed: 87 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,100 @@
1+
/**
2+
* Gatling simulation script for load testing a WebSocket-based chatbot application.
3+
*
4+
* @module basicSimulation.gatling
5+
*/
6+
17
import {
28
simulation,
3-
scenario,
49
constantUsersPerSec,
5-
global,
10+
scenario,
11+
feed,
12+
pause,
13+
exec,
14+
repeat,
15+
regex,
16+
csv,
617
getParameter,
718
} 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";
1420

21+
/**
22+
* Main Gatling simulation definition.
23+
*/
1524
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();
2031
const httpProtocol = http
2132
.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);
3039

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+
*/
3197
setUp(
3298
scn.injectOpen(constantUsersPerSec(usersPerSec).during(durationSeconds)),
33-
)
34-
.assertions(assertion)
35-
.protocols(httpProtocol);
99+
).protocols(httpProtocol);
36100
});

0 commit comments

Comments
 (0)