zoukankan      html  css  js  c++  java
  • 请教: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();
    }

     不过,有意思的是,我在CalcInfo增加一个集合属性Item,放入100个实例时,情况就完全反过来了。WCF完胜。至此,我感觉在小型的类序列化时,WCF体现不出优势,只有在传输的内容比较多的时候才有优势。
    修改后的测试代码 

    以下是修改后的CalcInfo类

    [DataContract]
    [Serializable]
    public class CalcInfo
    {
        public CalcInfo()
        {
            this.Items = new List<CalcItem>(500);
        }
        [DataMember]
        public string Method { get; set; }
        [DataMember]
        public decimal Para1 { get; set; }
        [DataMember]
        public decimal Para2 { get; set; }
        [DataMember]
        public decimal Result { get; set; }
        [DataMember]
        public List<CalcItem> Items { get; set; }
    
        public static CalcInfo BuildTest(int itemMax)
        {
            CalcInfo result = new CalcInfo();
            for(int i=0;i< itemMax; i++)
            {
                CalcItem item = new CalcItem();
                item.Name = "test item name is " + i;
                item.Para1 = i;
                item.Para2 = i * 100;
                result.Items.Add(item);
            }
            return result;
        }
    }
  • 相关阅读:
    TinySpider开源喽
    TinyXmlParser开源喽
    Tiny快速入门之控制层开发
    TinyDBRouter
    TinyIOC
    开源前要做好哪些准备工作?
    分布式锁的简单实现
    TinyDBCluster Vs routing4db
    HTML5基础(五)
    HTML5基础(四)
  • 原文地址:https://www.cnblogs.com/kevin-Y/p/10018168.html
Copyright © 2011-2022 走看看