PelicanDT can simulate an environment with server network latency to test the performance of your application. This topic describes how to create this simulation.
Prerequisites
You have installed the PelicanDT SDK. For more information, see Install the PelicanDT SDK.
You have an application running on a remote Linux server.
Procedure
This example simulates a test environment with a 1000 ms network latency that lasts for 10 s. The procedure is as follows:
Add the following code to your project and configure the parameters:
In the code, enter the IP address, username, and logon password for your remote server.
Set the delay and duration. In this example, set the delay to 1000 ms and the duration to 10 s.
import com.alibaba.pelican.chaos.client.RemoteCmdClientConfig; import com.alibaba.pelican.chaos.client.impl.RemoteCmdClient; import com.alibaba.pelican.chaos.client.utils.MemUtils; import com.alibaba.pelican.chaos.client.utils.NetTrafficUtils; import com.alibaba.pelican.deployment.element.impl.AbstractElement; import java.util.Map; /** * @author moyun@middleware */ public class DemoNetTraffic { public static void main(String[] args) { // The IP address of the remote server. String ip = ""; // The username for the remote server. String userName = ""; // The logon password for the remote server. String password = ""; RemoteCmdClientConfig config = new RemoteCmdClientConfig(); config.setIp(ip); config.setUserName(userName); config.setPassword(password); RemoteCmdClient client = new RemoteCmdClient(config); // The delay time in ms. int delayTime = 1000; // The duration in seconds. int delaySecond = 10; // Induce network latency. NetTrafficUtils.setNetworkDelay(client, delayTime, delaySecond); } }Run your project.
Verify the results
Let Server A be the remote server and Server B be the server on which you will induce network latency. Before running the sample program, log on to Server B. Run the ping command from Server B to Server A and record the network latency. After running the sample program, check whether the network latency increases by 1000 ms and returns to normal after 10 s.