• wcf不同协议的调用效率比较


    项目中使用的wcf服务调用起来反映比较慢,昨天看了下怎么使用tcp协议传输,今天就对tcp和http的调用效率做了一个比较。

    一.寄宿与iis的tcp协议的服务调用和控制台自寄宿http协议的服务调用的比较:

    因为tcp协议不能使用控制台自寄宿,所以用iis来代替,http使用轻量级的自寄宿。

    客户端的调用:

            static void Main(string[] args)
            {
                //比较一下使用http和tcp的性能
                TestHttpService();
                TestTcpService();
                Console.Read();
            }
    
            private static void TestTcpService()
            { 
                 using (ChannelFactory<ICalculator> channelFactory = new ChannelFactory<ICalculator>("calculatorservice"))
                 {
                    ICalculator calculator = channelFactory.CreateChannel();
                    using (calculator as IDisposable)
                    {
                        try
                        {
                            DateTime beginTime = DateTime.Now;
                            calculator.Add(1, 2);
                            DateTime endTime = DateTime.Now;
                            Console.WriteLine("tcp调用花费时间:" + (endTime-beginTime).TotalMilliseconds.ToString());    
                        }
                        catch (CommunicationException)
                        {
                            (calculator as ICommunicationObject).Abort();
                            throw;
                        }
                        catch (TimeoutException)
                        {
                            (calculator as ICommunicationObject).Abort();
                            throw;
                        }
                    }
                }
            }
    
            private static void TestHttpService()
            {
                using (HttpService.CalculatorClient client = new HttpService.CalculatorClient())
                {
                    try
                    {
                        DateTime beginTime = DateTime.Now;
                        client.Add(1, 2);
                        DateTime endTime = DateTime.Now;
                        Console.WriteLine("http调用花费时间:" + (endTime - beginTime).TotalMilliseconds.ToString());    
                    }
                    catch (CommunicationException)
                    {
                        (client as ICommunicationObject).Abort();
                        throw;
                    }
                    catch (TimeoutException)
                    {
                        (client as ICommunicationObject).Abort();
                        throw;
                    }
                }
            }

    控制台自寄宿的实现:

    static void Main(string[] args)
            {
                using (ServiceHost host = new ServiceHost(typeof(CalculatorService)))
                {
                    host.AddServiceEndpoint(typeof(ICalculator), new WSHttpBinding(), "http://127.0.0.1:9999/calculatorservice");
                   if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null)
                    {
                        ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
                        behavior.HttpGetEnabled = true;
                        behavior.HttpGetUrl = new Uri("http://127.0.0.1:9999/calculatorservice/metadata");
                         host.Description.Behaviors.Add(behavior);
                    }
                  host.Opened += delegate
                    {
                        Console.WriteLine("CalculaorService已经启动,按任意键终止服务!");
                    };
     
                     host.Open();
                    Console.Read();
                }
            }

    wcf的调用,使用毫秒级的单位就可以体现出来区别了,运行自寄宿程序,启动iis,第一次调用的速度真是坑,tcp的调用也用了800毫秒,而http竟然用了6秒多:

    第二次运行速度就快了很多,不过http还是比较慢:

    二.wcf应用程序的http协议和控制台寄宿的http协议的效率比较:

    第一次调用:

    第二次调用,vs调试器的效率竟然基本没有变化,被自寄宿的给爆成渣了:

    应该比较一下iis寄宿的http协议的效率,不过老是报错,今天就先到这里,过两天补上,未完待续...

  • 相关阅读:
    [转]理解I/O Completion Port
    Android OnTouchListener 导致 OnClickListener 接收不到消息 无反应
    linux如何查看所有的用户和组信息?
    本地连接属性:Internet协议版本4(TCP/IPv4)打开闪退解决办法
    解决IE11安装时需要“获取更新”(IE11离线安装)
    plsql中文乱码问题(显示问号)
    npm配置国内镜像资源+淘宝镜像
    Node.js安装及环境配置之Windows篇
    Docker报错 WARNING: IPv4 forwarding is disabled. Networking will not work.
    VirtualBox中CentOS7.2 网络配置(固定IP+联网)
  • 原文地址:https://www.cnblogs.com/jinshizuofei/p/4157778.html
Copyright © 2020-2023  润新知