兩段極為相似的代碼,主要想看看過輸與序列化過程兩者的用時差異,結果10000次的調用,WCF用了11秒多,remoting用了5秒不到!這是測試的源代碼 WCF的服務端 Remoting的服務端 代碼可以理解為一樣的,以下的客戶端,兩客戶端也可以視為一樣的過程WCF客戶端 Remoting客戶端 ...
兩段極為相似的代碼,主要想看看過輸與序列化過程兩者的用時差異,結果10000次的調用,WCF用了11秒多,remoting用了5秒不到!
這是測試的源代碼
Remoting的服務端
public class RemotCalc : MarshalByRefObject, ICalc { public CalcInfo Calc(CalcInfo pInfo) { CalcInfo info = new CalcInfo(); info.Method = string.Format("back_{0}", pInfo.Method); info.Para1 = pInfo.Para1; info.Para2 = pInfo.Para2; info.Result = pInfo.Result + 999; return info; } }
WCF的服務端
public class WcfCalc : Srv.Interface.ICalc { public CalcInfo Calc(CalcInfo pInfo) { CalcInfo info = new CalcInfo(); info.Method = string.Format("back_{0}", pInfo.Method); info.Para1 = pInfo.Para1; info.Para2 = pInfo.Para2; info.Result = pInfo.Result + 999; return info; } }
代碼可以理解為一樣的,以下的客戶端,兩客戶端也可以視為一樣的過程
WCF客戶端
static void Main(string[] args) { using(ChannelFactory<Srv.Interface.ICalc> factory = new ChannelFactory<Srv.Interface.ICalc>("Calc2")) { Srv.Interface.ICalc calc = factory.CreateChannel(); CalcInfo info = new CalcInfo(); info.Method = "test"; Console.WriteLine("press any key to run..."); Console.ReadLine(); int max = 10000; Console.WriteLine("it's run..."); DateTime start = DateTime.Now; for (int i = 0; i < max; i++) { CalcInfo res = calc.Calc(info); } TimeSpan sp = DateTime.Now - start; Console.WriteLine("run {0} times use {1}ms ", max, sp.TotalMilliseconds); Console.ReadLine(); } }
Remoting客戶端
static void Main(string[] args) { ChannelServices.RegisterChannel(new TcpClientChannel(), false); ICalc remoteobj = (ICalc)Activator.GetObject(typeof(ICalc), "tcp://localhost:6666/Calc"); CalcInfo info = new CalcInfo(); info.Method = "test"; Console.WriteLine("press any key to run..."); Console.ReadLine(); int max = 10000; DateTime start = DateTime.Now; for (int i = 0; i < max; i++) { CalcInfo res = remoteobj.Calc(info); } TimeSpan sp = DateTime.Now - start; Console.WriteLine("run {0} times use {1}ms ", max, sp.TotalMilliseconds); Console.ReadLine(); }