English 中文(简体)
在世界可可交易所的推进剂
原标题:Performance of protobuf-net vs DataContractSerializer over WCF

I tested protobuf serialization and it seems that for below a certain quantity of objects, it s slower than regular datacontract serialization. The transmission size is bigger using DataContractSerializer but during serialization and deserialization it is faster to use DataContractSerializer

Do you think this is normal or did I made a mistake?

[DataContract]
public partial class Toto
{
    [DataMember]
    public string NomToto { get; set; }

    [DataMember]
    public string PrenomToto { get; set; }
} 

这里是我的数据合同类别,对代用制来说,这是一样的。

[ProtoContract]
public partial class Titi
{
    [ProtoMember(1)]
    public string NomTiti { get; set; }

    [ProtoMember(2)]
    public string PrenomTiti { get; set; }
}

这里是我与代用公司(数据合同与手法相仿)一道使用WCF服务的方法。

public class TitiService : ITitiService
{
    public byte[] GetAllTitis()
    {
        List<Titi> titiList = new List<Titi>();
        for (int i = 0; i < 20000; i++)
        {
            var titi = new Titi
            {
                NomTiti = "NomTiti" + i,
                PrenomTiti = "PrenomTiti" + i
            };
            titiList.Add(titi);
        }
        var ms = new MemoryStream();
        Serializer.Serialize(ms, titiList);

        byte[] arr = ms.ToArray();
        return arr;
    }
}

签订数据合同的服务

public class TotoService : ITotoService
{
    public List<Toto> GetAllTotos()
    {
        List<Toto> totoList = new List<Toto>();
        for (int i = 0; i<20000; i++)
        {
            var toto = new Toto
            {
                NomToto = "NomToto" + i,
                PrenomToto = "PrenomToto" + i
            };
            totoList.Add(toto);
        }
        return totoList;
    }
}

这里的客户呼吁

    public partial class Program
{
    static ProtobufTestAzure.Client.TitiService.TitiServiceClient TitiClient;
    static ProtobufTestAzure.Client.TotoService.TotoServiceClient TotoClient;

    public static void Main(string[] args)
    {
        Stopwatch stopwatch1 = new Stopwatch();
        Stopwatch stopwatch2 = new Stopwatch();
        Stopwatch stopwatch3 = new Stopwatch();

        stopwatch1.Start();

        TitiClient = new ProtobufTestAzure.Client.TitiService.TitiServiceClient();
        Byte[] titiByte = TitiClient.GetAllTitis();
        TitiClient.Close();

        stopwatch1.Stop();


        stopwatch2.Start();

        var ms = new MemoryStream(titiByte);
        List<Titi> TitiList = Serializer.Deserialize<List<Titi>>(ms);

        stopwatch2.Stop();

        Console.WriteLine(" ");

        stopwatch3.Start();

        TotoClient = new ProtobufTestAzure.Client.TotoService.TotoServiceClient();
        var TotoList = TotoClient.GetAllTotos();
        TotoClient.Close();

        stopwatch3.Stop();

        Console.WriteLine("Time elapse for reception (Protobuf): {0} ms ({1} éléments)", stopwatch1.ElapsedMilliseconds, TitiList.Count);
        Console.WriteLine("Time elapse for deserialization (Protobuf : {0} ms ({1} éléments)", stopwatch2.ElapsedMilliseconds, TitiList.Count);
        Console.WriteLine("Time elapse for réception (Datacontract Serialization) : {0} ms ({1} éléments)", stopwatch3.ElapsedMilliseconds, TotoList.Count);

        Console.ReadLine();
    }
}

10 000件物体

Time elapse for reception (Protobuf): 3359 ms (10000 elements) Time elapse for deserialization (Protobuf): 138 ms (10000 elements) Time elapse for reception (Datacontract Serialization): 2200ms (10000 elements)

I test it whith 20000 objects It gave me for the first call

Time elapse for reception (Protobuf): 11258ms (20000 elements) Time elapse for deserialization (Protobuf): 133ms (20000 elements) Time elapse for reception (Datacontract Serialization): 3726ms (20000 elements)

第二次呼吁

Time elapse for reception (Protobuf): 2844 ms (20000 elements) Time elapse for deserialization (Protobuf): 141 ms (20000 elements) Time elapse for reception (Datacontract Serialization): 7541 ms (20000 elements)

2008年7月1日至2008年6月30日期间预算执行情况报告

Time elapse for reception (Protobuf): 2767ms (20000 elements) Time elapse for deserialization (Protobuf): 145 ms (20000 elements) Time elapse for reception (Datacontract Serialization): 3989 ms (20000 elements)

在Protobuf转让的MTOM启动之后,它给了我:

第一电话

Time elapse for reception (Protobuf): 3316 ms (20000 elements) Time elapse for deserialization (Protobuf): 63 ms (20000 elements) Time elapse for reception (Datacontract Serialization): 3769 ms (20000 elements)

第二电话

Time elapse for reception (Protobuf): 2279 ms (20000 elements) Time elapse for deserialization (Protobuf): 57 ms (20000 elements) Time elapse for reception (Datacontract Serialization): 3959 ms (20000 elements)

添加此部分的物体大小代码。

            long totoSize = new long();
        using (Stream s = new MemoryStream())
        {
            BinaryFormatter formatter = new BinaryFormatter();
            formatter.Serialize(s, totoList);
            totoSize = s.Length;
        }

        long titiSize = titiByte.Count();

it gave me 637780 with protobuf and 1038236 with DataContractSerializer Durations for call are better and more stable this morning first call protobuf = 2498 ms datacontract = 5085 ms

second call protobuf = 3649 ms datacontract = 3840 ms

third call protobuf = 2498 ms datacontract = 5085 ms

最佳回答

影响业绩的一些因素:

  • is the serializer prepared? This is automatic on the first use per-type; the first time through, it needs to do quite a bit of inspection etc to figure out how your model works. You can offset this by calling Serializer.PrepareSerializer<YourType>() somewhere during startup
    • or as an alternative, in v2 (available as "alpha") you can pre-generate the serializer as a dll if you need the fastest possible cold-start performance
  • what is the transport? in particular with WCF, you need to keep in mind how your byte[] is encoded (this isn t a problem on sockets, of course); for example, can the transport use MTOM? or is it base-64 encoding the byte[]?
    • and note also that it is possible that Stream and byte[] are handled differently; if you can measure the bandwidth, you might want to try both
    • basic-http with MTOM enabled is my preference for WCF transports if absolute speed is your aim; or sockets if you want to get closer to the limit
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签