WCF vs. .Net Remoting

前端 未结 4 1858
遥遥无期
遥遥无期 2021-01-30 13:50

according to this article, WCF with named pipes is the best choice for IPC, and it is around 25 % faster than .Net Remoting.

I have the following code that compares WCF

4条回答
  •  时光说笑
    2021-01-30 14:48

    Debuggers are not real measure when try to compare the performance , here is what I did and got WCF Kicking out Remoting from the ring ;)

    1) Also modified your test to run it from same program/exe

      namespace ConsoleApplication6
    {
      [ServiceContract]
      internal interface IRemote
      {
        [OperationContract]
        string Hello(string name);
      }
    
      [ServiceBehavior]
      internal class Remote : MarshalByRefObject, IRemote
      {
        public string Hello(string name)
        {
          return string.Format("Hello, {0}!", name);
        }
      }
    
      class Program
      {
        private const int Iterations = 5000;
    
        static void Main(string[] p)
        {
          TestWcf();
          TestRemoting();
        }
    
    
        static void TestWcf()
        {
          var address = "net.pipe://localhost/test";
    
          var host = new ServiceHost(typeof(Remote));
          host.AddServiceEndpoint(typeof(IRemote), new NetNamedPipeBinding(NetNamedPipeSecurityMode.None), address);
          host.Open();
    
          var proxy = ChannelFactory.CreateChannel(new NetNamedPipeBinding(NetNamedPipeSecurityMode.None), new EndpointAddress(address));
    
          TestWcf(proxy, Iterations);
          TestWcf(proxy, Iterations);
          TestWcf(proxy, Iterations);
          TestWcf(proxy, Iterations);
          TestWcf(proxy, Iterations);
    
          Console.WriteLine("WCF done");
    
          host.Close();
        }
    
        private static void TestWcf(IRemote proxy, int iterations)
        {
          var start = DateTime.Now;
    
          for (var i = 0; i < iterations; i++)
          {
            proxy.Hello("Sergey");
          }
    
          var stop = DateTime.Now;
    
          Console.WriteLine("Wcf: {0} ms.", (stop - start).TotalMilliseconds);
        }
    
        static void TestRemoting()
        {
          var domain = AppDomain.CreateDomain("TestDomain");
    
          var proxy =
              (IRemote)
              domain.CreateInstanceFromAndUnwrap(Assembly.GetEntryAssembly().Location, "ConsoleApplication6.Remote");
    
          TestRemoting(proxy, Iterations);
          TestRemoting(proxy, Iterations);
          TestRemoting(proxy, Iterations);
          TestRemoting(proxy, Iterations);
          TestRemoting(proxy, Iterations);
          Console.WriteLine("Remoting done");
          Console.ReadKey();
        }
    
        private static void TestRemoting(IRemote proxy, int iterations)
        {
          var start = DateTime.Now;
    
          for (var i = 0; i < iterations; i++)
          {
            proxy.Hello("Sergey");
          }
    
          var stop = DateTime.Now;
    
          Console.WriteLine("Remoting: {0} ms.", (stop - start).TotalMilliseconds);
        }
      }
    
    }
    

    2) Compile it in release mode and ran it outside debugger.

    here is my output enter image description here

提交回复
热议问题