static void Main(string[] args) { PrimitiveUnit.UnitTest(); PrimitiveUnit.UnitTest2(); PrimitiveUnit.UnitTestAsync().Wait(); PrimitiveUnit.UnitTest2Async().Wait(); Console.WriteLine($"{DateTime.Now} start generating serialization code for assembly"); //This is not necessary but can accelerate following serializing Serializer.PrepareForAssembly(typeof(NestedUnit).Assembly); Serializer2.PrepareForAssembly(typeof(NestedUnit).Assembly); Console.WriteLine($"{DateTime.Now} end generating serialization code for assembly"); OptionalFieldUnit.UnitTest(); OptionalFieldUnit.UnitTest2(); OptionalFieldUnit.UnitTestAsync().Wait(); OptionalFieldUnit.UnitTest2Async().Wait(); CollectionUnit.UnitTest(); CollectionUnit.UnitTest2(); CollectionUnit.UnitTestAsync().Wait(); CollectionUnit.UnitTest2Async().Wait(); RefUnit.UnitTest(); PerformanceTest(); Console.WriteLine($"{DateTime.Now} Unit test passed"); }
public static void UnitTest() { Console.WriteLine($"{DateTime.Now} Serializer2: start reference testing"); using var mem = new MemoryStream(); var serializer = new Serializer2(mem); var item1 = new RefUnit(); item1.Dict1[1] = item1; item1.List1.Add(item1); item1.Array1[0] = item1; item1.Array2 = item1.Array1; item1.Dict2 = item1.Dict1; item1.List2 = item1.List1; item1.Str2 = item1.Str1; mem.Seek(0, SeekOrigin.Begin); serializer.Serialize(item1); mem.Seek(0, SeekOrigin.Begin); var item2 = serializer.Deserialize <RefUnit>(); Debug.Assert(Object.ReferenceEquals(item2.Dict1[1], item2)); Debug.Assert(Object.ReferenceEquals(item2.List1[0], item2)); Debug.Assert(Object.ReferenceEquals(item2.Array1[0], item2)); Debug.Assert(item2.Array1[1] == null); Debug.Assert(item2.Array1[2] == null); Debug.Assert(Object.ReferenceEquals(item2.Array1, item2.Array2)); Debug.Assert(Object.ReferenceEquals(item2.Dict1, item2.Dict2)); Debug.Assert(Object.ReferenceEquals(item2.List1, item2.List2)); Debug.Assert(Object.ReferenceEquals(item2.Str1, item2.Str2)); Console.WriteLine($"{DateTime.Now} Serializer2: end reference testing"); }
private static void PerformanceTest() { using var mem = new MemoryStream(); var serializer = new Serializer(mem); var p1 = new CollectionUnit(); Console.WriteLine($"{DateTime.Now}.{DateTime.Now.Millisecond} Serializer: start performance testing"); for (var i = 0; i < 100000; ++i) { mem.Seek(0, SeekOrigin.Begin); serializer.Serialize(p1); mem.Seek(0, SeekOrigin.Begin); serializer.Deserialize <CollectionUnit>(); } Console.WriteLine($"{DateTime.Now}.{DateTime.Now.Millisecond} Serializer: end performance testing"); Console.WriteLine($"{DateTime.Now}.{DateTime.Now.Millisecond} Serializer2: start performance testing"); var serializer2 = new Serializer2(mem); for (var i = 0; i < 100000; ++i) { mem.Seek(0, SeekOrigin.Begin); serializer2.Serialize(p1); mem.Seek(0, SeekOrigin.Begin); serializer2.Deserialize <CollectionUnit>(); } Console.WriteLine($"{DateTime.Now}.{DateTime.Now.Millisecond} Serializer2: end performance testing"); Console.WriteLine($"{DateTime.Now} Serializer: start performance comparing"); for (var i = 0; i < 1000000; ++i) { mem.Seek(0, SeekOrigin.Begin); serializer.Serialize(p1.NestedSerializable); mem.Seek(0, SeekOrigin.Begin); serializer.Deserialize <NestedUnit>(); } Console.WriteLine($"{DateTime.Now} Serializer: end performance comparing"); Console.WriteLine($"{DateTime.Now} Serializer2: start performance comparing"); for (var i = 0; i < 1000000; ++i) { mem.Seek(0, SeekOrigin.Begin); serializer2.Serialize(p1.NestedSerializable); mem.Seek(0, SeekOrigin.Begin); serializer2.Deserialize <NestedUnit>(); } Console.WriteLine($"{DateTime.Now} Serializer2: end performance comparing"); Console.WriteLine($"{DateTime.Now} BinaryFormatter: start performance comparing"); var formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); for (var i = 0; i < 1000000; ++i) { mem.Seek(0, SeekOrigin.Begin); formatter.Serialize(mem, p1.NestedSerializable); mem.Seek(0, SeekOrigin.Begin); formatter.Deserialize(mem); } Console.WriteLine($"{DateTime.Now} BinaryFormatter: end performance comparing"); }