阅读 Lowy 声明的关键词是“<em>in the interest of throughput”。Lowy 指出,在使用 ConcurrencyMode.Single 时,WCF 会盲目地实现一个锁来强制对服务实例进行序列化。锁很昂贵,而且这个不是必需的,因为 PerCall 已经保证第二个线程永远不会尝试调用同一个服务实例。
在行为方面:
ConcurrencyMode 对于 PerCall 服务实例无关紧要。
在性能方面:
ConcurrencyMode.Multiple 的 PerCall 服务应该稍微快一些,因为它没有创建和获取 ConcurrencyMode.Single 正在使用的(不需要的)线程锁。
我写了一个快速的基准测试程序,看看我是否可以测量单个与多个 PerCall 服务的性能影响:基准测试显示没有有意义的差异。
如果您想尝试自己运行它,我将其粘贴在下面的代码中。
我试过的测试用例:
- 600 个线程调用服务 500 次
- 200 个线程调用服务 1000 次
- 8个线程调用服务10000次
- 1个线程调用服务10000次
我在运行 Service 2008 R2 的 4 CPU VM 上运行它。除了 1 线程情况外,所有情况都受 CPU 限制。
结果:所有的运行都在大约 5% 的范围内。
有时 ConcurrencyMode.Multiple 更快。有时 ConcurrencyMode.Single 更快。也许适当的统计分析可以选出赢家。在我看来,它们足够接近,无关紧要。
这是一个典型的输出:
在 net.pipe://localhost/base 上
启动单一服务... Type=SingleService ThreadCount=600 ThreadCallCount=500运行时:45156759 滴答 12615 毫秒
在 net.pipe://localhost/base 上
启动多项服务... Type=MultipleService ThreadCount=600 ThreadCallCount=500运行时:48731273 滴答 13613 毫秒
在 net.pipe://localhost/base 上
启动单一服务... Type=SingleService ThreadCount=600 ThreadCallCount=500运行时:48701509 滴答 13605 毫秒
在 net.pipe://localhost/base 上
启动多项服务... Type=MultipleService ThreadCount=600 ThreadCallCount=500运行时:48590336 滴答 13574 毫秒
基准代码:
通常的警告:这是基准代码,它采用了不适合生产使用的捷径。
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace WCFTest
{
[ServiceContract]
public interface ISimple
{
[OperationContract()]
void Put();
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Single)]
public class SingleService : ISimple
{
public void Put()
{
//Console.WriteLine("put got " + i);
return;
}
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Multiple)]
public class MultipleService : ISimple
{
public void Put()
{
//Console.WriteLine("put got " + i);
return;
}
}
public class ThreadParms
{
public int ManagedThreadId { get; set; }
public ServiceEndpoint ServiceEndpoint { get; set; }
}
public class BenchmarkService
{
public readonly int ThreadCount;
public readonly int ThreadCallCount;
public readonly Type ServiceType;
int _completed = 0;
System.Diagnostics.Stopwatch _stopWatch;
EventWaitHandle _waitHandle;
bool _done;
public BenchmarkService(Type serviceType, int threadCount, int threadCallCount)
{
this.ServiceType = serviceType;
this.ThreadCount = threadCount;
this.ThreadCallCount = threadCallCount;
_done = false;
}
public void Run(string baseAddress)
{
if (_done)
throw new InvalidOperationException("Can't run twice");
ServiceHost host = new ServiceHost(ServiceType, new Uri(baseAddress));
host.Open();
Console.WriteLine("Starting " + ServiceType.Name + " on " + baseAddress + "...");
_waitHandle = new EventWaitHandle(false, EventResetMode.ManualReset);
_completed = 0;
_stopWatch = System.Diagnostics.Stopwatch.StartNew();
ServiceEndpoint endpoint = host.Description.Endpoints.Find(typeof(ISimple));
for (int i = 1; i <= ThreadCount; i++)
{
// ServiceEndpoint is NOT thread safe. Make a copy for each thread.
ServiceEndpoint temp = new ServiceEndpoint(endpoint.Contract, endpoint.Binding, endpoint.Address);
ThreadPool.QueueUserWorkItem(new WaitCallback(CallServiceManyTimes),
new ThreadParms() { ManagedThreadId = i, ServiceEndpoint = temp });
}
_waitHandle.WaitOne();
host.Shutdown();
_done = true;
//Console.WriteLine("All DONE.");
Console.WriteLine(" Type=" + ServiceType.Name + " ThreadCount=" + ThreadCount + " ThreadCallCount=" + ThreadCallCount);
Console.WriteLine(" runtime: " + _stopWatch.ElapsedTicks + " ticks " + _stopWatch.ElapsedMilliseconds + " msec");
}
public void CallServiceManyTimes(object threadParams)
{
ThreadParms p = (ThreadParms)threadParams;
ChannelFactory<ISimple> factory = new ChannelFactory<ISimple>(p.ServiceEndpoint);
ISimple proxy = factory.CreateChannel();
for (int i = 1; i < ThreadCallCount; i++)
{
proxy.Put();
}
((ICommunicationObject)proxy).Shutdown();
factory.Shutdown();
int currentCompleted = Interlocked.Increment(ref _completed);
if (currentCompleted == ThreadCount)
{
_stopWatch.Stop();
_waitHandle.Set();
}
}
}
class Program
{
static void Main(string[] args)
{
BenchmarkService benchmark;
int threadCount = 600;
int threadCalls = 500;
string baseAddress = "net.pipe://localhost/base";
for (int i = 0; i <= 4; i++)
{
benchmark = new BenchmarkService(typeof(SingleService), threadCount, threadCalls);
benchmark.Run(baseAddress);
benchmark = new BenchmarkService(typeof(MultipleService), threadCount, threadCalls);
benchmark.Run(baseAddress);
}
baseAddress = "http://localhost/base";
for (int i = 0; i <= 4; i++)
{
benchmark = new BenchmarkService(typeof(SingleService), threadCount, threadCalls);
benchmark.Run(baseAddress);
benchmark = new BenchmarkService(typeof(MultipleService), threadCount, threadCalls);
benchmark.Run(baseAddress);
}
Console.WriteLine("Press ENTER to close.");
Console.ReadLine();
}
}
public static class Extensions
{
static public void Shutdown(this ICommunicationObject obj)
{
try
{
if (obj != null)
obj.Close();
}
catch (Exception ex)
{
Console.WriteLine("Shutdown exception: {0}", ex.Message);
obj.Abort();
}
}
}
}