The JMeter-based stress testing feature of Performance Testing Service (PTS) uses Apache JMeter scripts. This topic explains how to write and debug a WebSocket stress testing script locally with the JMeter WebSocket Samplers plugin, and then run it in a PTS stress testing scenario.
Overview
To generate a WebSocket stress testing script using Apache JMeter and run it in PTS, follow these five steps:
1. Install the JMeter WebSocket Samplers plugin
-
Download JMeter WebSocket Samplers. Download the latest version. This example uses
JMeterWebSocketSamplers-1.2.10.jar. -
Place the downloaded plugin JAR file in the
<JMeter installation directory>/lib/extdirectory.ImportantYou must also upload this plugin JAR file to PTS when running a stress test.
-
Restart Apache JMeter and follow these steps to verify that the plugin is installed.
-
Right-click Test Plan and navigate to Add > Config Element. You can see the new Config Elements: WebSocket Binary Frame Filter, WebSocket Ping/Pong Frame Filter, and WebSocket Text Frame Filter.
-
Right-click Test Plan and navigate to Add > Assertions. You can see the new assertion: Binary Response Assertion.
-
Right-click Test Plan and navigate to Add > Listener > View Results Tree. In the drop-down list at the bottom left of the editor page, you can see the new Binary option.
-
Right-click Test Plan and navigate to Add > Threads(Users) > Thread Group. Then, right-click thread group and navigate to Add > Sampler. You can see the following new sampler options.
The new sampler options include WebSocket Open Connection, WebSocket Ping/Pong, WebSocket Single Read Sampler, WebSocket Single Write Sampler, and WebSocket request-response Sampler.
Important-
WebSocket uses a long-lived connection for streaming, so a single request can receive multiple responses. These responses are added to a response queue. A read sampler retrieves a response from the head of the queue, while new responses are added to the tail.
-
The WebSocket Single Read Sampler is a read-only sampler that reads one response from the head of the queue. If the queue is empty, it waits until the timeout is reached.
-
The WebSocket Single Write Sampler is a write-only sampler that sends a request without waiting for a response.
-
The WebSocket request-response Sampler is a read-write sampler. It sends a request and then reads one response from the queue. If the queue is empty, it waits until the timeout is reached.
-
If you use an imbalanced number of read and write samplers, the data you read might be misaligned.
-
For information about features such as proxies, filters, and fragmentation that are not covered in this topic, see the JMeter WebSocket Samplers plugin documentation.
-
2. Create a script
This section shows how to add and configure samplers for a test scenario. The following steps provide a simple example that you can adapt to your needs.
-
Right-click Test Plan and navigate to Add > Thread (Users) > Thread Group to add a thread group to the Test Plan.
-
In the thread group, add and configure the following samplers in order.
Sampler name
Description
WebSocket Open Connection
Establishes a WebSocket connection.
WebSocket Ping/Pong
Sends a Ping control frame and expects a Pong response.
WebSocket request-response Sampler
Sends and receives text or binary WebSocket frames.
WebSocket Single Write Sampler
Sends text or binary WebSocket frames.
WebSocket Single Read Sampler
Receives text or binary WebSocket frames.
WebSocket Close
Closes the WebSocket connection.
-
Right-click thread group and navigate to Add > Sampler > WebSocket Open Connection.
On the WebSocket Open Connection configuration panel, under Server URL, set the protocol to
ws, Server name or IP toecho.example.com, and Port to8080. Set Connection timeout (ms) to20000and Read timeout (ms) to6000.NoteReplace the Server URL with your service's details. This example uses a simple Python-based Echo service for demonstration, which echoes all received messages back to the client.
server.pysample codeimport asyncio import websockets async def echo(websocket, path): async for message in websocket: message = "Your message: {}".format(message) await websocket.send(message) asyncio.get_event_loop().run_until_complete(websockets.serve(echo, '0.0.0.0', 8080)) asyncio.get_event_loop().run_forever() -
Right-click thread group and navigate to Add > Sampler > WebSocket Ping/Pong.
In the Behaviour section, select the ping/pong (send ping, expect pong) mode.
NotePong(read)timeout(ms): 6000
If JMeter does not receive a Pong message within the 6,000 ms timeout, the sampler fails. This sampler reuses the existing connection.
-
Right-click thread group and navigate to Add > Sampler > WebSocket request-response Sampler.
The configuration panel includes sections such as Connection and Data (request data type and content). The following table describes the key parameters.
Parameter
Value
Description
Connection
use existing connection
Specifies whether to create a new connection or reuse an existing one.
Data type
Text
Specifies the data type, request data, and response timeout.
Request data
PTS
The request data. This example uses text.
Response (read) timeout (ms)
6000
The response timeout.
-
Right-click thread group and navigate to Add > Sampler > WebSocket Single Write Sampler. This non-blocking sampler sends data to the server without waiting for a response.
Parameter
Value
Description
Connection
use existing connection
Specifies whether to create a new connection or reuse an existing one.
Data type
Text
Specifies the data type.
Request data
PTS bang bang bang
The request data. This example uses text.
-
Right-click thread group and navigate to Add > Sampler > WebSocket Single Read Sampler. This sampler receives message responses from the server.
Parameter
Value
Description
Connection
use existing connection
Specifies whether to create a new connection or reuse an existing one.
Data type
Text
Specifies the data type.
Response (read) timeout (ms)
6000
The response timeout.
-
Right-click Thread Group and select Add > Sampler > WebSocket Close. This sampler closes the existing WebSocket connection. Because this is a negotiated close, the response message is:
1000: sampler requested close.Parameter
Value
Description
Close status
1000
The status code defined when closing the connection. It consists of the first two bytes of the data frame.
Response (read) timeout (ms)
6000
The response timeout.
-
-
(Optional) To add headers, such as a token, right-click thread group and navigate to Add > Config Element > HttpHeaderManager. This adds a Config Element where you can configure the required headers. For more details, see the official JMeter documentation.
-
Right-click thread group and navigate to Add > Listener > View Results Tree to add a View Results Tree listener to the thread group.
3. Debug the script
After you finish editing the script, click the start button at the top of the page to run it.
Use the View Results Tree listener to check if the script runs correctly. In this listener, a green check mark next to a sampler indicates that it ran successfully. If a sampler fails, debug it by referring to 2. Create a script.
The following are some example results:
-
WebSocket request-response Sampler
Based on the script configuration, the Request information is as follows:
Connect URL: ws://echo.example.com:8080/ (using existing connection) Request data: PTSIn the View Results Tree listener, check the WebSocket request-response Sampler. The Response Body on the Response data tab should show
Your message: PTS. This confirms a successful request-response exchange. -
WebSocket Single Read Sampler
In View Results Tree, select the WebSocket Single Read Sampler. On the Response data tab, the response body should be
Your message: PTS bang bang bang. This confirms that the WebSocket message was sent and received correctly.
4. Export the script
After you successfully debug the local script, select File - Save Test Plan as from the top menu bar to save the script file to your local machine as wsTest.jmx.
5. Create and run a JMeter stress test
-
Log on to the PTS console, choose Performance Test > Create Scenario, and then click JMeter.
-
Configure the scenario.
-
Enter the scene name, and upload the exported
wsTest.jmxfile. -
After the script is successfully uploaded, PTS automatically installs the required plugins. If the installation fails, you can click Upload File to add the downloaded JAR package for JMeter WebSocket Samplers, such as
JMeterWebSocketSamplers-1.2.10.jar, and other data files, such asCSVparameter files.
-
-
Configure stress settings.
Set the maximum number of virtual users to
10and the stress test duration to2minutes. You can keep the default values for the other settings. Initiating a stress test incurs charges, so we recommend that you select an appropriate stress level. For more information, see Billing overview and Stress modes and level configuration. In the stress configuration section, set Stress Source to Public Network, Stress Mode to Virtual User Mode, and Traffic Model to Manual Speed Control. Set Maximum Virtual Users to 10, Starting Percentage to 100%, and Stress Test Duration to 2 minutes. For Specify Number of Loops, select No. Keep Number of Specified IPs set to 1, and ensure that Customize Traffic Region and IPv6 Traffic are disabled. -
Debug and start the test.
-
We recommend debugging the scenario first to verify your configuration and prevent test failures.
-
Click Save and Start. In the Note dialog box that appears, select Execute Now, check the The test is permitted and complies with the applicable laws and regulations. box, and then click Start Stress Testing.
-
For more information about running JMeter-based stress tests in PTS, see Create a JMeter scenario.
Analyze the test results
After the test is complete, PTS automatically generates a stress testing report. The report includes stress testing scenario metrics, business details, monitoring data, and API sampling logs. For more information, see View a JMeter stress testing report.