码迷,mamicode.com
首页 > 其他好文 > 详细

请教:WCF速度似乎比Remoting慢

时间:2018-11-26 02:05:41      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:localhost   lan   turn   for   log   get   test   ann   oid   

两段极为相似的代码,主要想看看过输与序列化过程两者的用时差异,结果10000次的调用,WCF用了11秒多,remoting用了5秒不到!
这是测试的源代码 

WCF的服务端

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;
    }
}

Remoting的服务端

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

 

请教:WCF速度似乎比Remoting慢

标签:localhost   lan   turn   for   log   get   test   ann   oid   

原文地址:https://www.cnblogs.com/kevin-Y/p/10018168.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!