我是在本地測試 測試代碼是做了一個根據使用者登陸查詢的功能。
使用我自己寫的一個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消息中是資料庫查詢耗時,線程耗時是總耗時。
想知道這是為什麼?