This document introduces how to write a WebSocket-based performance testing script.
Note:
For detailed API documentation, see pts/ws. Overview
WebSocket is an application-layer communication protocol that provides full-duplex communication over a single TCP connection.
Unlike the HTTP protocol's question-and-answer mode where the client initiates a request and the server responds, once a WebSocket connection is established, data can be continuously and bidirectionally exchanged between the client and server until the connection is closed. Therefore, in performance testing scenarios, scripts based on WebSocket requests differ in structure and action mechanism from scripts based on HTTP requests:
Each VU that executes the HTTP script will continuously iterate the main function (export default function() { ... }
) until the performance testing ends.
Each VU that executes the WebSocket script does not continuously iterate the main function, because the main function is blocked by the ws.connect
method that establishes the connection, until the connection is closed. However, in the callback function (function (socket) {...}
) after the connection is established, the corresponding method will continuously listen on and process asynchronous events until the performance testing ends.
Script Writing
The ws module of the PTS API provides APIs related to the WebSocket protocol. For details, see pts/ws. Basic usage:
Use the ws.connect
method to establish a connection and define your business logic in its callback function:
The required parameters for ws.connect
include the URL and callback function.
If the connection is successfully established, PTS will pass the created ws.Socket
object to the callback function. You can define your WebSocket request logic in the callback function.
After executing the callback function, the ws.connect
method will return a ws.Response
object.
Common methods of the ws.Socket
object:
send
: sends a text message.
close
: closes a connection.
on
: listens on events and processes them with the callback function. PTS supports the following events.
|
open | Establishes a connection. |
close | Closes a connection. |
error | Reports an error. |
message | Receives a text message. |
binaryMessage | Receives a binary message. |
pong | Receives a pong message. |
ping | Receives a ping message. |
The code example is as follows:
import ws from 'pts/ws';
import { check, sleep } from 'pts';
export default function () {
const res = ws.connect("ws://localhost:8080/echo", function (socket) {
socket.on('open', () => console.log('connected'));
socket.on('message', (data) => console.log('Message received: ', data));
socket.on('close', () => console.log('disconnected'));
socket.send("message");
socket.setTimeout(function () {
console.log('3 seconds passed, closing the socket');
socket.close();
}, 3000);
socket.setInterval(function () {
socket.ping();
}, 500);
socket.setLoop(function () {
sleep(0.1)
socket.send("loop message")
});
});
check("status is 101", () => res.status === 101);
}
File Dependency
In the performance testing scenario, you can upload the following types of files to provide status data during the execution of the performance testing task:
Parameter file: It dynamically provides test data in CSV format. That is, when the scenario is executed by each concurrent user (VU), each line of data will be obtained from the parameter file as the test data values for reference by variables in the script. For specific usage, see Using Parameter Files. Request file: It is required for constructing your request, for example, the file that needs to be uploaded. For specific usage, see Using Request Files. Protocol file: It is required for request serialization. For specific usage, see Using Protocol Files.