|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using Azure.Test.Stress; |
| 5 | +using CommandLine; |
| 6 | +using System; |
| 7 | +using System.Threading; |
| 8 | +using System.Threading.Channels; |
| 9 | +using System.Threading.Tasks; |
| 10 | + |
| 11 | +namespace Azure.Sample.Stress |
| 12 | +{ |
| 13 | + public class SendReceiveTest : StressTest<SendReceiveTest.SendReceiveOptions, SendReceiveTest.SendReceiveMetrics> |
| 14 | + { |
| 15 | + private Channel<int> _channel = Channel.CreateUnbounded<int>(); |
| 16 | + |
| 17 | + public SendReceiveTest(SendReceiveOptions options, SendReceiveMetrics metrics) : base(options, metrics) |
| 18 | + { |
| 19 | + } |
| 20 | + |
| 21 | + public override async Task RunAsync(CancellationToken cancellationToken) |
| 22 | + { |
| 23 | + var senderTask = Sender(cancellationToken); |
| 24 | + |
| 25 | + var receiverCts = new CancellationTokenSource(); |
| 26 | + |
| 27 | + var receiverTasks = new Task[Options.Receivers]; |
| 28 | + for (var i = 0; i < Options.Receivers; i++) |
| 29 | + { |
| 30 | + receiverTasks[i] = Receiver(receiverCts.Token); |
| 31 | + } |
| 32 | + |
| 33 | + try |
| 34 | + { |
| 35 | + await senderTask; |
| 36 | + } |
| 37 | + catch (Exception e) when (ContainsOperationCanceledException(e)) |
| 38 | + { |
| 39 | + } |
| 40 | + |
| 41 | + // Block until all messages have been received |
| 42 | + await DelayUntil(() => Metrics.Unprocessed == 0); |
| 43 | + |
| 44 | + receiverCts.Cancel(); |
| 45 | + |
| 46 | + await Task.WhenAll(receiverTasks); |
| 47 | + } |
| 48 | + |
| 49 | + private async Task Sender(CancellationToken cancellationToken) |
| 50 | + { |
| 51 | + var index = 0; |
| 52 | + while (!cancellationToken.IsCancellationRequested) |
| 53 | + { |
| 54 | + try |
| 55 | + { |
| 56 | + await Send(index, cancellationToken); |
| 57 | + Interlocked.Increment(ref Metrics.Sends); |
| 58 | + index++; |
| 59 | + } |
| 60 | + catch (Exception e) when (!ContainsOperationCanceledException(e)) |
| 61 | + { |
| 62 | + Metrics.Exceptions.Enqueue(e); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private async Task Receiver(CancellationToken cancellationToken) |
| 68 | + { |
| 69 | + while (!cancellationToken.IsCancellationRequested) |
| 70 | + { |
| 71 | + try |
| 72 | + { |
| 73 | + await Receive(cancellationToken); |
| 74 | + Interlocked.Increment(ref Metrics.Receives); |
| 75 | + } |
| 76 | + catch (Exception e) when (!ContainsOperationCanceledException(e)) |
| 77 | + { |
| 78 | + Metrics.Exceptions.Enqueue(e); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + // Simulates method in SDK |
| 84 | + private async Task Send(int index, CancellationToken cancellationToken) |
| 85 | + { |
| 86 | + await Task.Delay(TimeSpan.FromMilliseconds(Random.Next(0, Options.MaxSendDelayMs)), cancellationToken); |
| 87 | + var d = Random.NextDouble(); |
| 88 | + if (d < Options.SendExceptionRate) |
| 89 | + { |
| 90 | + throw new SendException(d.ToString()); |
| 91 | + } |
| 92 | + else |
| 93 | + { |
| 94 | + await _channel.Writer.WriteAsync(index, cancellationToken); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + // Simulates method in SDK |
| 99 | + private async Task Receive(CancellationToken cancellationToken) |
| 100 | + { |
| 101 | + await Task.Delay(TimeSpan.FromMilliseconds(Random.Next(0, Options.MaxReceiveDelayMs)), cancellationToken); |
| 102 | + var d = Random.NextDouble(); |
| 103 | + if (d < Options.ReceiveExceptionRate) |
| 104 | + { |
| 105 | + throw new ReceiveException(d.ToString()); |
| 106 | + } |
| 107 | + else |
| 108 | + { |
| 109 | + await _channel.Reader.ReadAsync(cancellationToken); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + public class SendReceiveOptions : StressOptions |
| 114 | + { |
| 115 | + [Option("maxSendDelayMs", Default = 50, HelpText = "Max send delay (in milliseconds)")] |
| 116 | + public int MaxSendDelayMs { get; set; } |
| 117 | + |
| 118 | + [Option("maxReceiveDelayMs", Default = 200, HelpText = "Max send delay (in milliseconds)")] |
| 119 | + public int MaxReceiveDelayMs { get; set; } |
| 120 | + |
| 121 | + [Option("receivers", Default = 3, HelpText = "Number of receivers")] |
| 122 | + public int Receivers { get; set; } |
| 123 | + |
| 124 | + [Option("sendExceptionRate", Default = .01, HelpText = "Rate of send exceptions")] |
| 125 | + public double SendExceptionRate { get; set; } |
| 126 | + |
| 127 | + [Option("receiveExceptionRate", Default = .02, HelpText = "Rate of receive exceptions")] |
| 128 | + public double ReceiveExceptionRate { get; set; } |
| 129 | + } |
| 130 | + |
| 131 | + public class SendReceiveMetrics : StressMetrics |
| 132 | + { |
| 133 | + public long Sends; |
| 134 | + public long Receives; |
| 135 | + public long Unprocessed => (Interlocked.Read(ref Sends) - Interlocked.Read(ref Receives)); |
| 136 | + } |
| 137 | + |
| 138 | + public class SendException : Exception |
| 139 | + { |
| 140 | + public SendException() |
| 141 | + { |
| 142 | + } |
| 143 | + |
| 144 | + public SendException(string message) : base(message) |
| 145 | + { |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + public class ReceiveException : Exception |
| 150 | + { |
| 151 | + public ReceiveException() |
| 152 | + { |
| 153 | + } |
| 154 | + |
| 155 | + public ReceiveException(string message) : base(message) |
| 156 | + { |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments