Beispiel #1
0
        private static void Main()
        {
            var dataSchema = new SampleSchema(
                0,
                SampleEnum.Value1,
                "sample text",
                Encoding.UTF8.GetBytes("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce porta eu velit ac molestie. Suspendisse viverra nulla ut nibh commodo luctus. Donec nec posuere dui. Sed ac nunc bibendum ante ornare eleifend vel sed ligula. Nam sed augue venenatis, elementum velit hendrerit, cursus lacus. Aenean finibus pretium mauris sit amet mollis. Nullam ullamcorper hendrerit magna, interdum lacinia metus interdum sed.")
                );

            Console.WriteLine("Running BitSerializer benchmark...");
            RunBenchmark_BitSerializer(dataSchema);
            Console.WriteLine();

            Console.WriteLine("Running Newtonsoft.Json benchmark...");
            RunBenchmark_NewtonsoftJson(dataSchema);
            Console.WriteLine();

            Console.WriteLine("Running BinaryFormatter benchmark...");
            RunBenchmark_BinaryFormatter(dataSchema);
            Console.WriteLine();

            Console.WriteLine("Benchamarking finished.");

            Console.ReadKey();
        }
Beispiel #2
0
        private static void RunBenchmark_BinaryFormatter(SampleSchema dataSchema)
        {
            var  totalSw = Stopwatch.StartNew();
            long len     = 0;

            using (var stream = new MemoryStream())
            {
                double lowestSerializationTime   = double.MaxValue;
                double lowestDeserializationTime = double.MaxValue;

                // Serialization
                for (int i = 0; i < Iterations; i++)
                {
                    dataSchema.SampleInt = i;

                    var sw = Stopwatch.StartNew();
                    new BinaryFormatter().Serialize(stream, dataSchema);
                    sw.Stop();

                    len             = stream.Position;
                    stream.Position = 0;

                    if (sw.Elapsed.TotalMilliseconds < lowestSerializationTime)
                    {
                        lowestSerializationTime = sw.Elapsed.TotalMilliseconds;
                    }
                }

                // Deserialization
                for (int i = 0; i < Iterations; i++)
                {
                    var sw = Stopwatch.StartNew();
                    new BinaryFormatter().Deserialize(stream);
                    sw.Stop();

                    stream.Position = 0;

                    if (sw.Elapsed.TotalMilliseconds < lowestDeserializationTime)
                    {
                        lowestDeserializationTime = sw.Elapsed.TotalMilliseconds;
                    }
                }
                totalSw.Stop();

                PrintResults("BinaryFormatter (Serialization)", lowestSerializationTime);
                PrintResults("BinaryFormatter (Bytes)", len, false);
                PrintResults("BinaryFormatter (Deserialization)", lowestDeserializationTime);
                PrintResults("BinaryFormatter (Total)", totalSw.Elapsed.TotalMilliseconds);
            }
        }
Beispiel #3
0
        private static void RunBenchmark_BitSerializer(SampleSchema dataSchema)
        {
            var totalSw = Stopwatch.StartNew();

            byte[] serialized = null;
            double lowestSerializationTime   = double.MaxValue;
            double lowestDeserializationTime = double.MaxValue;

            // Serialization
            for (int i = 0; i < Iterations; i++)
            {
                dataSchema.SampleInt = i;

                var sw = Stopwatch.StartNew();
                serialized = BinarySerializer.Serialize(dataSchema);
                sw.Stop();

                if (sw.Elapsed.TotalMilliseconds < lowestSerializationTime)
                {
                    lowestSerializationTime = sw.Elapsed.TotalMilliseconds;
                }
            }

            // Deserialization
            for (int i = 0; i < Iterations; i++)
            {
                var sw = Stopwatch.StartNew();
                BinarySerializer.Deserialize <SampleSchema>(serialized);
                sw.Stop();

                if (sw.Elapsed.TotalMilliseconds < lowestDeserializationTime)
                {
                    lowestDeserializationTime = sw.Elapsed.TotalMilliseconds;
                }
            }
            totalSw.Stop();

            PrintResults("BitSerializer (Serialization)", lowestSerializationTime);
            PrintResults("BinarySerializer (Bytes)", serialized.Length, false);
            PrintResults("BitSerializer (Deserialization)", lowestDeserializationTime);
            PrintResults("BinarySerializer (Total)", totalSw.Elapsed.TotalMilliseconds);
        }
Beispiel #4
0
        private static void RunBenchmark_NewtonsoftJson(SampleSchema dataSchema)
        {
            var    totalSw    = Stopwatch.StartNew();
            string serialized = null;
            double lowestSerializationTime   = double.MaxValue;
            double lowestDeserializationTime = double.MaxValue;

            // Serialization
            for (int i = 0; i < Iterations; i++)
            {
                dataSchema.SampleInt = i;

                var sw = Stopwatch.StartNew();
                serialized = JsonConvert.SerializeObject(dataSchema);
                sw.Stop();

                if (sw.Elapsed.TotalMilliseconds < lowestSerializationTime)
                {
                    lowestSerializationTime = sw.Elapsed.TotalMilliseconds;
                }
            }

            // Deserialization
            for (int i = 0; i < Iterations; i++)
            {
                var sw = Stopwatch.StartNew();
                JsonConvert.DeserializeObject <SampleSchema>(serialized);
                sw.Stop();

                if (sw.Elapsed.TotalMilliseconds < lowestDeserializationTime)
                {
                    lowestDeserializationTime = sw.Elapsed.TotalMilliseconds;
                }
            }
            totalSw.Stop();

            PrintResults("Newtonsoft.Json (Serialization)", lowestSerializationTime);
            PrintResults("Newtonsoft.Json (Bytes)", serialized.Length, false);
            PrintResults("Newtonsoft.Json (Deserialization)", lowestDeserializationTime);
            PrintResults("Newtonsoft.Json (Total)", totalSw.Elapsed.TotalMilliseconds);
        }