Remoting三種通道Http,Tcp,IPC和Web Service的訪問速度比較(轉)

來源:互聯網
上載者:User

標籤:style   blog   class   code   c   java   

  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();

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.