【发布时间】:2010-12-06 22:40:16
【问题描述】:
我正在尝试评估在新系统中使用哪种通信技术,现在看起来 Remoting 是我们唯一的选择,因为 WCF 的性能很糟糕。
与调用控制台应用程序中托管的远程处理接口相比,我已经对使用 nettcp 调用托管在 IIS7 中的 WCF 服务进行了基准测试。 WCF 服务需要大约 4.5 秒来执行 1000 个请求,在端点上同步(它只是返回一个对象的新实例)。远程客户端执行相同任务所需的时间
这是 WCF 客户端代码:
public class TcpNewsService : INewsService
{
private INewsService _service = null;
Lazy<ChannelFactory<INewsService>> _newsFactory = new Lazy<ChannelFactory<INewsService>>(() =>
{
var tcpBinding = new NetTcpBinding
{
//MaxBufferPoolSize = int.MaxValue,
//MaxBufferSize = int.MaxValue,
//MaxConnections = int.MaxValue,
//MaxReceivedMessageSize = int.MaxValue,
PortSharingEnabled=false,
TransactionFlow = false,
ListenBacklog = int.MaxValue,
Security = new NetTcpSecurity
{
Mode = SecurityMode.None,
Transport = new TcpTransportSecurity
{
ProtectionLevel = System.Net.Security.ProtectionLevel.None,
ClientCredentialType = TcpClientCredentialType.None
},
Message = new MessageSecurityOverTcp
{
ClientCredentialType = MessageCredentialType.None }
},
ReliableSession = new OptionalReliableSession { Enabled = false }
};
EndpointAddress endpointAddress = new EndpointAddress("net.tcp://localhost:8089/NewsService.svc");
return new ChannelFactory<INewsService>(tcpBinding, endpointAddress);
});
public TcpNewsService()
{
_service = _newsFactory.Value.CreateChannel();
((ICommunicationObject)_service).Open();
}
public List<NewsItem> GetNews()
{
return _service.GetNews();
}
}
还有一个简单的控制台应用程序来调用客户端代码:
var client = new TcpNewsService();
Console.WriteLine("Getting all news");
var sw = new System.Diagnostics.Stopwatch();
sw.Start();
for (int i = 0; i < 1000; i++)
{
var news = client.GetNews();
}
sw.Stop();
Console.WriteLine("Finished in " + sw.Elapsed.TotalSeconds);
Console.ReadLine();
IIS 主机的 web.config 文件如下所示:
<system.serviceModel>
<services>
<service behaviorConfiguration="NewsServiceBehavior" name="RiaSpike.News.Service.NewsService">
<endpoint address=""
binding="netTcpBinding"
bindingConfiguration="tcpBinding"
contract="RiaSpike.News.Types.INewsService">
</endpoint>
<endpoint address="http://localhost:8094/NewsService.svc"
binding="basicHttpBinding"
bindingConfiguration="httpBinding"
contract="RiaSpike.News.Types.INewsService">
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="NewsServiceBehavior">
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<basicHttpBinding>
<binding name="httpBinding">
<security mode="None">
<transport clientCredentialType="None" />
</security>
</binding>
</basicHttpBinding>
<netTcpBinding>
<binding name="tcpBinding" portSharingEnabled="false">
<security mode="None">
<transport clientCredentialType="None" />
<message clientCredentialType="None" />
</security>
<reliableSession enabled="false" />
</binding>
</netTcpBinding>
</bindings>
以及托管在 IIS 中的服务类:
[ServiceBehavior(
ConcurrencyMode = ConcurrencyMode.Multiple,
InstanceContextMode = InstanceContextMode.Single,
AddressFilterMode = AddressFilterMode.Any
)]
public class NewsService : MarshalByRefObject, INewsService
{
public List<NewsItem> GetNews()
{
return new List<NewsItem>
{
new NewsItem { Descripion = "The Description", Id = 1, Title = "The Title"}
};
}
}
我跟踪了 WCF 活动,发现该过程大约需要 5 毫秒才能完成(我无法上传图像,这是日志中的一个活动跟踪)
发件人:处理消息 5。转帐 3/12/2010 15:35:58.861
活动边界。开始时间 2010 年 3 月 12 日 15:35:58.861
通过频道收到消息。信息 2010 年 3 月 12 日 15:35:58.861
收件人:执行“Ria.Spike.News.INewsService.GetNews”传输 3/12/2010 15:35:58.864
活动边界。暂停 2010 年 3 月 12 日 15:35:58.864
来自:执行“Ria.Spike.News.INewsService.GetNews”传输 3/12/2010 15:35:58.864
活动边界。简历 2010 年 3 月 12 日 15:35:58.864
通过频道信息发送消息 2010 年 3 月 12 日 15:35:58.866
活动边界。停止 2010 年 3 月 12 日 15:35:58.866
这是最好的吗:s
这是本示例中使用的远程处理代码。
var iserver = (INewsService)Activator.GetObject(typeof(INewsService), "tcp://127.0.0.1:9000/news");
var sw = new System.Diagnostics.Stopwatch();
sw.Start();
for (int i = 0; i < 1000; i++)
{
var news = iserver.GetNews();
}
sw.Stop();
Console.WriteLine("Finished in " + sw.Elapsed.TotalSeconds);
Console.ReadLine();
以及在 IIS 中托管此远程处理通道的 TCP 端点:
public class Global : System.Web.HttpApplication
{
private TcpServerChannel _quote;
protected void Application_Start(object sender, EventArgs e)
{
_quote = new TcpServerChannel(9000);
if (ChannelServices.RegisteredChannels.Length ==0)
{
ChannelServices.RegisterChannel(_quote, false);
}
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(NewsService),
"news",
WellKnownObjectMode.SingleCall);
_quote.StartListening(null);
}
}
【问题讨论】:
-
有兴趣看看 WCF 版本是否存在启动惩罚。您能否尝试在至少调用一次之前不启动 sw 的运行并发布结果?
-
嗨彼得,我确实调用了两次 1000 调用迭代来消除启动成本。第一次和第二次之间的时间差可以忽略不计。
-
你的远程配置是什么?
-
添加了上面的Remoting代码,它的美在于它的简单:D
-
要执行正确的测试,我认为您也应该在控制台主机中托管 WCF 服务 - 删除其中一个变体。尽管像 James 一样,我怀疑问题在于远程处理使用二进制编码,而 netTcp 使用 SOAP 编码。