IPC、TCP、HTTP方式通讯速度比较,附源代码

一、运行环境

  • win7
  • 16g内存
  • cpu i7 2.7GHz
  • VS2017

二、结果展示

IPC、TCP、HTTP方式通讯速度比较,附源代码_第1张图片

 Http属于应用层协议,在Tcp协议上层,二Ipc则是同一物理机器上跨进程的通讯方式。很明显,在用时上Ipc

三、核心程序

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Ipc;
using System.Diagnostics;
using RemotingObjects;

namespace compareipctcphttp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

       
        //使用Http信道
        public void Http()
        {
            Stopwatch stopWatch = new Stopwatch();
            stopWatch.Start();
            IPerson obj = (IPerson)Activator.GetObject(typeof(RemotingObjects.IPerson), "http://localhost:9001/RemotingPersonService");
            obj.getName("wwwwwwwwwwwwwwwwwwwwwww");
            stopWatch.Stop();
            lsbHttp.Items.Add(stopWatch.ElapsedTicks);
        }
        //使用Tcp信道
        public void Tcp()
        {
            Stopwatch stopWatch = new Stopwatch();
            stopWatch.Start();            
            IPerson obj = (IPerson)Activator.GetObject(typeof(RemotingObjects.IPerson), "tcp://localhost:9002/RemotingPersonService");            
            obj.getName("wwwwwwwwwwwwwwwwwwwwwww");
            stopWatch.Stop();
            lsbTcp.Items.Add(stopWatch.ElapsedTicks);
        }
        //使用Ipc信道
        public void Ipc()
        {
            Stopwatch stopWatch = new Stopwatch();
            stopWatch.Start();
            IPerson obj = (IPerson)Activator.GetObject(typeof(RemotingObjects.IPerson), "Ipc://MyHost/RemotingPersonService");
            obj.getName("wwwwwwwwwwwwwwwwwwwwwww");
            stopWatch.Stop();
            lsbIpc.Items.Add(stopWatch.ElapsedTicks);
        }

       
        private void btnHttp_Click(object sender, EventArgs e)
        {
            Http();
        }

        private void btnTcp_Click(object sender, EventArgs e)
        {
            Tcp();
        }

      
        private void btnIpc_Click(object sender, EventArgs e)
        {
            Ipc();
        }

        //开始测试
        private void btnStat_Click(object sender, EventArgs e)
        {
            Int32 Times = int.Parse(txtTimes.Text);
            Int64 Sum = 0;
            double Ave = 0;
            lsbHttp.Items.Clear();
            lsbIpc.Items.Clear();
            lsbTcp.Items.Clear();
         
            for (Int32 i = 0; i < Times; i++)
            {
                Http();
                Tcp();
                Ipc();             
            }
            //计算平均时间
            for (Int32 i = 0; i < Times; i++)
            {
                Sum += int.Parse(lsbHttp.Items[i].ToString());
            }
            Ave = Sum / Times;
            txtHttp.Text = Ave.ToString();

            Sum = 0;
            for (Int32 i = 0; i < Times; i++)
            {
                Sum += int.Parse(lsbTcp.Items[i].ToString());
            }
            Ave = Sum / Times;
            txtTcp.Text = Ave.ToString();
           
            Sum = 0;
            for (Int32 i = 0; i < Times; i++)
            {
                Sum += int.Parse(lsbIpc.Items[i].ToString());
            }
            Ave = Sum / Times;
            txtIpc.Text = Ave.ToString();
        }
       
    }
}

四、源代码

https://github.com/timzhangliang/compareipctcphttp

https://github.com/timzhangliang/RemotingServer

先打开remoteserver控制台程序,再打开compareipctcphttp即可测试。

五、交流方式

微信号maidianb,欢迎同行交流指点。

你可能感兴趣的:(IPC、TCP、HTTP方式通讯速度比较,附源代码)