欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

请教:WCF速度似乎比Remoting慢

程序员文章站 2022-05-25 17:16:09
两段极为相似的代码,主要想看看过输与序列化过程两者的用时差异,结果10000次的调用,WCF用了11秒多,remoting用了5秒不到!这是测试的源代码 Remoting的服务端 WCF的服务端 代码可以理解为一样的,以下的客户端,两客户端也可以视为一样的过程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();
}