示例#1
0
            protected T Serialize <Parser, T>(TypeRegistry r
                                              , IFormatter f
                                              , Func <ArraySegment <Byte>, Parser> parser, T original)
                where Parser : IParser <Parser>
            {
                T   copy   = default(T);
                var packed = default(ArraySegment <Byte>);

                // Note:We should check MessagePackSerializer.Get<T>() on every iteration
                // But currenly MsgPack-Cli has bug of get serializer
                // https://github.com/msgpack/msgpack-cli/issues/191
                // so, get serializer at first.
                // and If enum serialization options to ByUnderlyingValue, gets more fast but we check default option only.

                var serializer   = (SerializerBase <T>)r.GetSerializer <T>();
                var deserializer = r.GetDeserializer <T>();

                using (new Measure("Serialize"))
                {
                    for (int i = 0; i < Iteration; i++)
                    {
                        f.Clear();
                        serializer.Serialize(original, f);
                        packed = f.GetStore().Bytes;
                    }
                }

                using (new Measure("Deserialize"))
                {
                    for (int i = 0; i < Iteration; i++)
                    {
                        //copy = Osaru.MessagePack.Deserializer.Deserialize<T>(bytes);
                        deserializer.Deserialize(parser(packed), ref copy);
                    }
                }

                Assert.AreEqual(original, copy);

                using (new Measure("ReSerialize"))
                {
                    for (int i = 0; i < Iteration; i++)
                    {
                        f.Clear();
                        serializer.Serialize(copy, f);
                        packed = f.GetStore().Bytes;
                    }
                }

                if (!dryRun)
                {
                    //Console.WriteLine(string.Format("{0,15}   {1}", "Binary Size", ToHumanReadableSize(bytes.Length)));
                }

                return(copy);
            }