Beispiel #1
0
    static async Task GenerateMessages(QueueInfo queueInfo, CancellationToken token)
    {
        var throttle = new SemaphoreSlim(32);
        var bodySize = 0;

        var random = new Random();

        var sendMeter = Metric.Meter("Sent", Unit.Custom("messages"));

        while (!token.IsCancellationRequested)
        {
            await throttle.WaitAsync(token).ConfigureAwait(false);

            // We don't need to wait for this task
            // ReSharper disable once UnusedVariable
            var sendTask = Task.Run(async() =>
            {
                try
                {
                    var ops = new SendOptions();

                    ops.SetHeader(Headers.HostId, "MyHost");
                    ops.SetHeader(Headers.HostDisplayName, "Load Generator");

                    ops.SetHeader(Headers.ProcessingMachine, RuntimeEnvironment.MachineName);
                    ops.SetHeader(Headers.ProcessingEndpoint, "LoadGenerator");

                    var now = DateTime.UtcNow;
                    ops.SetHeader(Headers.ProcessingStarted, DateTimeExtensions.ToWireFormattedString(now));
                    ops.SetHeader(Headers.ProcessingEnded, DateTimeExtensions.ToWireFormattedString(now));


                    var auditMessage  = new MyMessage();
                    auditMessage.Data = new byte[bodySize];
                    random.NextBytes(auditMessage.Data);

                    await sender.Send(auditMessage, ops).ConfigureAwait(false);
                    queueInfo.Sent();
                    sendMeter.Mark();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
                finally
                {
                    throttle.Release();
                }
            }, token);
        }
    }
Beispiel #2
0
        static async Task GenerateMessages(int bodySize, QueueInfo queueInfo, CancellationToken token, int maxConcurrency)
        {
            var throttle = new SemaphoreSlim(maxConcurrency);
            var random   = new Random();

            var sendMeter = Metric.Meter("Sent", Unit.Custom("messages"));

            while (!token.IsCancellationRequested)
            {
                await throttle.WaitAsync(token).ConfigureAwait(false);

                // We don't need to wait for this task
                // ReSharper disable once UnusedVariable
                var sendTask = Task.Run(async() =>
                {
                    try
                    {
                        var auditMessage = new MyMessage
                        {
                            Data = new byte[bodySize]
                        };
                        random.NextBytes(auditMessage.Data);

                        await endpointInstance.Send(auditMessage).ConfigureAwait(false);
                        queueInfo.Sent();
                        sendMeter.Mark();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }
                    finally
                    {
                        throttle.Release();
                    }
                }, token);
            }
        }