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