My test shows that .NET Remoting is 1.5 times faster than WCF 4

To indicate whether my project should migrate from a remote .net node to WCF, I extracted part of my network and implemented its WCF. I run the remote version and the wcf version and eventually find that remote work is 1.5 times faster than wcf , which is very different from the msdn article .

Testing Configuration

WCF and .NET Remoting are used as a tcp channel without encryption, not an app.config file. Compiled in release mode, without optimization.

Operations

What my program does is do it. sequence diagram

You can download two solutions here.

WCF test

Service host

ServiceHost host = new ServiceHost(typeof(Server), new Uri(string.Format("net.tcp://{0}:{1}/{2}", args[0], args[1], args[2]))); var binding = new NetTcpBinding(); binding.MaxReceivedMessageSize = 614400; binding.ReaderQuotas.MaxArrayLength = 512000;//a max picture of 500KB binding.Security.Mode = SecurityMode.None; host.AddServiceEndpoint(typeof(IServer), binding, string.Empty); host.Open(); 

Server

 [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single //, IncludeExceptionDetailInFaults = true , ConcurrencyMode = ConcurrencyMode.Reentrant )] public class Server : IServer { public EntryRequirement GetEntryRequirement() { return new EntryRequirement(new[] { "fuck", "sex" }, false); } public void AddClient() { var client = OperationContext.Current.GetCallbackChannel<IServerCallback>(); var p = client.Profile; var x = client.Password; System.Diagnostics.Debug.WriteLine(p); System.Diagnostics.Debug.WriteLine(x); } } 

Client side

  Player player = new Player(); player.Password = "12423"; player.Profile = new Contracts.PlayerProfile { Description = "I'm a man.", HeadImage = imageData, Name = "Loveright" }; var binding = new NetTcpBinding(); binding.Security.Mode = SecurityMode.None; Stopwatch watch = new Stopwatch(); watch.Start(); for (int i = 0; i < 20; i++) { ServerProxy server = new ServerProxy(player, binding, new EndpointAddress(string.Format("net.tcp://{0}:{1}/{2}", args[0], args[1], args[2]))); server.GetEntryRequirement(); server.AddClient(); } watch.Stop(); 

HeadImage is a 139KB image.

Player class

 [CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant)] class Player : IServerCallback { public PlayerProfile Profile { get; set; } public string Password { get; set; } public void ClientCollectionChangedEventHandler(object sender, ControllersChangedEventArgs e) { } public void ClientUpdatedEventHandler(object sender, ClientUpdatedEventArgs e) { } } 

.NET Remoting Test

Host

  var serverProv = new BinaryServerFormatterSinkProvider(); serverProv.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full; var clientProv = new BinaryClientFormatterSinkProvider(); IDictionary props = new Hashtable(); props["port"] = args[1]; props["name"] = "tcp server"; var channel = new TcpChannel(props, clientProv, serverProv); ChannelServices.RegisterChannel(channel, false); System.Runtime.Remoting.RemotingConfiguration.RegisterWellKnownServiceType(typeof(Server), args[2], System.Runtime.Remoting.WellKnownObjectMode.Singleton); 

Client

  var serverProv = new BinaryServerFormatterSinkProvider(); serverProv.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full; var clientProv = new BinaryClientFormatterSinkProvider(); IDictionary props = new Hashtable(); props["name"] = "tcp client " + Guid.NewGuid(); props["port"] = 0; var channel = new TcpChannel(props, clientProv, serverProv); ChannelServices.RegisterChannel(channel, false); FileStream stream = new FileStream(@"logotz6.png", FileMode.Open); byte[] imageData = new byte[stream.Length]; stream.Read(imageData, 0, imageData.Length); stream.Close(); Player player = new Player(); player.Password = "12423"; player.Profile = new PlayerProfile { Description = "I'm a man.", HeadImage = imageData, Name = "Loveright" }; Stopwatch watch = new Stopwatch(); watch.Start(); for (int i = 0; i < 20; i++) { var serverProxy = (IServer)Activator.GetObject(typeof(IServer), string.Format("tcp://{0}:{1}/{2}", args[0], args[1], args[2])); serverProxy.GetEntryRequirement(); serverProxy.AddClient(player); } watch.Stop(); 

You can download two solutions here.

Result

enter image description here

So am I doing a test somewhere unfair to WCF?

+7
source share
1 answer

Should it be code encoding a message?

Are you using binaryMessageEncoding instead of textMessageEncoding or soapMessageEncoding?

To do this, you can create a custom binding:

 internal sealed class MyBinding : CustomBinding { private static readonly BindingElementCollection elementCollection; static MyBinding() { MessageEncodingBindingElement encoding = new BinaryMessageEncodingBindingElement(); TcpTransportBindingElement transport = new TcpTransportBindingElement(); elementCollection = new BindingElementCollection(); elementCollection.Add(encoding); elementCollection.Add(transport); } internal MyBinding(string bindingName, string bindingNamespace) : base() { base.Namespace = bindingNamespace; base.Name = bindingName; } public override BindingElementCollection CreateBindingElements() { return elementCollection; } } 
0
source

All Articles