gpt4 book ai didi

c# - WCF 与 .Net 远程处理

转载 作者:IT王子 更新时间:2023-10-29 04:12:55 24 4
gpt4 key购买 nike

根据 this article , 带命名管道的 WCF 是 IPC 的最佳选择,它比 .Net Remoting 快 25% 左右。

我有以下代码将 WCF 与命名管道与 .Net Remoting 进行比较:

[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[] args)
{
TestWcf(Iterations);
TestRemoting(Iterations);

TestWcf(Iterations);
TestRemoting(Iterations);

TestWcf(Iterations);
TestRemoting(Iterations);

Console.ReadKey();
}

private static void TestRemoting(int iterations)
{
var domain = AppDomain.CreateDomain("TestDomain");

var proxy =
(IRemote)
domain.CreateInstanceFromAndUnwrap(Assembly.GetEntryAssembly().Location, "ConsoleApplication6.Remote");

Console.WriteLine("Remoting: {0} ms.", Test(proxy, iterations));
}

private static void TestWcf(int iterations)
{
var address = "net.pipe://localhost/test";

var host = new ServiceHost(typeof (Remote));
host.AddServiceEndpoint(typeof (IRemote), new NetNamedPipeBinding(), address);
host.Open();

var proxy = ChannelFactory<IRemote>.CreateChannel(new NetNamedPipeBinding(), new EndpointAddress(address));

Console.WriteLine("Wcf: {0} ms.", Test(proxy, iterations));

host.Close();
}

private static double Test(IRemote proxy, int iterations)
{
var start = DateTime.Now;

for (var i = 0; i < iterations; i++)
{
proxy.Hello("Sergey");
}

var stop = DateTime.Now;

return (stop - start).TotalMilliseconds;
}
}

A 迭代 5000 次得到如下结果:

Wcf: 14143 ms.
Remoting: 2232 ms.
Wcf: 14289 ms.
Remoting: 2130 ms.
Wcf: 14126 ms.
Remoting: 2112 ms.

在此测试中,Wcf 比 .Net Remoting 慢大约 7 倍。

我尝试过:

  • 将安全模式设置为无;
  • 将 InstanceContextMode 设置为 Single/PerCall;
  • 将并发模式设置为单/多;

但结果是一样的。

有人知道我做错了什么吗?为什么 WCF 这么慢?

有没有办法加快这段代码的速度?

提前致谢。

编辑:

我对测试做了一点修改。契约(Contract)是一样的。

第一个测试看起来像这样(Wcf 测试):

class Program
{
private const int Iterations = 5000;

static void Main(string[] args)
{
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<IRemote>.CreateChannel(new NetNamedPipeBinding(NetNamedPipeSecurityMode.None), new EndpointAddress(address));

TestWcf(proxy, Iterations);
TestWcf(proxy, Iterations);
TestWcf(proxy, Iterations);
TestWcf(proxy, Iterations);
TestWcf(proxy, Iterations);

Console.ReadKey();

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

结果如下:

Wcf: 2564 ms.
Wcf: 1026 ms.
Wcf: 986 ms.
Wcf: 990 ms.
Wcf: 992 ms.

第二个测试看起来像这样(.Net Remoting 测试):

class Program
{
private const int Iterations = 5000;

static void Main(string[] args)
{
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.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);
}
}

结果如下:

Remoting: 261 ms.
Remoting: 224 ms.
Remoting: 252 ms.
Remoting: 243 ms.
Remoting: 234 ms.

如您所见,.Net Remoting 再次变得更快。测试在调试器之外运行。

为什么会这样?

最佳答案

当尝试比较性能时,调试器并不是真正的衡量标准,这是我所做的,并从环中获得了 WCF 踢出远程处理 ;)

1) 还修改了您的测试以从相同的程序/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<IRemote>.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) 在 Release模式下编译并在调试器外运行。

这是我的输出 enter image description here

关于c# - WCF 与 .Net 远程处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8055565/

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