Network Speed Testing
Overview
ZEGO provides network speed testing functionality to detect uplink and downlink network speeds before users publish/play streams, and determine what bitrate of audio and video streams is suitable for publishing/playing under the current network environment.
When the uplink speed test results show a high packet loss rate, it is recommended to use methods such as lowering the resolution or lowering the frame rate to reduce the publishing stream bitrate to ensure normal publishing; when the downlink speed test results show a high packet loss rate, it is recommended to use the Video Advanced - Set Video Encoding Method function provided by the SDK to play low-bitrate streams to ensure normal playing.
When the following situations occur in the developer's business, ZEGO recommends using the SDK's network speed testing function:
-
In call scenarios, network quality assessment needs to be performed.
-
In education scenarios, pre-class network detection needs to be performed.
-
In live streaming scenarios, network connection speed testing needs to be performed.
The basic principle of network speed testing is shown in the following figure:

Prerequisites
Before implementing the network speed testing functionality, ensure:
- You have created a project in the ZEGOCLOUD Console and applied for a valid AppID and AppSign. For details, please refer to Console - Project Information.
- You have integrated ZEGO Express SDK into the project and implemented basic audio and video streaming functionality. For details, please refer to Quick Start - Integration and Quick Start - Implementation.
Usage Steps
1 Listen to Speed Test Callbacks
Before starting the speed test, you can set up speed test related callbacks first.
-
Under normal speed test conditions, when network speed quality updates, the networkSpeedTestQualityUpdate callback will be triggered.
-
When an error occurs during the speed test, the networkSpeedTestError callback will be triggered.
// Network speed test quality callback
ZegoExpressEngine.instance().on('networkSpeedTestQualityUpdate', (quality, type) {}
// Network speed test error callback
ZegoExpressEngine.instance().on('networkSpeedTestError', (errorCode, type) {}2 Start Speed Test
Create an instance of ZegoNetworkSpeedTestConfig network speed test configuration, set whether to perform uplink and downlink speed tests and expected bitrate according to the actual situation, and call the startNetworkSpeedTest interface to start network speed testing.
After calling the startNetworkSpeedTest interface to start speed testing, it supports speed testing for 30 seconds by default (if you need to adjust the duration, please contact ZEGOCLOUD Technical Support to modify the dynamic configuration). After the timeout, the speed test will be forcibly ended. If you need to continue speed testing, please call this interface again.
let config =new ZegoNetworkSpeedTestConfig();
// Perform uplink speed test, specify expected publishing stream bitrate
config.testUplink = true;
config.expectedUplinkBitrate = ZegoExpressEngine.instance().getVideoConfig().bitrate;
// Perform downlink speed test, specify expected playing stream bitrate
config.testDownlink = true;
config.expectedDownlinkBitrate = ZegoExpressEngine.instance().getVideoConfig().bitrate;
// Start speed test, default callback interval is 3 seconds
ZegoExpressEngine.instance().startNetworkSpeedTest(config);
// If you need to set the callback interval, you can refer to the following call (taking 1.5 seconds as an example)
ZegoExpressEngine.instance().startNetworkSpeedTest(config, 1500);The speed test results will be returned in the network speed test quality ZegoNetworkSpeedTestQuality in networkSpeedTestQualityUpdate. By analyzing the parameter values, you can know whether the current network quality is good.
The parameters in ZegoNetworkSpeedTestQuality are as follows:
| Parameter | Description | Explanation |
|---|---|---|
| connectCost | Time to connect to server, in milliseconds. | If the network connection is disconnected during the speed test, it will automatically reconnect. This variable will be updated accordingly. The smaller the value, the better. |
| rtt | Network latency, in milliseconds. | Represents the time consumed between the SDK and the server round trip. The smaller the value, the better. |
| packetLostRate | Packet loss rate, in percentage. | The value range is 0.0 - 1.0. For example, 0.5 means that out of every 10 packets sent to the server, 5 may be lost on the way. |
| quality | Network quality | Represents the current network quality level. For details, please refer to ZegoStreamQualityLevel:
|
3 Stop Speed Test
Call the stopNetworkSpeedTest interface to stop network speed testing.
After stopping the speed test, you will no longer receive networkSpeedTestQualityUpdate or networkSpeedTestError callbacks.
ZegoExpressEngine.instance().stopNetworkSpeedTest();