我是在本地测试 测试代码是做了一个根据用户登陆查询的功能。
使用我自己写的一个hosting承载了一个服务 地址:http://127.0.0.1:83/Client/Login

代码如下
private void ThreadStart()
{
webServiceHost = new WebServiceHost(typeof(ClientService), new Uri("http://127.0.0.1:83/Client"));
webServiceHost.AddServiceEndpoint(typeof(IClientService), new WebHttpBinding(), "");
webServiceHost.Description.Endpoints[0].Behaviors.Add(new WebHttpBehavior { HelpEnabled = true });
webServiceHost.Opened += delegate
{
ServiceEndpoint endpoinit = webServiceHost.Description.Endpoints[0];
MessageBox.Show(string.Format("服务开启!\n服务地址:{0}", endpoinit.Address));
};
webServiceHost.Open();
}
又使用WCF主机服务承载了一个相同的服务 地址:http://127.0.0.1:82/Client/Login
写了一个控制台应用程序用来测试
public static void DoWork()
{
using (WebClient webClient = new WebClient())
{
while (Run)
{
try
{
DateTime olderTime = DateTime.Now;
webClient.Headers.Add("Content-Type", "application/json");
webClient.Encoding = Encoding.UTF8;
string PostData = "{\"Password\":\"" + rd.Next(99999999) + "\",\"UserName\":\"" + rd.Next(99999999) + "\",\"isRember\":true}";
string result = webClient.UploadString("http://127.0.0.1:82/Client/Login", PostData);
totalNum++;
DateTime newTime = DateTime.Now;
TimeSpan ts = newTime - olderTime;
totalMilliseconds += ts.TotalMilliseconds;
Console.WriteLine(string.Format("线程{0}耗时{1} 平均耗时{2} 次数:{3} 结果:", Thread.CurrentThread.GetHashCode(), ts.TotalMilliseconds, totalMilliseconds / totalNum, totalNum));
Console.WriteLine(result);
}
catch
{
}
}
}
}
我自己的测试图
WCF主机服务的测试图
可以看到效率差了 5倍左右 有的时候能差10倍。
用的是多线程处理,其中Message消息中是数据库查询耗时,线程耗时是总耗时。
想知道这是为什么?