Exemplo n.º 1
0
 public void SerializeToString_TextJson()
 {
     textJsonSerializer.SerializeToString(serializeToStringObject);
 }
Exemplo n.º 2
0
        private void CompareSerializers <T>(T obj, int runs)
        {
            //warm-up

#if NETFULL || NETCOREAPP3_1
            jilserializer.SerializeToString(obj);
            using (MemoryStream mem = new MemoryStream())
            {
                binaryserializer.Serialize(obj, mem);
            }

            using (MemoryStream mem = new MemoryStream())
            {
                datacontractserializer.Serialize(obj, mem);
            }
#endif

#if NETFULL
            using (MemoryStream mem = new MemoryStream())
            {
                soapserializer.Serialize(obj, mem);
            }
#endif

            var netserializer = SerializerFactory.Get("NET");
            using (MemoryStream mem = new MemoryStream())
            {
                netserializer.Serialize(obj, mem);
            }

            jsonnetserializer.SerializeToString(obj);
            textjsonserializer.SerializeToString(obj);

            using (MemoryStream mem = new MemoryStream())
            {
                protobufserializer.Serialize(obj, mem);
            }


            xmlserializer.SerializeToString(obj);

            var keys = serializer.Keys.ToList();

#if NETFULL || NETCOREAPP3_1
            serializer["Jil"].Act = () =>
            {
                GC.Collect(2, GCCollectionMode.Forced, blocking: true);
                serializer["Jil"].Score = Helper.AverageRuntime(() =>
                {
                    jilserializer.SerializeToString(obj);
                }, runs);
            };
            serializer["Binary"].Act = () =>
            {
                GC.Collect(2, GCCollectionMode.Forced, blocking: true);
                serializer["Binary"].Score = Helper.AverageRuntime(() =>
                {
                    using (MemoryStream mem = new MemoryStream())
                    {
                        binaryserializer.Serialize(obj, mem);
                    }
                }, runs);
            };

            serializer["DataContract"].Act = () =>
            {
                GC.Collect(2, GCCollectionMode.Forced, blocking: true);
                serializer["DataContract"].Score = Helper.AverageRuntime(() =>
                {
                    using (MemoryStream mem = new MemoryStream())
                    {
                        datacontractserializer.Serialize(obj, mem);
                    }
                }, runs);
            };
#endif

#if NETFULL
            serializer["Soap"].Act = () =>
            {
                GC.Collect(2, GCCollectionMode.Forced, blocking: true);
                serializer["Soap"].Score = Helper.AverageRuntime(() =>
                {
                    using (MemoryStream mem = new MemoryStream())
                    {
                        soapserializer.Serialize(obj, mem);
                    }
                }, runs);
            };
#endif

            serializer["NET"].Act = () =>
            {
                GC.Collect(2, GCCollectionMode.Forced, blocking: true);
                serializer["NET"].Score = Helper.AverageRuntime(() =>
                {
                    using (MemoryStream mem = new MemoryStream())
                    {
                        netserializer.Serialize(obj, mem);
                    }
                }, runs);
            };

            serializer["Json"].Act = () =>
            {
                GC.Collect(2, GCCollectionMode.Forced, blocking: true);
                serializer["Json"].Score = Helper.AverageRuntime(() =>
                {
                    jsonnetserializer.SerializeToString(obj);
                }, runs);
            };

            serializer["TextJson"].Act = () =>
            {
                GC.Collect(2, GCCollectionMode.Forced, blocking: true);
                serializer["TextJson"].Score = Helper.AverageRuntime(() =>
                {
                    textjsonserializer.SerializeToString(obj);
                }, runs);
            };

            serializer["Xml"].Act = () =>
            {
                GC.Collect(2, GCCollectionMode.Forced, blocking: true);
                serializer["Xml"].Score = Helper.AverageRuntime(() =>
                {
                    xmlserializer.SerializeToString(obj);
                }, runs);
            };

            serializer["Protobuf"].Act = () =>
            {
                GC.Collect(2, GCCollectionMode.Forced, blocking: true);
                serializer["Protobuf"].Score = Helper.AverageRuntime(() =>
                {
                    using (MemoryStream mem = new MemoryStream())
                    {
                        protobufserializer.Serialize(obj, mem);
                    }
                }, runs);
            };

            keys.ForEach(k =>
            {
                serializer[k].Act();
            });
        }