請教:WCF速度彷佛比Remoting慢

兩段極爲類似的代碼,主要想看看過輸與序列化過程二者的用時差別,結果10000次的調用,WCF用了11秒多,remoting用了5秒不到!
這是測試的源代碼 tcp

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的服務端ui

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客戶端this

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客戶端spa

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體現不出優點,只有在傳輸的內容比較多的時候纔有優點。
修改後的測試代碼 
code

如下是修改後的CalcInfo類orm

[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;
    }
}
相關文章
相關標籤/搜索