Skip to content

Commit 94c2da3

Browse files
Refactors on web socket sampler
1 parent 4a6723a commit 94c2da3

File tree

13 files changed

+195
-157
lines changed

13 files changed

+195
-157
lines changed

docs/guide/protocols/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
<!-- @include: jdbc.md -->
66
<!-- @include: java.md -->
77
<!-- @include: selenium.md -->
8+
<!-- @include: websocket.md -->
Lines changed: 43 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,27 @@
1-
# WebSocket Sampler Documentation
1+
### WebSocket Sampler Documentation
22

3-
## Overview
3+
The `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

5-
The `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.
5+
To use it, add the following dependency to your project:
66

7-
## Features
8-
9-
- **Connection Management**: Establish and close WebSocket connections
10-
- **Data Transmission**: Send and receive WebSocket messages
11-
- **URL Parsing**: Automatic parsing of WebSocket URLs (ws:// and wss://)
12-
- **Timeout Configuration**: Configurable connection and response timeouts
13-
- **TLS Support**: Secure WebSocket connections with WSS protocol
14-
- **Fluent API**: Method chaining for easy configuration
15-
16-
## Main Components
17-
18-
### 1. DslWebsocketSampler (Main Class)
19-
20-
The main class that provides static factory methods for creating different types of WebSocket samplers.
21-
22-
#### Static Methods
23-
24-
- `webSocketSampler()` - Creates a basic WebSocket sampler
25-
- `connect()` - Creates a WebSocket connection sampler
26-
- `connect(String url)` - Creates a WebSocket connection sampler with URL parsing
27-
- `disconnect()` - Creates a WebSocket disconnection sampler
28-
- `write()` - Creates a WebSocket write sampler
29-
- `read()` - Creates a WebSocket read sampler
7+
```xml
8+
<dependency>
9+
<groupId>us.abstracta.jmeter</groupId>
10+
<artifactId>jmeter-java-dsl-websocket</artifactId>
11+
<version>2.2</version>
12+
<scope>test</scope>
13+
</dependency>
14+
```
3015

31-
### 2. DslConnectSampler (Connection Operations)
16+
#### Main Components
3217

33-
Handles WebSocket connection establishment.
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
3423

35-
#### Configuration Methods
24+
##### Connect configuration
3625

3726
- `connectionTimeout(String timeout)` - Sets connection timeout in milliseconds
3827
- `responseTimeout(String timeout)` - Sets response timeout in milliseconds
@@ -41,7 +30,7 @@ Handles WebSocket connection establishment.
4130
- `path(String path)` - Sets the WebSocket path
4231
- `tls(boolean tls)` - Enables/disables TLS encryption
4332

44-
#### URL Parsing
33+
##### URL Parsing
4534

4635
The `connect(String url)` method automatically parses WebSocket URLs and extracts:
4736
- Protocol (ws:// or wss://)
@@ -55,39 +44,27 @@ The `connect(String url)` method automatically parses WebSocket URLs and extract
5544
- `wss://example.com:8443/chat?room=general`
5645
- `wss://api.example.com/ws`
5746

58-
### 3. DslDisconnectSampler (Disconnection Operations)
59-
60-
Handles WebSocket connection closure.
61-
62-
#### Configuration Methods
47+
##### Disconnection configuration
6348

6449
- `responseTimeout(String timeout)` - Sets response timeout in milliseconds
6550
- `statusCode(String statusCode)` - Sets the close status code (e.g., "1000" for normal closure)
6651

67-
### 4. DslWriteSampler (Write Operations)
68-
69-
Handles sending data through WebSocket connections.
70-
71-
#### Configuration Methods
52+
##### Write configuration
7253

7354
- `connectionTimeout(String timeout)` - Sets connection timeout in milliseconds
7455
- `requestData(String requestData)` - Sets the data to send
7556
- `createNewConnection(boolean createNewConnection)` - Whether to create a new connection
7657
- `loadDataFromFile(boolean loadDataFromFile)` - Whether to load data from a file
7758

78-
### 5. DslReadSampler (Read Operations)
79-
80-
Handles receiving data from WebSocket connections.
81-
82-
#### Configuration Methods
59+
##### Read configuration
8360

8461
- `connectionTimeout(String timeout)` - Sets connection timeout in milliseconds
8562
- `responseTimeout(String timeout)` - Sets response timeout in milliseconds
8663
- `createNewConnection(boolean createNewConnection)` - Whether to create a new connection
8764

88-
## Usage Examples
65+
#### Usage Examples
8966

90-
### Basic WebSocket Test
67+
##### Basic WebSocket Test
9168

9269
```java
9370
import static us.abstracta.jmeter.javadsl.JmeterDsl.*;
@@ -99,19 +76,19 @@ public class Test {
9976
TestPlanStats stats = testPlan(
10077
threadGroup(1, 1,
10178
// Connect to WebSocket server
102-
DslWebsocketSampler
79+
webSocketSampler()
10380
.connect("wss://ws.postman-echo.com/raw")
10481
.connectionTimeout("10000")
10582
.responseTimeout("5000"),
10683

10784
// Send a message
108-
DslWebsocketSampler
85+
webSocketSampler()
10986
.write()
11087
.requestData("Hello WebSocket!")
11188
.createNewConnection(false),
11289

11390
// Read the response
114-
DslWebsocketSampler
91+
webSocketSampler()
11592
.read()
11693
.responseTimeout("5000")
11794
.createNewConnection(false)
@@ -121,7 +98,7 @@ public class Test {
12198
),
12299

123100
// Close the connection
124-
DslWebsocketSampler
101+
webSocketSampler()
125102
.disconnect()
126103
.responseTimeout("1000")
127104
.statusCode("1000")
@@ -131,10 +108,10 @@ public class Test {
131108
}
132109
```
133110

134-
### Manual Connection Configuration
111+
##### Manual Connection Configuration
135112

136113
```java
137-
DslWebsocketSampler
114+
webSocketSampler()
138115
.connect()
139116
.server("localhost")
140117
.port("8080")
@@ -144,10 +121,10 @@ DslWebsocketSampler
144121
.responseTimeout("3000")
145122
```
146123

147-
### Connection with Assertions
124+
##### Connection with Assertions
148125

149126
```java
150-
DslWebsocketSampler
127+
webSocketSampler()
151128
.read()
152129
.responseTimeout("5000")
153130
.createNewConnection(false)
@@ -157,51 +134,51 @@ DslWebsocketSampler
157134
)
158135
```
159136

160-
## Error Handling
137+
#### Error Handling
161138

162-
### Invalid URL Handling
139+
##### Invalid URL Handling
163140

164141
```java
165142
// This will throw IllegalArgumentException
166-
DslWebsocketSampler.connect("http://localhost:80/test");
143+
webSocketSampler().connect("http://localhost:80/test");
167144
```
168145

169146
The URL parser validates:
170147
- Protocol must be `ws://` or `wss://`
171148
- Hostname is required
172149
- Valid URI syntax
173150

174-
### Connection Timeouts
151+
##### Connection Timeouts
175152

176153
Configure appropriate timeouts to handle network issues:
177154

178155
```java
179-
DslWebsocketSampler
156+
webSocketSampler()
180157
.connect("wss://example.com/ws")
181158
.connectionTimeout("10000") // 10 seconds
182159
.responseTimeout("5000") // 5 seconds
183160
```
184161

185-
## Best Practices
162+
#### Best Practices
186163

187164
1. **Connection Reuse**: Set `createNewConnection(false)` for write/read operations to reuse existing connections
188165
2. **Timeout Configuration**: Always set appropriate timeouts to avoid hanging tests
189166
3. **Error Handling**: Use response assertions to validate WebSocket responses
190167
4. **URL Parsing**: Use the `connect(String url)` method for cleaner code when you have complete URLs
191168
5. **Status Codes**: Use standard WebSocket close codes (1000 for normal closure)
192169

193-
## Integration with Test Plans
170+
#### Integration with Test Plans
194171

195172
WebSocket samplers integrate seamlessly with other JMeter DSL components:
196173

197174
```java
198175
testPlan(
199176
threadGroup(10, 100,
200177
// WebSocket operations
201-
DslWebsocketSampler.connect("wss://api.example.com/ws"),
202-
DslWebsocketSampler.write().requestData("test data"),
203-
DslWebsocketSampler.read(),
204-
DslWebsocketSampler.disconnect(),
178+
webSocketSampler().connect("wss://api.example.com/ws"),
179+
webSocketSampler().write().requestData("test data"),
180+
webSocketSampler().read(),
181+
webSocketSampler().disconnect(),
205182
),
206183
// Results collection
207184
jtlWriter("results.jtl"),

jmeter-java-dsl-cli/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@
6565
<artifactId>jmeter-java-dsl-recorder</artifactId>
6666
<version>${project.version}</version>
6767
</dependency>
68+
<dependency>
69+
<groupId>us.abstracta.jmeter</groupId>
70+
<artifactId>jmeter-java-dsl-websocket</artifactId>
71+
<version>${project.version}</version>
72+
</dependency>
6873
<dependency>
6974
<groupId>org.slf4j</groupId>
7075
<artifactId>jul-to-slf4j</artifactId>
@@ -201,6 +206,7 @@
201206
<include>org.apache-extras.beanshell:bsh</include>
202207
<!-- required for converting jsonpath -->
203208
<include>com.jayway.jsonpath:json-path</include>
209+
<!-- required for kotlin support in JMeter 5.6.3+ -->
204210
<include>org.jetbrains.kotlin:kotlin-stdlib</include>
205211
<include>org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm</include>
206212
<include>org.jetbrains.kotlinx:kotlinx-coroutines-swing</include>

jmeter-java-dsl-cli/src/main/java/us/abstracta/jmeter/javadsl/cli/Jmx2DslCommand.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import us.abstracta.jmeter.javadsl.graphql.DslGraphqlSampler;
1212
import us.abstracta.jmeter.javadsl.jdbc.JdbcJmeterDsl;
1313
import us.abstracta.jmeter.javadsl.parallel.ParallelController;
14+
import us.abstracta.jmeter.javadsl.websocket.DslWebsocketSampler;
1415
import us.abstracta.jmeter.javadsl.wrapper.WrapperJmeterDsl;
1516

1617
@Command(name = "jmx2dsl", header = "Converts a JMX file to DSL code",
@@ -48,6 +49,7 @@ public Integer call() throws Exception {
4849
addBuildersFrom(ElasticsearchBackendListener.class, "jmeter-java-dsl-elasticsearch-listener",
4950
codeGenerator);
5051
addBuildersFrom(DatadogBackendListener.class, "jmeter-java-dsl-datadog", codeGenerator);
52+
addBuildersFrom(DslWebsocketSampler.class, "jmeter-java-dsl-websocket", codeGenerator);
5153
System.out.println(codeGenerator.generateCodeFromJmx(jmxFile));
5254
return 0;
5355
}

jmeter-java-dsl-websocket/pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
<relativePath>../pom.xml</relativePath>
1111
</parent>
1212

13-
<groupId>us.abstracta.jmeter.javadsl.websocket</groupId>
1413
<artifactId>jmeter-java-dsl-websocket</artifactId>
1514

1615
<dependencies>

0 commit comments

Comments
 (0)