Exemplo n.º 1
0
        static async Task MemoryMappedDecode()
        {
            const string path = "t8.shakespeare.txt";

            Console.WriteLine();
            var fi = new FileInfo(path);

            if (!fi.Exists)
            {
                Console.WriteLine($"Input file not found: {path}");
                return;
            }
            const int REPEAT = 10;
            var       enc    = Encoding.ASCII;

            Console.WriteLine($"Reading: {fi.Name}, {fi.Length} bytes, encoding: {enc.EncodingName}");
            Console.WriteLine();
            Console.WriteLine("Using PipeTextReader/MemoryMappedPipeReader (no buffer)");
            for (int i = 0; i < REPEAT; i++)
            {
                var mmap = MemoryMappedPipeReader.Create(path);
                using (mmap as IDisposable)
                    using (var reader = PipeTextReader.Create(mmap, enc, bufferSize: 0))
                    {
                        var watch = Stopwatch.StartNew();
                        var s     = await TestReaderTests.MeasureAndTime(reader);

                        watch.Stop();
                        Console.WriteLine($"{s}; time taken: {watch.ElapsedMilliseconds}ms");
                    }
            }
            Console.WriteLine();
            Console.WriteLine("Using PipeTextReader/MemoryMappedPipeReader (leased buffer)");
            for (int i = 0; i < REPEAT; i++)
            {
                var mmap = MemoryMappedPipeReader.Create(path);
                using (mmap as IDisposable)
                    using (var reader = PipeTextReader.Create(mmap, enc))
                    {
                        var watch = Stopwatch.StartNew();
                        var s     = await TestReaderTests.MeasureAndTime(reader);

                        watch.Stop();
                        Console.WriteLine($"{s}; time taken: {watch.ElapsedMilliseconds}ms");
                    }
            }
            Console.WriteLine();
            Console.WriteLine("Using StreamReader/FileStream");
            for (int i = 0; i < REPEAT; i++)
            {
                using (var reader = new StreamReader(path, enc))
                {
                    var watch = Stopwatch.StartNew();
                    var s     = await TestReaderTests.MeasureAndTime(reader);

                    watch.Stop();
                    Console.WriteLine($"{s}; time taken: {watch.ElapsedMilliseconds}ms");
                }
            }
        }
Exemplo n.º 2
0
        public async ValueTask <int> TestMemoryMappedPipe()
        {
            int x = 0;

            for (int i = 0; i < LoopCount; i++)
            {
                var mmap = MemoryMappedPipeReader.Create(path);
                using (mmap as IDisposable)
                    using (var reader = PipeTextReader.Create(mmap, encoding))
                    {
                        x += (await TestReaderTests.MeasureAndTime(reader)).Length;
                    }
            }
            return(x);
        }
        public async Task Basic_Pipelines_Text_PingPong()
        {
            Log.DebugLog();
            var tuple = CreateConnectedSocketPair();

            using (var client = tuple.Item1)
                using (var server = tuple.Item2)
                {
                    var clientPipe = SocketConnection.Create(client, InlineReceive, InlineSend, name: "socket client");
                    var serverPipe = SocketConnection.Create(server, InlineReceive, InlineSend, name: "socket server");

                    var enc = Encoding.UTF8;
                    await PingPong(
                        PipeTextReader.Create(clientPipe.Input, enc),
                        PipeTextWriter.Create(clientPipe.Output, enc),
                        PipeTextReader.Create(serverPipe.Input, enc),
                        PipeTextWriter.Create(serverPipe.Output, enc),
                        LoopCount);
                }
            Log.DebugLog("All good!");
        }