Last active
November 14, 2016 07:06
-
-
Save neuecc/9fe6f5e0aaa77114ba99487f8c0c0fc0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void SerializeZeroFormatter() | |
{ | |
var bytes = ZeroFormatter.ZeroFormatterSerializer.Serialize(Value); | |
RunTest("ZeroFormatter", () => | |
{ | |
ZeroFormatter.ZeroFormatterSerializer.Serialize(Value); | |
}, () => | |
{ | |
ZeroFormatter.ZeroFormatterSerializer.Deserialize<T>(bytes); | |
}, bytes.Length); | |
} | |
private void SerializeZeroFormatterWithPooling() | |
{ | |
var bytes = ZeroFormatter.ZeroFormatterSerializer.Serialize(Value); | |
RunTest("ZeroFormatterWithPooling", () => | |
{ | |
ZeroFormatter.ZeroFormatterSerializer.Serialize(ref bytes, 0, Value); | |
}, () => | |
{ | |
ZeroFormatter.ZeroFormatterSerializer.Deserialize<T>(bytes); | |
}, bytes.Length); | |
} | |
private static void Run() | |
{ | |
//var largeStructTest = new LargeStructTest(); | |
//largeStructTest.Run(1000000); | |
//var guidArrayTest = new GuidArrayTest(); | |
//guidArrayTest.Run(30000); | |
//var guidTest = new GuidTest(); | |
//guidTest.Run(1000000); | |
//var typicalPersonArrayTest = new TypicalPersonArrayTest(); | |
//typicalPersonArrayTest.Run(1000); | |
//var typicalPersonTest = new TypicalPersonTest(); | |
//typicalPersonTest.Run(100000); | |
//var typicalMessageArrayTest = new TypicalMessageArrayTest(); | |
//typicalMessageArrayTest.Run(10000); | |
// new TypicalMessage().GuidProp | |
ZeroFormatter.Formatters.Formatter<Guid>.Register(new GuidFormatter()); | |
var typicalMessageTest = new TypicalMessageTest(); | |
typicalMessageTest.Run(1000000); | |
Console.ReadLine(); | |
} | |
protected virtual void TestAll() | |
{ | |
SerializeKnownTypesReuseSession(); | |
SerializeKnownTypes(); | |
SerializeDefault(); | |
//SerializeVersionTolerant(); | |
//SerializeVersionPreserveObjects(); | |
//SerializeNFXSlim(); | |
//SerializeNFXSlimPreregister(); | |
//SerializeSSText(); | |
//SerializeNetSerializer(); | |
SerializeProtoBufNet(); | |
//SerializeJsonNet(); | |
SerializeZeroFormatter(); | |
SerializeZeroFormatterWithPooling(); | |
} | |
[ProtoContract] | |
[Serializable] | |
[ZeroFormattable] | |
public class TypicalMessage | |
{ | |
[ProtoMember(1)] | |
[Index(0)] | |
public virtual string StringProp { get; set; } | |
[ProtoMember(2)] | |
[Index(1)] | |
public virtual int IntProp { get; set; } | |
[ProtoMember(3)] | |
[Index(2)] | |
public virtual Guid GuidProp { get; set; } | |
[ProtoMember(4)] | |
[Index(3)] | |
public virtual DateTime DateProp { get; set; } | |
} | |
public class GuidFormatter : Formatter<Guid> | |
{ | |
public override int? GetLength() | |
{ | |
// If size is fixed, return fixed size. | |
return 16; | |
} | |
public override int Serialize(ref byte[] bytes, int offset, Guid value) | |
{ | |
// BinaryUtil is helpers of byte[] operation | |
return BinaryUtil.WriteBytes(ref bytes, offset, value.ToByteArray()); | |
} | |
public override Guid Deserialize(ref byte[] bytes, int offset, DirtyTracker tracker, out int byteSize) | |
{ | |
byteSize = 16; | |
var guidBytes = BinaryUtil.ReadBytes(ref bytes, offset, 16); | |
return new Guid(guidBytes); | |
} | |
} |
Author
neuecc
commented
Nov 14, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment