public async Task SendMessageAsync(RunnerStats stats, CancellationToken cancellationToken) { var msg = this.CreateMessage(); for (var attempt = 1; attempt <= MaxSendAttempts; ++attempt) { try { await this.SendAsync(msg, cancellationToken); stats.IncrementMessageSent(); if (this.Config.DuplicateEvery <= 0 || this.random.Next(this.Config.DuplicateEvery) != 0) { break; } attempt = 1; } catch (Exception ex) when(this.IsTransientException(ex)) { stats.IncrementSendTelemetryErrors(); await Task.Yield(); } catch (Exception) { stats.IncrementSendTelemetryErrors(); await Task.Delay(WaitTimeOnTransientError); } } }
private async Task SendMessageAsync(RunnerStats stats, CancellationToken cancellationToken) { const int MaxAttempts = 3; var msg = CreateMessage(); for (var attempt = 1; attempt <= MaxAttempts; ++attempt) { try { await deviceClient.SendEventAsync(msg, cancellationToken); stats.IncrementMessageSent(); break; } catch (IotHubCommunicationException) { stats.IncrementSendTelemetryErrors(); await Task.Yield(); } catch (IotHubException) { stats.IncrementSendTelemetryErrors(); await Task.Delay(WaitTimeOnIotHubError); } } }
async Task RunnerAsync(RunnerStats stats, CancellationToken cancellationToken) { try { await this.sender.OpenAsync(); stats.IncrementDeviceConnected(); // Delay first event by a random amount to avoid bursts await Task.Delay(this.random.Next(this.config.Interval), cancellationToken); var stopwatch = new Stopwatch(); stopwatch.Start(); for (var i = 0L; !cancellationToken.IsCancellationRequested && (this.config.MessageCount <= 0 || i < this.config.MessageCount); i++) { await this.sender.SendMessageAsync(stats, cancellationToken); var millisecondsDelay = Math.Max(0, this.config.Interval * i - stopwatch.ElapsedMilliseconds); await Task.Delay((int)millisecondsDelay, cancellationToken); } stats.IncrementCompletedDevice(); } catch (OperationCanceledException) when(cancellationToken.IsCancellationRequested) { } catch (Exception ex) { Console.Error.WriteLine(ex); } }
async Task RunnerAsync(RunnerStats stats, CancellationToken cancellationToken) { try { await this.sender.OpenAsync(); stats.IncrementDeviceConnected(); for (var i = 0; !cancellationToken.IsCancellationRequested && (this.config.MessageCount <= 0 || i < this.config.MessageCount); i++) { await Task.Delay(this.config.Interval, cancellationToken); await this.sender.SendMessageAsync(stats, cancellationToken); } stats.IncrementCompletedDevice(); } catch (OperationCanceledException) when(cancellationToken.IsCancellationRequested) { } catch (Exception ex) { Console.Error.WriteLine(ex); } }
private async Task RunnerAsync() { var timer = Stopwatch.StartNew(); try { Console.WriteLine("========================================================================================================================"); Console.WriteLine(); Console.WriteLine($"Starting simulator v{Constants.AppVersion}"); Console.WriteLine(); Console.WriteLine("Device count = " + config.DeviceCount); Console.WriteLine($"Device prefix = {config.DevicePrefix}"); Console.WriteLine($"Device 0-last = ({devices[0].DeviceID}-{devices.Last().DeviceID})"); Console.WriteLine("Device index = " + config.DeviceIndex); Console.WriteLine("Message count = " + config.MessageCount); Console.WriteLine("Interval = " + config.Interval + "ms"); if (config.FixPayload != null) { Console.WriteLine("Fix payload = " + config.FixPayload.Length + " bytes"); } else if (config.Template != null) { Console.WriteLine("Template = " + config.Template.GetTemplateDefinition()); } Console.WriteLine("Header = " + config.Header?.GetTemplateDefinition()); Console.WriteLine("========================================================================================================================"); stats = new RunnerStats(); await Task.WhenAll(devices.Select(x => x.Start(stats, stopping.Token))); timer.Stop(); Console.WriteLine($"{DateTime.UtcNow.ToString("o")}: Errors sending telemetry == {stats.TotalSendTelemetryErrors}"); Console.WriteLine($"{DateTime.UtcNow.ToString("o")}: Telemetry generation ended after {timer.ElapsedMilliseconds}ms"); } catch (OperationCanceledException) when(stopping.Token.IsCancellationRequested) { } catch (Exception ex) { Console.Error.WriteLine(ex); } applicationLifetime.StopApplication(); }
async Task RunnerAsync(RunnerStats stats, CancellationToken cancellationToken) { try { await this.sender.OpenAsync(); stats.IncrementDeviceConnected(); // Delay first event by a random amount to avoid bursts int currentInterval = this.interval[0]; await Task.Delay(this.random.Next(currentInterval), cancellationToken); var stopwatch = new Stopwatch(); stopwatch.Start(); long totalIntervalTime = 0; var messageCount = (ulong)this.config.MessageCount; for (ulong i = 0; !cancellationToken.IsCancellationRequested && (messageCount == 0 || i < messageCount); i++) { if (i % 1000 == 0) { totalIntervalTime = 0; stopwatch.Restart(); } await this.sender.SendMessageAsync(stats, cancellationToken); currentInterval = this.interval[i % (ulong)this.interval.Length]; totalIntervalTime += currentInterval; var millisecondsDelay = Math.Max(0, totalIntervalTime - stopwatch.ElapsedMilliseconds); await Task.Delay((int)millisecondsDelay, cancellationToken); } stats.IncrementCompletedDevice(); } catch (OperationCanceledException) when(cancellationToken.IsCancellationRequested) { } catch (Exception ex) { Console.Error.WriteLine(ex); } }
private async Task RunnerAsync() { var timer = Stopwatch.StartNew(); try { Console.WriteLine("========================================================================================================================"); Console.WriteLine(); Console.WriteLine($"Starting simulator v{Constants.AppVersion}"); Console.WriteLine(); Console.WriteLine("Device count = " + this.config.DeviceCount); Console.WriteLine($"Device prefix = {this.config.DevicePrefix}"); Console.WriteLine($"Device 0-last = ({this.devices[0].DeviceID}-{this.devices.Last().DeviceID})"); Console.WriteLine("Device index = " + this.config.DeviceIndex); Console.WriteLine("Message count = " + this.config.MessageCount); Console.WriteLine("Interval = " + string.Join(",", this.config.Intervals) + "ms"); Console.WriteLine("Template = " + this.config.PayloadGenerator.GetDescription()); Console.WriteLine("Header = " + this.config.Header?.GetTemplateDefinition()); Console.WriteLine("========================================================================================================================"); this.stats = new RunnerStats(); await Task.WhenAll(this.devices.Select(x => x.Start(this.stats, this.stopping.Token))); timer.Stop(); Console.WriteLine( this.stats.TotalSendTelemetryErrors != 0 ? $"{DateTime.UtcNow:o}: Errors sending telemetry == {this.stats.TotalSendTelemetryErrors}" : $"{DateTime.UtcNow:o}: No errors sending telemetry"); Console.WriteLine($"{DateTime.UtcNow:o}: Telemetry generation ended after {timer.ElapsedMilliseconds}ms"); } catch (OperationCanceledException) when(this.stopping.Token.IsCancellationRequested) { } catch (Exception ex) { Console.Error.WriteLine(ex); } this.applicationLifetime.StopApplication(); }
public Task Start(RunnerStats stats, CancellationToken cancellationToken) { return(Task.Run(() => this.RunnerAsync(stats, cancellationToken), cancellationToken)); }