gpt4 book ai didi

wcf - 我的测试表明 .NET Remoting 比 WCF 4 快 1.5 倍

转载 作者:行者123 更新时间:2023-12-04 12:59:31 25 4
gpt4 key购买 nike

为了判断我的项目是否应该从 .net 远程处理迁移到 WCF,我提取了它的网络通信部分并由 WCF 实现。我运行remoting版本和wcf版本,最终找到远程处理比 wcf 快 1.5 倍 , 与 the msdn article 有很大不同.

测试配置

WCF 和 .NET Remoting 都使用没有加密的 tcp channel ,没有 app.config 文件。在 Release模式下编译,没有优化。

运营

我的程序所做的就是这个。
sequence diagram

You can download the two solutions here.

WCF 测试

服务主机

    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();

服务器
[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);
}
}

客户端
    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 是一张大小为 139KB 的图片。

玩家等级
[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 远程测试

主持人
    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);

客户
    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 the two solutions here.

结果

enter image description here

那么我是否在对 WCF 不公平的地方进行测试?

最佳答案

它应该是消息编码王吗?

您是否使用 binaryMessageEncoding 而不是 textMessageEncoding 或 soapMessageEncoding ?

您可以创建自定义绑定(bind)来执行此操作:

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;
}
}

关于wcf - 我的测试表明 .NET Remoting 比 WCF 4 快 1.5 倍,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9225725/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com