Пример #1
0
        public async Task TestStagedStreamAsync()
        {
            var readStream  = new MemoryStream();
            var writeStream = new MemoryStream();

            var messageStream = new MessageStream <IStagedBodyMessage>(
                new MessageStreamReader(readStream),
                new StagedBodyMessageDeserializer(
                    new MessageProvider <int, IStagedBodyMessage>(),
                    new TestMessageDeserializer()
                    ),
                new MessageStreamWriter(writeStream),
                new StagedBodyMessageSerializer(
                    new TestMessageSerializer()
                    )
                );

            await messageStream.OpenAsync().ConfigureAwait(false);

            // Write two messages
            await messageStream.WriteAsync(new TestMessage
            {
                Value = 2
            }).ConfigureAwait(false);

            await messageStream.WriteAsync(new TestMessage
            {
                Value = 4
            }).ConfigureAwait(false);

            await messageStream.CloseAsync().ConfigureAwait(false);

            // Reset the streams position so we can read in the messages
            writeStream.Position = 0;
            writeStream.CopyTo(readStream);
            readStream.Position = 0;

            await messageStream.OpenAsync().ConfigureAwait(false);

            // Read the two messages
            var result = await messageStream.ReadAsync().ConfigureAwait(false);

            Assert.False(result.IsCompleted);
            Assert.IsType <TestMessage>(result.Result);
            Assert.Equal(2, (result.Result as TestMessage).Value);

            var result2 = await messageStream.ReadAsync().ConfigureAwait(false);

            Assert.False(result2.IsCompleted);
            Assert.IsType <TestMessage>(result.Result);
            Assert.Equal(4, (result2.Result as TestMessage).Value);

            // This read should signal it's completed.
            var result3 = await messageStream.ReadAsync().ConfigureAwait(false);

            Assert.True(result3.IsCompleted);

            // Close
            await messageStream.CloseAsync().ConfigureAwait(false);
        }
Пример #2
0
        public async Task TestSimpleStreamAsync()
        {
            var readStream  = new MemoryStream();
            var writeStream = new MemoryStream();

            var messageStream = new MessageStream <SimpleMessage>(
                new MessageStreamReader(readStream),
                new SimpleMessageDeserializer(),
                new MessageStreamWriter(writeStream),
                new SimpleMessageSerializer()
                );

            await messageStream.OpenAsync().ConfigureAwait(false);

            // Write two messages
            await messageStream.WriteAsync(new SimpleMessage
            {
                Id    = 1,
                Value = 2
            }).ConfigureAwait(false);

            await messageStream.WriteAsync(new SimpleMessage
            {
                Id    = 2,
                Value = 4
            }).ConfigureAwait(false);

            await messageStream.CloseAsync().ConfigureAwait(false);

            // Reset the streams position so we can read in the messages
            writeStream.Position = 0;
            writeStream.CopyTo(readStream);
            readStream.Position = 0;

            await messageStream.OpenAsync().ConfigureAwait(false);

            // Read the two messages
            var result = await messageStream.ReadAsync().ConfigureAwait(false);

            Assert.False(result.IsCompleted);
            Assert.Equal(1, result.Result.Id);
            Assert.Equal(2, result.Result.Value);

            var result2 = await messageStream.ReadAsync().ConfigureAwait(false);

            Assert.False(result2.IsCompleted);
            Assert.Equal(2, result2.Result.Id);
            Assert.Equal(4, result2.Result.Value);

            // This read should signal it's completed.
            var result3 = await messageStream.ReadAsync().ConfigureAwait(false);

            Assert.True(result3.IsCompleted);

            // Close
            await messageStream.CloseAsync().ConfigureAwait(false);
        }
Пример #3
0
        public async Task TestSimpleProtoBufStream()
        {
            var readStream  = new MemoryStream();
            var writeStream = new MemoryStream();

            var messageStream = new MessageStream <object>(
                new ProtoBuf.ProtoBufMessageDeserializer(),
                new ProtoBuf.ProtoBufMessageSerializer(),
                new StreamDuplexMessageStream(readStream, writeStream)
                );

            await messageStream.OpenAsync().ConfigureAwait(false);

            // Write two messages
            await messageStream.WriteAsync(new SimpleMessage
            {
                Id    = 1,
                Value = 2
            }).ConfigureAwait(false);

            await messageStream.WriteAsync(new SimpleMessage
            {
                Id    = 2,
                Value = 4
            }).ConfigureAwait(false);

            await messageStream.CloseAsync().ConfigureAwait(false);

            // Reset the streams position so we can read in the messages
            readStream          = new MemoryStream(writeStream.ToArray());
            readStream.Position = 0;
            writeStream         = new MemoryStream();

            messageStream = new MessageStream <object>(
                new ProtoBuf.ProtoBufMessageDeserializer(),
                new ProtoBuf.ProtoBufMessageSerializer(),
                new StreamDuplexMessageStream(readStream, writeStream)
                );

            await messageStream.OpenAsync().ConfigureAwait(false);

            // Read the two messages
            var result = await messageStream.ReadAsync().ConfigureAwait(false);

            Assert.False(result.IsCompleted);
            Assert.IsType <SimpleMessage>(result.Result);

            var result2 = await messageStream.ReadAsync().ConfigureAwait(false);

            Assert.False(result2.IsCompleted);
            Assert.IsType <SimpleMessage>(result.Result);

            // This read should signal it's completed.
            var result3 = await messageStream.ReadAsync().ConfigureAwait(false);

            Assert.True(result3.IsCompleted);

            // Close
            await messageStream.CloseAsync().ConfigureAwait(false);
        }
Пример #4
0
        private static async Task <Stream> GetReadStreamAsync(int messageCount)
        {
            var memoryStream  = new MemoryStream();
            var messageStream = new MessageStream <IStagedBodyMessage>(
                new StagedBodyMessageDeserializer(
                    new MessageProvider <int, IStagedBodyMessage>(),
                    new TestMessageDeserializer()
                    ),
                new StagedBodyMessageSerializer(
                    new TestMessageSerializer(),
                    new TestMessage2Serializer()
                    ),
                new StreamDuplexMessageStream(new MemoryStream(), memoryStream)
                );
            var stopwatch = new Stopwatch();

            await messageStream.OpenAsync().ConfigureAwait(false);

            stopwatch.Start();

            var random = new Random();

            for (int i = 0; i < messageCount; i++)
            {
                if (i % 2 == 0)
                {
                    await messageStream.WriteAsync(new TestMessage
                    {
                        Value = (short)random.Next(short.MaxValue)
                    }).ConfigureAwait(false);
                }
                else
                {
                    await messageStream.WriteAsync(new TestMessage2
                    {
                        Value = (uint)random.Next(int.MaxValue)
                    }).ConfigureAwait(false);
                }

                if (i % 1000 == 0)
                {
                    await messageStream.FlushAsync().ConfigureAwait(false);
                }
            }

            stopwatch.Stop();
            Console.WriteLine($"Took {stopwatch.ElapsedMilliseconds}ms to write {messageCount} messages. {messageCount / stopwatch.Elapsed.TotalSeconds} messages/s");

            await messageStream.CloseAsync().ConfigureAwait(false);

            memoryStream.Position = 0;

            return(memoryStream);
        }
        public async Task TestSimpleStreamAsync()
        {
            var readStream  = new MemoryStream();
            var writeStream = new MemoryStream();

            var messageStream = new MessageStream <SimpleMessage>(
                new SimpleMessageDeserializer(),
                new SimpleMessageSerializer(),
                new StreamDuplexMessageStream(readStream, writeStream)
                );

            await messageStream.OpenAsync().ConfigureAwait(false);

            // Write two messages
            await messageStream.WriteAsync(new SimpleMessage
            {
                Id    = 1,
                Value = 2
            }).ConfigureAwait(false);

            await messageStream.WriteAsync(new SimpleMessage
            {
                Id    = 2,
                Value = 4
            }).ConfigureAwait(false);

            await messageStream.CloseAsync().ConfigureAwait(false);

            // Reset the streams position so we can read in the messages
            readStream          = new MemoryStream(writeStream.ToArray());
            readStream.Position = 0;
            writeStream         = new MemoryStream();

            messageStream = new MessageStream <SimpleMessage>(
                new SimpleMessageDeserializer(),
                new SimpleMessageSerializer(),
                new StreamDuplexMessageStream(readStream, writeStream)
                );

            var receivedMessages = new List <SimpleMessage>();
            var closedTcs        = new TaskCompletionSource <bool>();

            var eventedMessageStream = new EventMessageStream <SimpleMessage>
                                       (
                new SimpleMessageDeserializer(),
                new SimpleMessageSerializer(),
                new StreamDuplexMessageStream(readStream, writeStream),
                message =>
            {
                receivedMessages.Add(message);
                return(new ValueTask <bool>());
            },
                () =>     // ignore keep alive.
            {
                return(new ValueTask());
            },
                (ex) =>     // The stream will close because the memory stream will run out of data so ignore results
            {
                closedTcs.TrySetResult(true);
                return(new ValueTask());
            }
                                       );

            await eventedMessageStream.OpenAsync().ConfigureAwait(false);

            await closedTcs.Task.ConfigureAwait(false);

            Assert.Equal(2, receivedMessages.Count);

            try
            {
                await eventedMessageStream.CloseAsync().ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                // ignore, the EOF will have closed it.
            }
        }
Пример #6
0
        public static async Task Main(string[] args)
        {
            const int messageCount = 10_000_000;
            const int iterations   = 10;

            int    messageCounter        = 0;
            double messageParseTimeTotal = 0;
            var    stopwatch             = new Stopwatch();

            var messageProvider = new MessageProvider <int, IStagedBodyMessage>();

            var readStream = await GetReadStreamAsync(messageCount).ConfigureAwait(false);

            var writeStream = new MemoryStream((int)readStream.Length);

            var messageStream = new MessageStream <IStagedBodyMessage>(
                new StagedBodyMessageDeserializer(
                    messageProvider,
                    new TestMessageDeserializer()
                    ),
                new StagedBodyMessageSerializer(
                    new TestMessageSerializer(),
                    new TestMessage2Serializer()
                    ),
                new StreamDuplexMessageStream(readStream, writeStream)
                );

            Console.WriteLine();

            for (int i = 0; i < iterations; i++)
            {
                readStream.Position  = 0;
                writeStream.Position = 0;

                messageCounter        = 0;
                messageParseTimeTotal = 0;

                await messageStream.OpenAsync().ConfigureAwait(false);

                MessageReadResult <IStagedBodyMessage> messageResult;

                stopwatch.Restart();

                while (true)
                {
                    messageResult = await messageStream.ReadAsync().ConfigureAwait(false);

                    if (messageResult.IsCompleted)
                    {
                        break;
                    }

                    messageParseTimeTotal += (messageResult.ParsedTimeUtc - messageResult.ReceivedTimeUtc).TotalMilliseconds;
                    messageCounter++;
                }

                if (messageStream.Open)
                {
                    await messageStream.CloseAsync().ConfigureAwait(false);
                }

                if (messageResult.Error)
                {
                    Console.WriteLine($"Error reading message stream.");
                }

                stopwatch.Stop();

                Console.WriteLine($"Done iteration: {messageCounter / stopwatch.Elapsed.TotalSeconds} messages/s. Avg message parse time: {messageParseTimeTotal / messageCounter}. {messageCounter} total messages read.");
            }

            readStream.Dispose();
            writeStream.Dispose();

            Console.ReadLine();
        }
Пример #7
0
        public async Task TestStagedStreamAsync()
        {
            var readStream  = new MemoryStream();
            var writeStream = new MemoryStream();

            var messageStream = new MessageStream <TestMessage>(
                new StagedBodyMessageDeserializer(
                    new MessageProvider <int, TestMessage>(),
                    new TestMessageDeserializer()
                    ),
                new StagedBodyMessageSerializer(
                    new TestMessageSerializer()
                    ),
                new StreamDuplexMessageStream(readStream, writeStream).MakeWriteOnly()
                );

            await messageStream.OpenAsync().ConfigureAwait(false);

            // Write two messages
            await messageStream.WriteAsync(new TestMessage
            {
                Value = 2
            }).ConfigureAwait(false);

            await messageStream.WriteAsync(new TestMessage
            {
                Value = 4
            }).ConfigureAwait(false);

            await messageStream.CloseAsync().ConfigureAwait(false);

            // Reset the streams position so we can read in the messages
            readStream          = new MemoryStream(writeStream.ToArray());
            readStream.Position = 0;
            writeStream         = new MemoryStream();

            messageStream = new MessageStream <TestMessage>(
                new StagedBodyMessageDeserializer(
                    new MessageProvider <int, TestMessage>(),
                    new TestMessageDeserializer()
                    ),
                new StagedBodyMessageSerializer(
                    new TestMessageSerializer()
                    ),
                new StreamDuplexMessageStream(readStream, writeStream).MakeReadOnly()
                );

            await messageStream.OpenAsync().ConfigureAwait(false);

            // Read the two messages
            var result = await messageStream.ReadAsync().ConfigureAwait(false);

            Assert.False(result.IsCompleted);
            Assert.Equal(2, result.Result.Value);

            var result2 = await messageStream.ReadAsync().ConfigureAwait(false);

            Assert.False(result2.IsCompleted);
            Assert.Equal(4, result2.Result.Value);

            // This read should signal it's completed.
            var result3 = await messageStream.ReadAsync().ConfigureAwait(false);

            Assert.True(result3.IsCompleted);

            // Close
            await messageStream.CloseAsync().ConfigureAwait(false);
        }
Пример #8
0
        public static async Task Main0(string[] args)
        {
            const int messageCount = 1_000_000;
            const int iterations   = 10;

            int messageCounter = 0;
            var stopwatch      = new Stopwatch();

            var messageProvider = new MessageProvider <int, IStagedBodyMessage>();

            var readStream = await GetReadStreamAsync(messageCount).ConfigureAwait(false);

            var writeStream = new MemoryStream();

            var messageStream = new MessageStream <IStagedBodyMessage>(
                new MessageStreamReader(readStream),
                new StagedBodyMessageDeserializer(
                    messageProvider,
                    new TestMessageDeserializer()
                    ),
                new MessageStreamWriter(writeStream),
                new StagedBodyMessageSerializer(
                    new TestMessageSerializer(),
                    new TestMessage2Serializer()
                    )
                );

            for (int i = 0; i < iterations; i++)
            {
                readStream.Position  = 0;
                writeStream.Position = 0;

                messageCounter = 0;

                await messageStream.OpenAsync().ConfigureAwait(false);

                MessageReadResult <IStagedBodyMessage> messageResult;

                stopwatch.Restart();

                while (true)
                {
                    messageResult = await messageStream.ReadAsync().ConfigureAwait(false);

                    if (messageResult.IsCompleted)
                    {
                        break;
                    }

                    //if (messageStream.Open)
                    //{
                    //    await messageStream.WriteAsync(messageResult.Result).ConfigureAwait(false);
                    //}

                    if (messageResult.Result != null)
                    {
                        //messageProvider.Return(messageResult.Result.MessageId, messageResult.Result);
                    }

                    messageCounter++;
                }

                if (messageStream.Open)
                {
                    await messageStream.CloseAsync().ConfigureAwait(false);
                }

                if (messageResult.Error)
                {
                    Console.WriteLine($"Error reading message stream.");
                }

                stopwatch.Stop();

                Console.WriteLine($"Done iteration: {messageCounter / stopwatch.Elapsed.TotalSeconds} messages/s. {messageCounter} total messages read.");

                Console.WriteLine();
                Console.WriteLine();
            }

            readStream.Dispose();
            writeStream.Dispose();

            Console.ReadLine();
        }