测试结果:
ProtoBuf Length:115
BinaryFormatter Length:1177
XmlSerializer Length:814
xml length:825
做了一个各种序列化方案的压缩比例测试,可以看到protobuf序列化后的大小是xml原始格式的8分之一,是xml序列化后的8分之一,是二进制序列化的10分之一,总体看来ProtoBuf的优势还是很明显的,不过ProtoBuf.net不是google官方提供的,也许和其它平台不兼容,但如果做.NET服务端应用,两边都是.NET,还是可以适用的,即使有一边不是.NET,反正是开源的东西,协议也有,也可以自己实现相应语言的兼容ProtoBuf.net的协议栈。
SOAPFormatter没有测试,一般用的不多。还有就是怪事了,为什么二进制序列化反而大呢,奇怪了。
本次测试主要考虑协议的压缩率的比较,不考虑序列化/解序列化的速度,官方声明比XML解析要快几十倍,有空看下它的实现代码,我的SVN老下载不下来code.google的代码,汗了。
测试代码如下
{
private static void Main(string[] args)
{
MemoryStream ms = null;
Customer customer = Customer.GetOneCustomer();
using (ms = new MemoryStream())
{
Serializer.Serialize(ms, customer);
Console.WriteLine("ProtoBuf Length:{0}", ms.Length);
}
using (ms = new MemoryStream())
{
var formater = new BinaryFormatter();
formater.Serialize(ms, customer);
Console.WriteLine("BinaryFormatter Length:{0}", ms.Length);
}
using (ms = new MemoryStream())
{
var serializer = new XmlSerializer(typeof (Customer));
serializer.Serialize(ms, customer);
Console.WriteLine("XmlSerializer Length:{0}", ms.Length);
}
string xml =
@"<?xml version=""1.0"" ?>
<Customer xmlns=""urn:Sep2003Example"">
<CustomerID>ALFKI</CustomerID>
<PO>9572658</PO>
<Address>
<Street>One Main Street</Street>
<City>Anywhere</City>
<State>NJ</State>
<Zip>08080</Zip>
</Address>
<Order>
<OrderID>10966</OrderID >
<LineItem>
<ProductID>37</ProductID>
<UnitPrice>26.50 </UnitPrice>
<Quantity>8</Quantity>
<Description>Gravad lax </Description>
</LineItem>
<LineItem>
<ProductID>56 </ProductID>
<UnitPrice>38.00</UnitPrice>
<Quantity>12</Quantity>
<Description>Gnocchi di nonna Alice</Description>
</LineItem>
</Order>
</Customer>";
Console.WriteLine("xml length:{0}", Encoding.UTF8.GetByteCount(xml));
Console.ReadKey();
}
}
相关数据结构如下
[Serializable]
public class Customer {
[ProtoMember(1)]
public string CustomerID { get; set; }
[ProtoMember(2)]
public int PO { get; set; }
[ProtoMember(3)]
public Address Address { get; set; }
[ProtoMember(4)]
public Order Order { get; set; }
public static Customer GetOneCustomer() {
Customer customer = new Customer {
CustomerID = "ALFKI",
PO = 9572658,
Address = new Address {
Street = "One Main Street",
City = "Anywhere",
State = "NJ",
Zip = 08080
},
Order = new Order {
OrderID = 10966,
LineItems = new List<LineItem>
{
new LineItem
{
ProductID = 37,
UnitPrice = 26.50M,
Quantity =8,
Description ="Gravad lax"
},
new LineItem
{
ProductID = 56,
UnitPrice = 38.00M,
Quantity =12,
Description ="Gnocchi di nonna Alice"
}
}
}
};
return customer;
}
}
[ProtoContract]
[Serializable]
public class Address {
[ProtoMember(1)]
public string Street { get; set; }
[ProtoMember(2)]
public string City { get; set; }
[ProtoMember(3)]
public string State { get; set; }
[ProtoMember(4)]
public int Zip { get; set; }
}
[ProtoContract]
[Serializable]
public class Order {
[ProtoMember(1)]
public int OrderID { get; set; }
[ProtoMember(2)]
public List<LineItem> LineItems { get; set; }
}
[ProtoContract]
[Serializable]
public class LineItem {
[ProtoMember(1)]
public int ProductID { get; set; }
[ProtoMember(2)]
public decimal UnitPrice { get; set; }
[ProtoMember(3)]
public int Quantity { get; set; }
[ProtoMember(4)]
public string Description { get; set; }
}
相关链接
Protocol Buffers 性能测试
http://hellobmw.com/archives/protocol-buffers-performance.html
Windows Communication Protocols (MCPP)
http://msdn.microsoft.com/en-us/library/cc216513(PROT.10).aspx
浅谈如何使用.NET存储XML数据
http://developer.51cto.com/art/200905/122238.htm
.net下二进制序列化的格式分析[转]
http://www.cnblogs.com/lxinxuan/archive/2006/09/06/496340.html
Protocol Buffers Encoding
http://code.google.com/intl/zh-CN/apis/protocolbuffers/docs/encoding.html