All Products
Search
Document Center

Performance Testing:Perform performance testing on WebSocket

Last Updated:Dec 17, 2024

The Apache JMeter-based performance testing feature of Performance Testing (PTS) depends on scripts generated by Apache JMeter. This topic describes how to use Apache JMeter on an on-premises machine to write and debug a performance testing script and how to perform performance testing on WebSocket in PTS. In this topic, the JMeter WebSocket Samplers plug-in is used.

Overview

To use Apache JMeter to generate a script for performance testing on WebSocket and perform performance testing in PTS, perform the following steps:

1. Install the JMeter WebSocket Samplers plug-in

  1. Download the JMeter WebSocket Samplers plug-in. We recommend that you download the latest version of the plug-in. In this topic, JMeterWebSocketSamplers-1.2.10.jar is used.

  2. Save the downloaded JAR package of the plug-in in the <Apache JMeter installation directory>/lib/ext directory.

    Important

    If you want to perform performance testing in PTS, you must upload the JAR package to PTS.

  3. Restart Apache JMeter and perform the following steps to check whether the plug-in is installed:

    1. Right-click Test Plan and choose Add > Config Element. Check whether the following elements are displayed: WebSocket Binary Frame Filter, WebSocket Ping/Pong Frame Filter, and WebSocket Text Frame Filter.image

    2. Right-click Test Plan and choose Add > Assertions. Check whether the Binary Response Assertion assertion is displayed.image

    3. Right-click Test Plan and choose Add > listener > View Results Tree. In the Text drop-down list, check whether Binary is displayed.

      image

    4. Right-click Test Plan and choose Add > Threads(Users) > Thread Group. Right-click Thread Group and choose Add > Sampler. Check whether samplers displayed in the following figure are available.

      image

      Important
      • WebSocket is a protocol that provides streaming communication channels over a long-lived connection. Therefore, multiple responses may be returned for a request. Responses are placed in a response queue in chronological order. When a sampler that contains the read operation is executed, a response is obtained from the start of the queue. When a response is received, the response is placed at the end of the queue.

      • WebSocket Single Read Sampler only reads a response from the start of the queue and waits for a response until a timeout occurs.

      • WebSocket Single Write Sampler only sends a request to the connection channel and does not wait for a response.

      • WebSocket Request Response Sampler sends a request to the connection channel and reads a response from the start of the response queue. This sampler waits for a response until a timeout occurs.

      • If the number of read samplers does not match the number of write samplers in a connection, the read data may be misaligned.

    For more information about samplers that are used for proxy services, data filtering, and data sharding, see JMeter WebSocket Samplers.

2. Create a script

This section describes how to add and configure samplers in a test scenario. This section is only for reference. You can add and configure samplers based on your business requirements.

  1. Right-click Test Plan and choose Add > Thread (Users) > Thread Group to add a thread group to the test plan.

  2. In the Thread Group pane, add and configure samplers in sequence. The following table describes the samplers.

    Sampler

    Description

    WebSocket Open Connection

    Initiates a WebSocket connection.

    WebSocket Ping/Pong

    Sends ping frames and receives pong frames.

    WebSocket request-response Sampler

    Sends and receives WebSocket frames of the text and binary types.

    WebSocket Single Write Sampler

    Sends WebSocket frames of the text and binary types.

    WebSocket Single Read Sampler

    Receives WebSocket frames of the text and binary types.

    WebSocket Close

    Closes the WebSocket connection.

    1. Right-click Thread Group and choose Add > Sampler > WebSocket Open Connection.

      image

      Note

      In the Server URL section, configure the service that you want to test. In this example, a simple WebSocket service that is built by using an SDK for Python is used. The WebSocket service is an Echo service, and all messages sent to this service are echoed to the client.

      server.py sample code

      import 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()
    2. Right-click Thread Group and choose Add > Sampler > WebSocket Ping/Pong.

      image

      Note

      Set the Pong (read) timeout (ms) parameter to 6000.

      This configuration indicates that if Apache JMeter does not receive a pong message within 6,000 milliseconds, the sampler fails. In this case, the sampler reuses the existing connection.

    3. Right-click Thread Group and choose Add > Sampler > WebSocket request-response Sampler.image

      Parameter

      Example value

      Description

      Connection

      use existing connection

      Specifies whether to establish a new connection or reuse an existing connection.

      Data type

      Text

      The type of the data.

      Request data

      PTS

      The request data. In this example, the text data is used.

      Response (read) timeout (ms)

      6000

      The timeout period for a response.

    4. Right-click Thread Group and choose Add > Sampler > WebSocket Single Write Sampler. WebSocket Single Write Sampler is a non-blocking sampler. The sampler is used only to send data to the server and does not wait for a response.image

      Parameter

      Example value

      Description

      Connection

      use existing connection

      Specifies whether to establish a new connection or reuse an existing connection.

      Data type

      Text

      The type of the data.

      Request data

      PTS bang bang bang

      The request data. In this example, the text data is used.

    5. Right-click Thread Group and choose Add > Sampler > WebSocket Single Read Sampler. WebSocket Single Read Sampler is used only to receive responses.image

      Parameter

      Example value

      Description

      Connection

      use existing connection

      Specifies whether to establish a new connection or reuse an existing connection.

      Data type

      Text

      The type of the data.

      Response (read) timeout (ms)

      6000

      The timeout period for a response.

    6. Right-click Thread Group and choose Add > Sampler > WebSocket Close. WebSocket Close is used to close the existing WebSocket connection based on negotiation. The response message is 1000:sampler requested close.image

      Parameter

      Example value

      Description

      Close status

      1000

      The status code, which is the first two bytes of the data frame that is sent when the connection is closed.

      Response (read) timeout (ms)

      6000

      The timeout period for a response.

  3. Optional. If you want to add headers, such as tokens, during the communication, click Thread Group and choose Add > ConfigElement > HttpHeaderManager. Add the HttpHeaderManager element to the thread group and then add the headers. For more information, see the Apache JMeter documentation.

  4. Right-click Thread Group and choose Add > Listener > View Results Tree to add the View Results Tree listener to the thread group.

3. Debug the script

After you write the script, click the Start button on the top of the page to run the script.

Check whether the script runs as expected by using the View Results Tree listener. On the View Results Tree page, if a green check mark is displayed before a sampler, the sampler runs as expected. If the running of a sampler fails, debug the script based on Create a script.

The following figures show the script configurations of different samplers.

  1. WebSocket request-response Sampler

    The following figure shows the request information in the script.image

    The following figure shows the response data in the script when Text is selected as the data type.image

  2. WebSocket Single Read Sampler

    The following figure shows the response data in the script when Text is selected as the data type.image

4. Export the script

After the script is debugged, choose File > Save Test Plan as in the top navigation bar to save the script file to the on-premises machine. In this example, name the file wsTest.jmx.

5. Create an Apache JMeter-based performance testing scenario and start the performance testing

  1. Log on to the PTS console, choose Performance Test > Create Scenario, and then click JMeter.

  2. Configure the scenario.

    1. Specify the scenario name and upload the wsTest.jmx file that you exported.

    2. After you upload the script file, PTS automatically supplements the plug-in. If the supplementation fails, click Upload File to add the downloaded JAR package (JMeterWebSocketSamplers-1.2.10.jar) and other data files, such as parameter files in the CSV format.image

  3. Configure stress settings.

    Set the Max VUs parameter to 10 and the Test Duration and Increment Duration parameters to 2 minutes. Retain the default values for other parameters. You are charged for performance testing. We recommend that you configure the load level based on your business requirements. For more information, see Billing overview and Configure the stress testing model and level.image

  4. Debug and start performance testing.

    1. Recommended. Debug the scenario to verify the configurations. This helps prevent performance testing failures. For more information, see Debug a stress testing scenario.

    2. Click Save and Test. On the Tips page, select Trigger Now and Make sure that the test is approved and complies with local laws, and then click Start Stress Testing.

Note

For more information about Apache JMeter-based performance testing in PTS, see Create a JMeter scenario.

Analyze the performance testing result

After the performance testing is finished, the system automatically obtains the performance testing data and generates a performance testing report. The performance testing data includes the performance testing scenario metrics, business details, monitoring details, and API sampling logs. For more information, see View a JMeter performance testing report.