Skip to content

Commit

Permalink
Updated to send random burst size
Browse files Browse the repository at this point in the history
  • Loading branch information
zylinc-bcs committed Sep 20, 2022
1 parent dfb8f2a commit 716c0e5
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 30 deletions.
44 changes: 37 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,47 @@ Run RabbitMQ in docker on local PC with this command
2. Run consumer.exe
3. Run producer.exe

The producer sends a burst of 1 to 9 messages to the consumer.

Output will look like this:

```
CONSUMER: waiting 5 seconds to try initial connection
CONSUMER: waiting for messages...
CONSUMER received 09-16-2022 12:50:59.937 iteration 1 at 09-16-2022 12:50:59.975 - delay: 38,8116 ms
CONSUMER received 09-16-2022 12:51:04.977 iteration 2 at 09-16-2022 12:51:04.984 - delay: 7,5664 ms
CONSUMER received 09-16-2022 12:51:09.983 iteration 3 at 09-16-2022 12:51:09.985 - delay: 2,696 ms
CONSUMER received 09-16-2022 12:51:14.996 iteration 4 at 09-16-2022 12:51:15.000 - delay: 4,8325 ms
CONSUMER received 09-16-2022 12:51:20.013 iteration 5 at 09-16-2022 12:51:20.015 - delay: 2,8959 ms
CONSUMER received 09-16-2022 12:51:25.014 iteration 6 at 09-16-2022 12:51:25.016 - delay: 2,616 ms
CONSUMER received message 1 at 09-20-2022 09:23:52.473, delay: 30,5195 ms
CONSUMER received message 2 at 09-20-2022 09:23:52.510, delay: 49,3794 ms
CONSUMER received message 3 at 09-20-2022 09:23:52.510, delay: 49,8232 ms
CONSUMER received message 4 at 09-20-2022 09:23:57.469, delay: 5,5229 ms
CONSUMER received message 5 at 09-20-2022 09:23:57.470, delay: 5,2937 ms
CONSUMER received message 6 at 09-20-2022 09:24:02.497, delay: 12,1088 ms
CONSUMER received message 7 at 09-20-2022 09:24:02.502, delay: 9,989 ms
CONSUMER received message 8 at 09-20-2022 09:24:02.505, delay: 8,349 ms
CONSUMER received message 9 at 09-20-2022 09:24:02.512, delay: 15,5252 ms
CONSUMER received message 10 at 09-20-2022 09:24:02.521, delay: 23,2979 ms
CONSUMER received message 11 at 09-20-2022 09:24:02.521, delay: 22,9266 ms
CONSUMER received message 12 at 09-20-2022 09:24:02.522, delay: 22,1546 ms
CONSUMER received message 13 at 09-20-2022 09:24:02.522, delay: 20,4214 ms
CONSUMER received message 14 at 09-20-2022 09:24:07.517, delay: 3,8164 ms
CONSUMER received message 15 at 09-20-2022 09:24:07.518, delay: 2,6562 ms
CONSUMER received message 16 at 09-20-2022 09:24:07.518, delay: 2,971 ms
CONSUMER received message 17 at 09-20-2022 09:24:07.519, delay: 3,1154 ms
CONSUMER received message 18 at 09-20-2022 09:24:12.535, delay: 13,0199 ms
CONSUMER received message 19 at 09-20-2022 09:24:12.541, delay: 12,4516 ms
CONSUMER received message 20 at 09-20-2022 09:24:12.543, delay: 8,7846 ms
CONSUMER received message 21 at 09-20-2022 09:24:12.545, delay: 4,7128 ms
CONSUMER received message 22 at 09-20-2022 09:24:12.549, delay: 3,7252 ms
CONSUMER received message 23 at 09-20-2022 09:24:12.560, delay: 6,016 ms
CONSUMER received message 24 at 09-20-2022 09:24:12.564, delay: 3,1196 ms
CONSUMER received message 25 at 09-20-2022 09:24:12.567, delay: 3,7524 ms
```

Delivery of first message is 4 - 5 times as slow as the subsequent messages.
## Observations

Delivery of the first burst of messages is 4 - 5 times as slow as the subsequent messages.

From then on the delivery times vary a lot.

The times above was using RabbitMQ running in a docker container on the same PC.

Related RabbitMQ .NET Client issue<br />
https://github.com/rabbitmq/rabbitmq-dotnet-client/issues/1252
11 changes: 7 additions & 4 deletions consumer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void CancelHandler(object? sender, ConsoleCancelEventArgs e)
}
else
{
int i = 1;
int messageCounter = 0;

using (var channel = connection.CreateModel())
{
Expand All @@ -68,13 +68,16 @@ void CancelHandler(object? sender, ConsoleCancelEventArgs e)
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
messageCounter++;
var body = ea.Body.ToArray();
string message = Encoding.ASCII.GetString(body);
DateTime received = DateTime.Now;
DateTime sent = DateTime.ParseExact(message, "MM/dd/yyyy HH:mm:ss.fff", null);
TimeSpan delay = received - sent;
DateTime received = DateTime.Now;
string receivedText = received.ToString("MM/dd/yyyy HH:mm:ss.fff");
Console.WriteLine($"CONSUMER received at {receivedText}, sent at {message} - iteration: {i++}, delay: {delay.TotalMilliseconds} ms");
TimeSpan delay = received - sent;
Console.WriteLine($"CONSUMER received message {messageCounter} at {receivedText}, delay: {delay.TotalMilliseconds} ms");
};

channel.BasicConsume(queue: "hello", autoAck: true, consumer: consumer);
Expand Down
33 changes: 14 additions & 19 deletions producer/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using RabbitMQ.Client;
using System.Text;
using RabbitMQ.Client;
using RabbitMQ.Client.Exceptions;

using System.Text;

Console.WriteLine("PRODUCER: waiting 5 seconds to try initial connection");
Thread.Sleep(TimeSpan.FromSeconds(5));

Expand All @@ -18,6 +17,7 @@
bool connected = false;

IConnection? connection = null;
Random randomGenerator = new Random();

while (!connected)
{
Expand All @@ -42,7 +42,7 @@
}
else
{
int i = 1;
int messageCounter = 0;
using var channel = connection.CreateModel();

Dictionary<string, object>? arguments = null;
Expand All @@ -57,24 +57,19 @@

while (true)
{
string message = DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss.fff");
var body = Encoding.ASCII.GetBytes(message);
channel.BasicPublish(exchange: "", routingKey: "hello", basicProperties: null, body: body);
Console.WriteLine($"PRODUCER sent {message} - iteration {i++}");
int burstSize = randomGenerator.Next(1, 10);

if (Console.KeyAvailable)
{
ConsoleKeyInfo keyInfo = Console.ReadKey(true);
if (keyInfo.Key == ConsoleKey.Enter)
{
Console.WriteLine("Send loop paused. Press any key to resume or CTRL-C to exit");
Console.ReadKey(true);
}
}
else
for (int n = 0; n < burstSize; n++)
{
Thread.Sleep(TimeSpan.FromSeconds(5));
messageCounter++;
string sendTime = DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss.fff");
var body = Encoding.ASCII.GetBytes(sendTime);
channel.BasicPublish(exchange: "", routingKey: "hello", basicProperties: null, body: body);
Console.WriteLine($"PRODUCER sent message {messageCounter} at {sendTime}");
}

Console.WriteLine();
Thread.Sleep(TimeSpan.FromSeconds(5));
}
}
}

0 comments on commit 716c0e5

Please sign in to comment.