Ejemplo n.º 1
0
 public SerTools()
 {
     Serializer   = new SerializersTable();
     Deserializer = new DeserializersTable();
     SerStream    = new RecycleMemoryStream();
     ComStream    = new RecycleMemoryStream();
 }
Ejemplo n.º 2
0
        private void RunTestEx(object value, int times, ICompressor compressor)
        {
            var vType = value?.GetType() ?? typeof(object);
            //var memStream = new MemoryStream();
            var memStream = new RecycleMemoryStream();
            //var jsonSerializer = new JsonTextSerializer { Compressor = compressor };
            //var ut8JsonSerializer = new Utf8JsonTextSerializer { Compressor = compressor };
            var nBinarySerializer = new NBinarySerializer {
                Compressor = compressor
            };
            var rawBinarySerializer = new RawBinarySerializer {
                Compressor = compressor
            };

            Core.Log.Warning("Running Serializer Test. Use Compressor = {0}", compressor?.EncodingType ?? "(no)");
            Core.Log.WriteEmptyLine();
            Core.Log.InfoBasic("By size:");
            //Core.Log.InfoBasic("\tNewtonsoft Bytes Count: {0}", SerializerSizeProcess(value, vType, jsonSerializer));
            //Core.Log.InfoBasic("\tUTF8Json Bytes Count: {0}", SerializerSizeProcess(value, vType, ut8JsonSerializer));
            Core.Log.InfoBasic("\tNBinary Bytes Count: {0}", SerializerSizeProcess(value, vType, nBinarySerializer));
            Core.Log.InfoBasic("\tRawBinary Bytes Count: {0}", SerializerSizeProcess(value, vType, rawBinarySerializer));
            Core.Log.WriteEmptyLine();
            Console.ReadLine();
            Core.Log.InfoBasic("By Times: {0}", times);
            //SerializerProcess("Json", value, vType, times, jsonSerializer, memStream);
            //SerializerProcess("UTF8Json", value, vType, times, ut8JsonSerializer, memStream);
            SerializerProcess("NBinary", value, vType, times, nBinarySerializer, memStream);
            SerializerProcess("RawBinary", value, vType, times, rawBinarySerializer, memStream);
            Console.ReadLine();
        }
Ejemplo n.º 3
0
 public static MultiArray <byte> ReadAllBytes(this Stream stream)
 {
     using (var ms = new RecycleMemoryStream())
     {
         stream.CopyTo(ms);
         return(ms.GetMultiArray());
     }
 }
Ejemplo n.º 4
0
        public static async Task <MultiArray <byte> > ReadAllBytesAsync(this Stream stream, int bufferSize, CancellationToken cancellationToken)
        {
            using (var ms = new RecycleMemoryStream())
            {
                await stream.CopyToAsync(ms, bufferSize, cancellationToken).ConfigureAwait(false);

                return(ms.GetMultiArray());
            }
        }
Ejemplo n.º 5
0
        public static async Task <MultiArray <byte> > ReadAllBytesAsync(this Stream stream)
        {
            using (var ms = new RecycleMemoryStream())
            {
                await stream.CopyToAsync(ms).ConfigureAwait(false);

                return(ms.GetMultiArray());
            }
        }
Ejemplo n.º 6
0
        private void RunTest(object value, int times, bool useGZip)
        {
            var vType          = value?.GetType() ?? typeof(object);
            var compressor     = useGZip ? CompressorManager.GetByEncodingType("gzip") : null;
            var memStream      = new RecycleMemoryStream();
            var jsonSerializer = new JsonTextSerializer {
                Compressor = compressor
            };
            var xmlSerializer = new XmlTextSerializer {
                Compressor = compressor
            };
            var binarySerializer = new BinaryFormatterSerializer {
                Compressor = compressor
            };
            var ut8JsonSerializer = new Utf8JsonTextSerializer {
                Compressor = compressor
            };
            var msgPackSerializer = new MsgPackSerializer {
                Compressor = compressor
            };
            var nBinarySerializer = new NBinarySerializer {
                Compressor = compressor
            };
            var rawBinarySerializer = new RawBinarySerializer {
                Compressor = compressor
            };
            var wBinarySerializer = new WBinarySerializer {
                Compressor = compressor
            };
            var pwBinarySerializer = new PWBinarySerializer {
                Compressor = compressor
            };

            Core.Log.Warning("Running Serializer Test. Use GZIP = {0}", useGZip);
            Core.Log.WriteEmptyLine();
            Core.Log.InfoBasic("By size:");
            Core.Log.InfoBasic("\tJson Bytes Count: {0}", SerializerSizeProcess(value, vType, jsonSerializer));
            Core.Log.InfoBasic("\tXml Bytes Count: {0}", SerializerSizeProcess(value, vType, xmlSerializer));
            Core.Log.InfoBasic("\tBinaryFormatter Bytes Count: {0}", SerializerSizeProcess(value, vType, binarySerializer));
            Core.Log.InfoBasic("\tUtf8Json Bytes Count: {0}", SerializerSizeProcess(value, vType, ut8JsonSerializer));
            Core.Log.InfoBasic("\tMessagePack Bytes Count: {0}", SerializerSizeProcess(value, vType, msgPackSerializer));
            Core.Log.InfoBasic("\tNBinary Bytes Count: {0}", SerializerSizeProcess(value, vType, nBinarySerializer));
            Core.Log.InfoBasic("\tRawBinary Bytes Count: {0}", SerializerSizeProcess(value, vType, rawBinarySerializer));
            Core.Log.InfoBasic("\tWBinary Bytes Count: {0}", SerializerSizeProcess(value, vType, wBinarySerializer));
            Core.Log.InfoBasic("\tPortable WBinary Bytes Count: {0}", SerializerSizeProcess(value, vType, pwBinarySerializer));
            Core.Log.WriteEmptyLine();
            Core.Log.InfoBasic("By Times: {0}", times);
            SerializerProcess("Json", value, vType, times, jsonSerializer, memStream);
            SerializerProcess("Utf8Json", value, vType, times, ut8JsonSerializer, memStream);
            SerializerProcess("NBinary", value, vType, times, nBinarySerializer, memStream);
            SerializerProcess("RawBinary", value, vType, times, rawBinarySerializer, memStream);
            SerializerProcess("WBinary", value, vType, times, wBinarySerializer, memStream);
            SerializerProcess("PWBinary", value, vType, times, pwBinarySerializer, memStream);
            Console.ReadLine();
        }
Ejemplo n.º 7
0
        private void RunSingleTest(object value, int times, bool useGZip)
        {
            var vType             = value?.GetType() ?? typeof(object);
            var memStream         = new RecycleMemoryStream();
            var compressor        = useGZip ? CompressorManager.GetByEncodingType("gzip") : null;
            var nBinarySerializer = new NBinarySerializer {
                Compressor = compressor
            };

            Core.Log.Warning("Running Serializer Test. Use GZIP = {0}, Times = {1}", useGZip, times);
            SerializerProcess("NBinary", value, vType, times, nBinarySerializer, memStream, false);
        }
Ejemplo n.º 8
0
        public static async Task <MultiArray <byte> > ReadAllBytesAsync(this Stream stream, int bufferSize, int timeout)
        {
            var cts = new CancellationTokenSource();

            using (var ms = new RecycleMemoryStream())
            {
                cts.CancelAfter(timeout);
                await stream.CopyToAsync(ms, bufferSize, cts.Token).ConfigureAwait(false);

                return(ms.GetMultiArray());
            }
        }
Ejemplo n.º 9
0
 public object Deserialize(Stream stream, Type itemType)
 {
     if (Compressor == null)
     {
         return(OnDeserialize(stream, itemType));
     }
     using (var ms = new RecycleMemoryStream())
     {
         Compressor.Decompress(stream, ms);
         ms.Position = 0;
         var res = OnDeserialize(ms, itemType);
         return(res);
     }
 }
Ejemplo n.º 10
0
 public void Serialize(object item, Type itemType, Stream stream)
 {
     if (Compressor == null)
     {
         OnSerialize(stream, item, itemType);
         return;
     }
     using (var ms = new RecycleMemoryStream())
     {
         OnSerialize(ms, item, itemType);
         ms.Position = 0;
         Compressor.Compress(ms, stream);
     }
 }
Ejemplo n.º 11
0
        /// <summary>
        /// Reads an object from the request body.
        /// </summary>
        /// <param name="context">The Microsoft.AspNetCore.Mvc.Formatters.InputFormatterContext.</param>
        /// <returns>A System.Threading.Tasks.Task that on completion deserializes the request body.</returns>
        public override async Task <InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context)
        {
            try
            {
                var origin = context.HttpContext.Request.Body;
                using (var rms = new RecycleMemoryStream())
                {
                    await origin.CopyToAsync(rms).ConfigureAwait(false);

                    rms.Position = 0;
                    var obj = _serializer.Deserialize(rms, context.ModelType);
                    return(InputFormatterResult.Success(obj));
                }
            }
            catch (Exception ex)
            {
                Core.Log.Write(ex);
                return(InputFormatterResult.Failure());
            }
        }
Ejemplo n.º 12
0
        public async Task SerializeToFileAsync(object item, Type itemType, string filePath)
        {
            filePath = Factory.GetAbsolutePath(filePath);
            var resPath = filePath;

            if (UseFileExtensions)
            {
                if (Compressor != null)
                {
                    var compExt = Compressor.FileExtension;
                    if (!Extensions.Any((ext, vTuple) => vTuple.resPath.EndsWith(ext + vTuple.compExt, StringComparison.OrdinalIgnoreCase), (resPath, compExt)))
                    {
                        if (!Extensions.Any((ext, rPath) => rPath.EndsWith(ext, StringComparison.OrdinalIgnoreCase), resPath))
                        {
                            resPath = filePath + Extensions.FirstOrDefault() + compExt;
                        }
                        else
                        {
                            resPath = filePath + compExt;
                        }
                    }
                }
                else if (!Extensions.Any((ext, rPath) => rPath.EndsWith(ext, StringComparison.OrdinalIgnoreCase), resPath))
                {
                    resPath = filePath + Extensions.FirstOrDefault();
                }
            }
            if (!string.Equals(resPath, filePath, StringComparison.OrdinalIgnoreCase) && !SerializerManager.SupressFileExtensionWarning)
            {
                Core.Log.Warning("The {0} is using the UseFileExtensions flag, so the file: {1} was changed to: {2}", GetType().Name, filePath, resPath);
            }
            using (var stream = new RecycleMemoryStream())
            {
                Serialize(item, itemType, stream);
                stream.Position = 0;
                using (var fstream = File.Open(resPath, FileMode.Create, FileAccess.Write))
                {
                    await stream.CopyToAsync(fstream).ConfigureAwait(false);
                }
            }
        }
Ejemplo n.º 13
0
        protected override void OnHandler(ParameterHandlerInfo info)
        {
            Core.Log.Warning("Starting MEMORY STREAMS TEST");


            Core.Log.WriteEmptyLine();
            Core.Log.InfoBasic("Press Enter to Start RecycleMemoryStream Test.");
            Console.ReadLine();
            var xbuffer = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

            using (Watch.Create("RecycleMemoryStream"))
            {
                for (var m = 0; m < 50000; m++)
                {
                    using (var rms = new RecycleMemoryStream())
                    {
                        for (var x = 0; x < 1; x++)
                        {
                            for (int i = 0; i < 10000; i++)
                            {
                                rms.Write(xbuffer, 0, xbuffer.Length);
                            }
                        }
                        rms.Position = 0;
                        for (var x = 0; x < 10; x++)
                        {
                            for (int i = 0; i < 2000; i++)
                            {
                                var bt = rms.ReadByte();
                            }
                        }
                    }
                }
            }
            Core.Log.WriteEmptyLine();
            Core.Log.InfoBasic("Press Enter to Start MemoryStream Test.");
            Console.ReadLine();
            using (Watch.Create("MemoryStream"))
            {
                for (var m = 0; m < 50000; m++)
                {
                    using (var rms = new MemoryStream())
                    {
                        for (var x = 0; x < 1; x++)
                        {
                            for (var i = 0; i < 10000; i++)
                            {
                                rms.Write(xbuffer, 0, xbuffer.Length);
                            }
                        }
                        rms.Position = 0;
                        for (var x = 0; x < 10; x++)
                        {
                            for (var i = 0; i < 2000; i++)
                            {
                                var bt = rms.ReadByte();
                            }
                        }
                    }
                }
            }



            Core.Log.WriteEmptyLine();
            Core.Log.InfoBasic("Press Enter to Start CircularBufferStream Test. Press Enter Again to finish the test.");
            Console.ReadLine();
            using (var cbs = new CircularBufferStream(50))
            {
                var cts = new CancellationTokenSource();
                Task.Run(async() =>
                {
                    var i = 0;
                    while (!cts.Token.IsCancellationRequested)
                    {
                        i++;
                        cbs.WriteBytes(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 });
                        if (i % 50 == 0)
                        {
                            Core.Log.InfoMedium("Write {0}", i);
                        }
                        await Task.Delay(1, cts.Token).ConfigureAwait(false);
                    }
                });
                Task.Run(async() =>
                {
                    var i      = 0;
                    var buffer = new byte[15];
                    while (!cts.Token.IsCancellationRequested)
                    {
                        i++;
                        cbs.Read(buffer, 0, 7);
                        if (i % 50 == 0)
                        {
                            Core.Log.InfoMedium("Read {0}", i);
                        }
                        await Task.Delay(1, cts.Token).ConfigureAwait(false);
                    }
                });
                Console.ReadLine();
                cts.Cancel();
            }



            Core.Log.WriteEmptyLine();
            Core.Log.InfoBasic("Press Enter to Start SharedMemoryStream Test. Press Enter Again to finish the test.");
            Console.ReadLine();
            using (var sharedms = new SharedMemoryStream("test", 2000))
            {
                var cts = new CancellationTokenSource();
                Task.Run(async() =>
                {
                    var i = 0;
                    while (!cts.Token.IsCancellationRequested)
                    {
                        i++;
                        sharedms.WriteBytes(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 });
                        if (i % 50 == 0)
                        {
                            Core.Log.InfoMedium("Write {0}", i);
                        }
                        await Task.Delay(1, cts.Token).ConfigureAwait(false);
                    }
                });
                Task.Run(async() =>
                {
                    var i      = 0;
                    var buffer = new byte[15];
                    while (!cts.Token.IsCancellationRequested)
                    {
                        i++;
                        sharedms.Read(buffer, 0, 7);
                        if (i % 50 == 0)
                        {
                            Core.Log.InfoMedium("Read {0}", i);
                        }
                        await Task.Delay(1, cts.Token).ConfigureAwait(false);
                    }
                });
                Console.ReadLine();
            }
        }