我想弄清楚用protobuf-net序列化的文件的最终大小,所以我可以选择最好的方法。如何计算protobuf文件大小?
我做了不同的原型配置和二进制序列化的比较测试,但我仍然不明白“varint to bytes”转换的工作原理。
类
public class Pt2D
{
public Pt2D() { }
public Pt2D(double x, double y)
{
X = x;
Y = y;
}
public double X { get; set; }
public double Y { get; set; }
}
public class Pt3D : Pt2D
{
public Pt3D() { }
public Pt3D(double x, double y, double z) : base(x, y)
{
Z = z;
}
public double Z { get; set; }
}
public class FullPt3D
{
public FullPt3D() { }
public FullPt3D(double x, double y, double z)
{
X = x;
Y = y;
Z = z;
}
public double X { get; set; }
public double Y { get; set; }
public double Z { get; set; }
}
测试用例
private void ProtoBufferTest()
{
var model = RuntimeTypeModel.Default;
model.Add(typeof(Pt2D), false)
.Add(1, "X")
.Add(2, "Y")
.AddSubType(101, typeof(Pt3D));
model[typeof(Pt3D)]
.Add(1, "Z");
model.Add(typeof(FullPt3D), false)
.Add(1, "X")
.Add(2, "Y")
.Add(3, "Z");
double x = 5.6050692524784562;
double y = 0.74161805247031987;
double z = 8.5883424750474937;
string filename = "testPt3D.pb";
using (var file = File.Create(filename))
{
Serializer.Serialize(file, new Pt3D(x, y, z));
}
Console.WriteLine(filename + " length = " + new FileInfo(filename).Length + " bytes") ;
filename = "testFullPt3D.pb";
using (var file = File.Create(filename))
{
Serializer.Serialize(file, new FullPt3D(x, y, z));
}
Console.WriteLine(filename + " length = " + new FileInfo(filename).Length + " bytes");
filename = "testBinaryWriter.bin";
using (var file = File.Create(filename))
{
using (var writer = new BinaryWriter(file))
{
writer.Write(x);
writer.Write(y);
writer.Write(z);
}
}
Console.WriteLine(filename + " length = " + new FileInfo(filename).Length + " bytes");
}
测试结果
1)testPt3D.pb长度= 30个字节
2)testFullPt3D.pb长度= 27个字节
3)testBinaryWriter.bin长度= 24个字节
Q1) 24个字节被用于存储3个值和它的确定,但什么值被存储在例1)和2),以达到30首27个字节? (我想在模型映射中使用INT值)
Q2)我通过改变Pt2D亚型映射进行了一些测试,但我不理解的大小改变
model.Add(typeof(Pt2D), false)
.Add(1, "X")
.Add(2, "Y")
.AddSubType(3, typeof(Pt3D));
结果:testPt3D.pb长度= 29个字节
model.Add(typeof(Pt2D), false)
.Add(1, "X")
.Add(2, "Y")
.AddSubType(21, typeof(Pt3D));
结果:testPt3D.pb长度= 30个字节
model.Add(typeof(Pt2D), false)
.Add(1, "X")
.Add(2, "Y")
.AddSubType(1111, typeof(Pt3D));
结果:testPt3D.pb长度= 30字节
我试图用this tool来更好地理解,但它给出了不同的字节转换结果。
为什么使用21,101或1111可以获得相同的尺寸?
还有这方面的线索吗? :( – ilCosmico