예제 #1
0
        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");
        }
예제 #2
0
        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");
        }
예제 #3
0
        private static void UnitTest <TSerializer>(
            TSerializer serializer,
            MemoryStream mem) where TSerializer : ISerializer
        {
            Console.WriteLine($"{DateTime.Now} {typeof(TSerializer).Name}: start testing collection/enum");

            var p1 = new CollectionUnit {
                EnumField = ColorEnum.Green
            };

            p1.NestedSerializable.DoubleField = 0.7662;
            p1.NestedSerializable.FloatField  = 2.335f;
            p1.NestedSerializable.ListField.Add(234);
            p1.NestedSerializable.DictField[789] = "Cool";

            mem.Seek(0, SeekOrigin.Begin);
            serializer.Serialize(p1);
            mem.Seek(0, SeekOrigin.Begin);
            var p2 = serializer.Deserialize <CollectionUnit>();

            Debug.Assert(p2.EnumField == p1.EnumField);
            Debug.Assert(p2.FlagField == p1.FlagField);
            Debug.Assert(!p2.NullableField.HasValue);
            Debug.Assert(p2.EmptyArray.Length == 0);

            Debug.Assert(p1.NestedSerializable.Guid == p2.NestedSerializable.Guid);
            Debug.Assert(p1.NestedSerializable.DoubleField == p2.NestedSerializable.DoubleField);
            Debug.Assert(p1.NestedSerializable.FloatField == p2.NestedSerializable.FloatField);
            Debug.Assert(p1.NestedSerializable.DateTimeField == p2.NestedSerializable.DateTimeField);
            Debug.Assert(p2.NestedSerializable.ListField.Last() == 234);
            Debug.Assert(p2.NestedSerializable.DictField[789] == p1.NestedSerializable.DictField[789]);

            p1.NullableField = 56789;
            p1.ArrayField[0] = "Greatwall";
            p1.DictField.Add("First", new NestedUnit {
                FloatField = 556
            });
            p1.FlagField = FileModeEnum.Read | FileModeEnum.Write;
            mem.Seek(0, SeekOrigin.Begin);
            serializer.Serialize(p1);
            mem.Seek(0, SeekOrigin.Begin);
            p2 = serializer.Deserialize <CollectionUnit>();
            Debug.Assert(p2.ArrayField[0] == p1.ArrayField[0]);
            Debug.Assert(p2.NullableField.Value == 56789);
            Debug.Assert(p2.DictField["First"].FloatField == 556);
            Debug.Assert(p2.FlagField == p1.FlagField);

            p1.Dict1["hello"] = 123;
            p1.Dict2["hello"] = "world";
            p1.Dict3[123]     = "hello";
            p1.Dict4[123]     = 456;
            mem.Seek(0, SeekOrigin.Begin);
            serializer.Serialize(p1);
            mem.Seek(0, SeekOrigin.Begin);
            p2 = serializer.Deserialize <CollectionUnit>();
            Debug.Assert(p2.Dict1["hello"] == 123);
            Debug.Assert(p2.Dict2["hello"] == "world");
            Debug.Assert(p2.Dict3[123] == "hello");
            Debug.Assert(p2.Dict4[123] == 456);

            Console.WriteLine($"{DateTime.Now} {typeof(TSerializer).Name}: end testing collection/enum");
        }