1- ### WebSocket Sampler Documentation
1+ ### WebSocket
22
33The ` DslWebsocketSampler ` class provides a Java DSL for creating WebSocket performance tests using JMeter. It supports the full WebSocket lifecycle including connection, data transmission, and disconnection operations. It is based on [ WebSocket Samplers by Peter Doornbosch] ( https://bitbucket.org/pjtr/jmeter-websocket-samplers/src/master/ ) plugin.
44
55To use it, add the following dependency to your project:
66
7+ :::: code-group
8+ ::: code-group-item Maven
79``` xml
810<dependency >
911 <groupId >us.abstracta.jmeter</groupId >
@@ -12,176 +14,55 @@ To use it, add the following dependency to your project:
1214 <scope >test</scope >
1315</dependency >
1416```
17+ :::
18+ ::: code-group-item Gradle
19+ ``` groovy
20+ testImplementation 'us.abstracta.jmeter:jmeter-java-dsl-websocket:2.2'
21+ ```
22+ :::
23+ ::::
1524
16- #### Main Components
17-
18- - ` webSocketSampler().connect() ` - Creates a WebSocket connection sampler
19- - ` webSocketSampler().connect(String url) ` - Creates a WebSocket connection sampler with URL parsing
20- - ` webSocketSampler().disconnect() ` - Creates a WebSocket disconnection sampler
21- - ` webSocketSampler().write() ` - Creates a WebSocket write sampler
22- - ` webSocketSampler().read() ` - Creates a WebSocket read sampler
23-
24- ##### Connect configuration
25-
26- - ` connectionTimeout(String timeout) ` - Sets connection timeout in milliseconds
27- - ` responseTimeout(String timeout) ` - Sets response timeout in milliseconds
28- - ` server(String server) ` - Sets the WebSocket server hostname
29- - ` port(String port) ` - Sets the WebSocket server port
30- - ` path(String path) ` - Sets the WebSocket path
31- - ` tls(boolean tls) ` - Enables/disables TLS encryption
32-
33- ##### URL Parsing
34-
35- The ` connect(String url) ` method automatically parses WebSocket URLs and extracts:
36- - Protocol (ws:// or wss://)
37- - Hostname
38- - Port (defaults to 80 for ws://, 443 for wss://)
39- - Path and query parameters
40- - TLS configuration
41-
42- ** Supported URL formats:**
43- - ` ws://localhost:8080/websocket `
44- - ` wss://example.com:8443/chat?room=general `
45- - ` wss://api.example.com/ws `
46-
47- ##### Disconnection configuration
48-
49- - ` responseTimeout(String timeout) ` - Sets response timeout in milliseconds
50- - ` statusCode(String statusCode) ` - Sets the close status code (e.g., "1000" for normal closure)
51-
52- ##### Write configuration
53-
54- - ` connectionTimeout(String timeout) ` - Sets connection timeout in milliseconds
55- - ` requestData(String requestData) ` - Sets the data to send
56- - ` createNewConnection(boolean createNewConnection) ` - Whether to create a new connection
57- - ` loadDataFromFile(boolean loadDataFromFile) ` - Whether to load data from a file
58-
59- ##### Read configuration
60-
61- - ` connectionTimeout(String timeout) ` - Sets connection timeout in milliseconds
62- - ` responseTimeout(String timeout) ` - Sets response timeout in milliseconds
63- - ` createNewConnection(boolean createNewConnection) ` - Whether to create a new connection
6425
65- #### Usage Examples
6626
67- ##### Basic WebSocket Test
27+ Following you can see a basic usage example of Web Socket protocol.
6828
6929``` java
7030import static us.abstracta.jmeter.javadsl.JmeterDsl.* ;
71- import us.abstracta.jmeter.javadsl.websocket.DslWebsocketSampler ;
31+ import static us.abstracta.jmeter.javadsl.websocket.DslWebsocketSampler.webSocketSampler ;
7232import us.abstracta.jmeter.javadsl.core.TestPlanStats ;
7333
7434public class Test {
7535 public static void main (String [] args ) throws Exception {
7636 TestPlanStats stats = testPlan(
7737 threadGroup(1 , 1 ,
78- // Connect to WebSocket server
79- webSocketSampler()
80- .connect(" wss://ws.postman-echo.com/raw" )
81- .connectionTimeout(" 10000" )
82- .responseTimeout(" 5000" ),
83-
84- // Send a message
85- webSocketSampler()
86- .write()
87- .requestData(" Hello WebSocket!" )
88- .createNewConnection(false ),
89-
90- // Read the response
91- webSocketSampler()
92- .read()
93- .responseTimeout(" 5000" )
94- .createNewConnection(false )
38+ webSocketSampler(). connect(" wss://ws.postman-echo.com/raw" ),
39+ webSocketSampler(). write(" Hello WebSocket!" ),
40+ webSocketSampler(). read()
9541 .children(
9642 responseAssertion()
9743 .equalsToStrings(" Hello WebSocket!" )
9844 ),
99-
100- // Close the connection
101- webSocketSampler()
102- .disconnect()
103- .responseTimeout(" 1000" )
104- .statusCode(" 1000" )
45+ webSocketSampler(). disconnect()
10546 )
10647 ). run();
10748 }
10849}
10950```
11051
111- ##### Manual Connection Configuration
112-
113- ``` java
114- webSocketSampler()
115- .connect()
116- .server(" localhost" )
117- .port(" 8080" )
118- .path(" /websocket" )
119- .tls(false )
120- .connectionTimeout(" 5000" )
121- .responseTimeout(" 3000" )
122- ```
123-
124- ##### Connection with Assertions
125-
126- ``` java
127- webSocketSampler()
128- .read()
129- .responseTimeout(" 5000" )
130- .createNewConnection(false )
131- .children(
132- responseAssertion()
133- .containsSubstrings(" expected response" )
134- )
135- ```
136-
137- #### Error Handling
138-
139- ##### Invalid URL Handling
140-
141- ``` java
142- // This will throw IllegalArgumentException
143- webSocketSampler(). connect(" http://localhost:80/test" );
144- ```
145-
146- The URL parser validates:
147- - Protocol must be ` ws:// ` or ` wss:// `
148- - Hostname is required
149- - Valid URI syntax
52+ ::: warning
53+ Only ` ws:// ` and ` wss:// ` protocols are supported. Using any other scheme will throw an ` IllegalArgumentException ` .
54+ :::
15055
151- ##### Connection Timeouts
152-
153- Configure appropriate timeouts to handle network issues:
56+ You can use a non blocking read if it is necessary in the following way
15457
15558``` java
156- webSocketSampler()
157- .connect(" wss://example.com/ws" )
158- .connectionTimeout(" 10000" ) // 10 seconds
159- .responseTimeout(" 5000" ) // 5 seconds
59+ webSocketSampler(). read(). waitForResponse(false )
16060```
16161
162- #### Best Practices
163-
164- 1 . ** Connection Reuse** : Set ` createNewConnection(false) ` for write/read operations to reuse existing connections
165- 2 . ** Timeout Configuration** : Always set appropriate timeouts to avoid hanging tests
166- 3 . ** Error Handling** : Use response assertions to validate WebSocket responses
167- 4 . ** URL Parsing** : Use the ` connect(String url) ` method for cleaner code when you have complete URLs
168- 5 . ** Status Codes** : Use standard WebSocket close codes (1000 for normal closure)
62+ ::: warning
63+ In this case is not recommended to add an assertion due the response could be empty
64+ :::
16965
170- #### Integration with Test Plans
171-
172- WebSocket samplers integrate seamlessly with other JMeter DSL components:
173-
174- ``` java
175- testPlan(
176- threadGroup(10 , 100 ,
177- // WebSocket operations
178- webSocketSampler(). connect(" wss://api.example.com/ws" ),
179- webSocketSampler(). write(). requestData(" test data" ),
180- webSocketSampler(). read(),
181- webSocketSampler(). disconnect(),
182- ),
183- // Results collection
184- jtlWriter(" results.jtl" ),
185- resultsTreeVisualizer()
186- )
187- ```
66+ ::: tip
67+ Web Socket protocol only supports one connection at a time. If you want to change Web Socket server during execution you should add a disconnect sampler and then establish a new connection.
68+ :::
0 commit comments