Performant Entity Serialization: BSON vs MessagePack (vs JSON)

后端 未结 6 1444
南方客
南方客 2020-12-04 04:57

Recently I\'ve found MessagePack, an alternative binary serialization format to Google\'s Protocol Buffers and JSON which also out

6条回答
  •  有刺的猬
    2020-12-04 05:28

    I made quick benchmark to compare encoding and decoding speed of MessagePack vs BSON. BSON is faster at least if you have large binary arrays:

    BSON writer: 2296 ms (243487 bytes)
    BSON reader: 435 ms
    MESSAGEPACK writer: 5472 ms (243510 bytes)
    MESSAGEPACK reader: 1364 ms
    

    Using C# Newtonsoft.Json and MessagePack by neuecc:

        public class TestData
        {
            public byte[] buffer;
            public bool foobar;
            public int x, y, w, h;
        }
    
        static void Main(string[] args)
        {
            try
            {
                int loop = 10000;
    
                var buffer = new TestData();
                TestData data2;
                byte[] data = null;
                int val = 0, val2 = 0, val3 = 0;
    
                buffer.buffer = new byte[243432];
    
                var sw = new Stopwatch();
    
                sw.Start();
                for (int i = 0; i < loop; i++)
                {
                    data = SerializeBson(buffer);
                    val2 = data.Length;
                }
    
                var rc1 = sw.ElapsedMilliseconds;
    
                sw.Restart();
                for (int i = 0; i < loop; i++)
                {
                    data2 = DeserializeBson(data);
                    val += data2.buffer[0];
                }
                var rc2 = sw.ElapsedMilliseconds;
    
                sw.Restart();
                for (int i = 0; i < loop; i++)
                {
                    data = SerializeMP(buffer);
                    val3 = data.Length;
                    val += data[0];
                }
    
                var rc3 = sw.ElapsedMilliseconds;
    
                sw.Restart();
                for (int i = 0; i < loop; i++)
                {
                    data2 = DeserializeMP(data);
                    val += data2.buffer[0];
                }
                var rc4 = sw.ElapsedMilliseconds;
    
                Console.WriteLine("Results:", val);
                Console.WriteLine("BSON writer: {0} ms ({1} bytes)", rc1, val2);
                Console.WriteLine("BSON reader: {0} ms", rc2);
                Console.WriteLine("MESSAGEPACK writer: {0} ms ({1} bytes)", rc3, val3);
                Console.WriteLine("MESSAGEPACK reader: {0} ms", rc4);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
    
            Console.ReadLine();
        }
    
        static private byte[] SerializeBson(TestData data)
        {
            var ms = new MemoryStream();
    
            using (var writer = new Newtonsoft.Json.Bson.BsonWriter(ms))
            {
                var s = new Newtonsoft.Json.JsonSerializer();
                s.Serialize(writer, data);
                return ms.ToArray();
            }
        }
    
        static private TestData DeserializeBson(byte[] data)
        {
            var ms = new MemoryStream(data);
    
            using (var reader = new Newtonsoft.Json.Bson.BsonReader(ms))
            {
                var s = new Newtonsoft.Json.JsonSerializer();
                return s.Deserialize(reader);
            }
        }
    
        static private byte[] SerializeMP(TestData data)
        {
            return MessagePackSerializer.Typeless.Serialize(data);
        }
    
        static private TestData DeserializeMP(byte[] data)
        {
            return (TestData)MessagePackSerializer.Typeless.Deserialize(data);
        }
    

提交回复
热议问题