Remoting和Web Service是.net中的重要技术,都可用来实现分布式系统开发,如果是不同的平台就只能选择Web Service,但如果是同一平台,就都可以选择了。到底选择那种,当然还有访问效率上的考虑,同时在Remoting中又有三中信道 Http,Tcp,Ipc,它们又各有差别。HTTP方式的信道在跨越防火墙上有优势;TCP方式的信道常用在局域网内通信,速度比HTTP快很 多;IPC信道用于同一台机器的进程间通信,通信不占用网络资源,速度又比TCP快很多。为了能够实际的比较一下这四者的实际访问速度,我写了个小程序用 测试。这个程序的实现很简单利用Remoting三种信道和Web Service 访问同一个对象(相当于实际项目中的业务层),而这个对象实现返回系统的时间。就这么简单。如果有对Remoting和Web Service不太了解的,也可以通过我这个例子熟悉一下Remoting三种信道的写法差别和Web Service的调用。
下面是程序运行的界面,我使用.net中的最小时间度量:刻度(用毫秒在本机上可能都很难测出它们之间的差别),来测试每次调用所发的时间,并通过多次调 用来测的一个平均时间来比较访问的速度。通过测试可以看得出他们四者得访问速度:ipc>tcp>http>Web Service.(其实Remoting的http信道和Web Service的访问速度还有待比较,跟测试的主机还有一定关系,在我办公室里的一台电脑上好像Web service的访问速度更快于http信道),大家可以自己测试一下,或研究一个比较好的方法。
相关代码:
1 //使用Http信道 2 public void Http() 3 { 4 Stopwatch stopWatch = new Stopwatch(); 5 stopWatch.Start(); 6 MyObject myObj = (MyObject)Activator.GetObject(typeof(MyObject), "http://localhost:9001/MyObject"); 7 myObj.GetServerTime(); 8 stopWatch.Stop(); 9 lsbHttp.Items.Add(stopWatch.ElapsedTicks); 10 } 11 //使用Tcp信道 12 public void Tcp() 13 { 14 Stopwatch stopWatch = new Stopwatch(); 15 stopWatch.Start(); 16 MyObject myObj = (MyObject)Activator.GetObject(typeof(MyObject), "tcp://localhost:9002/MyObject"); 17 myObj.GetServerTime(); 18 stopWatch.Stop(); 19 lsbTcp.Items.Add(stopWatch.ElapsedTicks); 20 } 21 //使用Ipc信道 22 public void Ipc() 23 { 24 Stopwatch stopWatch = new Stopwatch(); 25 stopWatch.Start(); 26 MyObject myObj = (MyObject)Activator.GetObject(typeof(MyObject), "Ipc://MyHost/MyObject"); 27 myObj.GetServerTime(); 28 stopWatch.Stop(); 29 lsbIpc.Items.Add(stopWatch.ElapsedTicks); 30 } 31 32 //访问Web Service 33 public void WebService() 34 { 35 Stopwatch stopWatch = new Stopwatch(); 36 stopWatch.Start(); 37 localhost.Service ws = new localhost.Service(); 38 ws.GetServerTime(); 39 stopWatch.Stop(); 40 lsbWeb.Items.Add(stopWatch.ElapsedTicks); 41 } 42 private void btnHttp_Click(object sender, EventArgs e) 43 { 44 Http(); 45 } 46 47 private void btnTcp_Click(object sender, EventArgs e) 48 { 49 Tcp(); 50 } 51 52 private void btnWebService_Click(object sender, EventArgs e) 53 { 54 WebService(); 55 } 56 57 private void btnIpc_Click(object sender, EventArgs e) 58 { 59 Ipc(); 60 } 61 62 //开始测试 63 private void btnStat_Click(object sender, EventArgs e) 64 { 65 Int32 Times = int.Parse(txtTimes.Text); 66 Int64 Sum = 0; 67 double Ave=0; 68 lsbHttp.Items.Clear(); 69 lsbIpc.Items.Clear(); 70 lsbTcp.Items.Clear(); 71 lsbWeb.Items.Clear(); 72 73 for (Int32 i = 0; i < Times; i++) 74 { 75 Http(); 76 Tcp(); 77 Ipc(); 78 WebService(); 79 } 80 //计算平均时间 81 for(Int32 i=0;i<Times;i++) 82 { 83 Sum += int.Parse(lsbHttp.Items[i].ToString ()); 84 } 85 Ave = Sum / Times; 86 txtHttp.Text = Ave.ToString(); 87 88 Sum = 0; 89 for (Int32 i = 0; i < Times; i++) 90 { 91 Sum += int.Parse(lsbTcp.Items[i].ToString()); 92 } 93 Ave = Sum / Times; 94 txtTcp.Text = Ave.ToString(); 95 96 Sum = 0; 97 for (Int32 i = 0; i < Times; i++) 98 { 99 Sum += int.Parse(lsbWeb.Items[i].ToString()); 100 } 101 Ave = Sum / Times; 102 txtWebService.Text = Ave.ToString(); 103 104 Sum = 0; 105 for (Int32 i = 0; i < Times; i++) 106 { 107 Sum += int.Parse(lsbIpc.Items[i].ToString()); 108 } 109 Ave = Sum / Times; 110 txtIpc.Text = Ave.ToString(); 111 } 112 HttpChannel httpChannel = new HttpChannel(9001); 113 ChannelServices.RegisterChannel(httpChannel,false ); 114 115 TcpChannel tcpChannel = new TcpChannel(9002); 116 ChannelServices.RegisterChannel(tcpChannel,false ); 117 118 IpcChannel ipcChannel = new IpcChannel("MyHost"); 119 ChannelServices.RegisterChannel(ipcChannel,false ); 120 121 RemotingConfiguration .RegisterWellKnownServiceType (typeof (RemoteObject .MyObject ),"MyObject",WellKnownObjectMode.SingleCall); 122 Console.ReadLine();