开发者

How do I properly simulate high latency on a C# socket server test?

开发者 https://www.devze.com 2023-01-06 02:40 出处:网络
I have a class that uses \'System.Net.Sockets.Socket\' directly for network comunication, and currently I use the following two interfaces to break dependency on the Socket class :

I have a class that uses 'System.Net.Sockets.Socket' directly for network comunication, and currently I use the following two interfaces to break dependency on the Socket class :

public interface ISocketListener
    {
        void Listen(int backlog);

        ISocket Accept();

        void Bind(EndPoint endpoint);               
    }

public interface ISocket
    {
        void Connect (EndPoint开发者_运维问答 ep);

        Stream CommunicationStream { get; }

        void Close();

        void Close(int timeout);

        void Shutdown();
    }

In the production implementation I just redirect all the method calls to the private Socket object. In testing environment I use a MemoryStream as the comunication 'pipe' between the sockets. Since I have little experience in the subject, some questions appeared in my head while writing tests : Are there any 'formal' good practices in testing this kind of software? When doing integration testing, how do I test the performance of this server in multiple connections/high latency situations (more specifically, how to simulate these situations)? Why there are asynchronous versions of Socket.Accept/Socket.Receive? Can I replace the asynchronous methods for handling their synchronous versions in separate threads?


You could simulate high latency by creating a proxy server to sit inbetween your connection, then you can just add a delay when re-sending the data.


I'm not familiar with the socket object. However, I have done some research about testing. It looks like you're on the right track. Writing interfaces that can refer to either the actual objects that will be used in production or test objects is generally considered good practice. This pattern is known as "Dependency Injection".

I don't know how the socket works, but what you can do for testing purposes is create a new test object to use in place of the socket you want to test. This test object can simply call Thread.Sleep(x) to add some simulated latency before returning data.

Edit: I would also like to point out that you're going to have to be very careful to ascertain exactly where the network latency occurs in your code so that when you inject the artificial latency, you add it to the same place in the code where it would occur in the real world.


Are there any 'formal' good practices in testing this kind of software?

I can speak to this but I'm sure there is some good information here on stack or out in the wild.

When doing integration testing, how do I test the performance of this server in multiple connections/high latency situations (more specifically, how to simulate these situations)?

For a simple start write some test and logging routines, running them on a separate box, that hammered the server with a verity of requests...including nonsensical ones. Running log4net on both would also be helpful. Introducing some lag could be done, as already mentioned, via a sort of proxy...a box that sits between the client and server. The client points to the proxy and the proxy modifies the data...delay, change packet order, etc....sends to to the server...reverse and repeat. As a slight aside...I would avoid Thread.Sleep(...). Better to use something like this :

lock (timelock) { Monitor.Wait(timelock, TimeoutInMilliseconds); }

...asynchronous versions of Socket.Accept/Socket.Receive? Can I replace the asynchronous methods for handling their synchronous versions in separate threads?

You have many options, asynchronous methods, plain old threading, the threadpool, the TPL, etc. The best choice is application specific.

Here is an Asynchronous Server Socket Example from the MSDN entry on sockets.

As luck would have it....the O'Reilly book: C# 4.0 in a Nutshell has an excellent set of TCP Server examples that cover both asynchronous/threaded methods in blocking/non-blocking manifestations. Here is one of the examples....

  public class Server
  {
    public void Serve(IPAddress address, int port)
    {

      TcpListener listener = new TcpListener(address, port);
      listener.Start();
      while (true)
      {
        TcpClient c = listener.AcceptTcpClient();
        Task.Factory.StartNew(Accept, c);
      }
    }

    void Accept(object clientObject)
    {
      using (TcpClient client = (TcpClient)clientObject)
      {
        using (NetworkStream n = client.GetStream())
        {
          byte[] data = new byte[5000];

          int bytesRead = 0; int chunkSize = 1;

          while (bytesRead < data.Length && chunkSize > 0)
          {
            bytesRead +=
              chunkSize = n.Read /// Read is blocking
                (data, bytesRead, data.Length - bytesRead);
          }

          Array.Reverse(data);
          n.Write                /// Write is blocking
            (data, 0, data.Length);
        }
      }
    }
  }

Hope this is helpful.

P.S. Get a copy of C# 4.0 in a Nutshell...tis good.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号