简体   繁体   中英

TestSettings Network Emulation for Unit Test

I would like to leverage Network Emulation to simulate various network speeds and packet loss rates from a Unit Test. I am trying to benchmark my network layer in combination with different packet loss rates. I have followed these instructions here, but am still having trouble getting it to work.

Using Network Emulation

Setting testsettings

I have tried testing through loopback connection 127.0.0.1 as well as downloading a remote file from a unit test. In both cases network speed is not reduced.

This is using Visual Studio 2013 Premium

How can I get Network Emulation settigns to work with a unit test?

To get this to work:

  • If you plan to test a connection between client/server on same machine by connecting through a loopback, you must first install the Microsoft Loopback Adapter . If you forget/change mind later, see later Loopback section.
  • Run Visual Studio 2013 Premium as administrator (I believe use of testsettings requires Visual Studio Ultimate, Visual Studio Premium, or Visual Studio Test Professional)
  • Create a testsettings file (Solution->Add Item)
    • Double click the file to open Test Settings Dialog
    • Select Data and Diagnostics on left
    • Selected Network Emulation, click Configure, set 56kpb modem (we'll use slow network emulation to help verify the emulation is working, you can change this later)
    • Click Apply from Test Settings dialog. Will be prompted to install network bindings. (this will fail if you didn't run Visual Studio) It will automatically install to all network bindings.
  • Select the file from main menu TEST->Test Settings -> Select testsetting file
  • Make sure Keep Test Execution Engine Running is unchecked under TEST->Test Settings. I think having this checked causes the engine to not pick up on all the changes we've made since it never shutdowns/starts to reload changes.

An image file that took less than a second to download previously, now took almost a minute with the 56kbps emulation:

[TestMethod]
public void TestMethod1()
{
    string page = "http://upload.wikimedia.org/wikipedia/commons/5/53/Wikipedia-logo-en-big.png";

    // ... Use HttpClient.
    using (HttpClient client = new HttpClient())
    {
       byte[] result  = client.GetByteArrayAsync(page).Result;
       Console.WriteLine("length" + result.Length);              
    }
}

Modifying Network Emulation Settings

The testsettings GUI provides only a few network profiles. If you open the testsettings file as a XML file in something like Notepad++, you can tweak the network settings and adjust packet loss/latency to test various scenarios.

Loopback

You can't use 127.0.0.1 because it bypasses much of the networking layer for performance, including bypassing the emulator. Installing the Microsoft Loopback Adapter adds an interface with its own IP address to which the emulator driver can bind.

If you installed the Microsoft Loopback Adapter after going through the above procedure to bind the Network Emulation driver, then it only bound to the existing interfaces. To bind to newly installed interface , you must uninstall and reinstall the network emulation driver . From Start->Programs->Visual Studio Tools->Developer Command Prompt..., run the two commands:

VSTestConfig NETWORKEMULATION /uninstall
VSTestConfig NETWORKEMULATION /install

The second command should indicate the number of interfaces it binds to.

To determine the IP address for your loopback run ipconfig/all from a command line, and look for the entry with Description "Microsoft Loopback Adapter". This IP address will be the one you need to code your client/server tests to use.

Ethernet adapter Local Area Connection 2:    
...
   Description . . . . . . . . . . . : Microsoft Loopback Adapter
...
   Link-local IPv6 Address . . . . . : fe80::e0d2:c0f8:b3f8:2199%20(Preferred)
   Autoconfiguration IPv4 Address. . : 169.254.33.153(Preferred)

Impact on Internet

Because the Network Emulation driver binds to all available interfaces, then while you are running a unit test, and the emulation kicks in, your internet connection will also be slowed down. You can uncheck/disable the Emulation Driver from other interfaces from the Network Connection Properties dialog. It will be listed as Microsoft VSTS Network Emulation NDIS6 Driver under the This connection uses the following items: (don't do this while a unit test is running, I got a blue screen and it corrupted the driver and had to manually reinstall again)

网络连接属性对话框

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM