public async Task RunSimulationAsync(string scenario, CancellationToken token) { //TODO: we need to find a friendlier way to show this. if (!_devices.Any()) { throw new InvalidOperationException("No devices found. Please execute device provisioning first."); } ScenarioSimulatorEventSource.Log.SimulationStarted(_hostName, scenario); var produceEventsForScenario = SimulationScenarios.GetScenarioByName(scenario); var simulationTasks = new List <Task>(); var warmup = _simulatorConfiguration.WarmupDuration; var warmupPerDevice = warmup.Ticks; _observableTotalCount .Sum() .Subscribe(total => ScenarioSimulatorEventSource.Log.FinalEventCountForAllDevices(total)); _observableTotalCount .Buffer(TimeSpan.FromMinutes(5)) .Scan(0, (total, next) => total + next.Sum()) .Subscribe(total => ScenarioSimulatorEventSource.Log.CurrentEventCountForAllDevices(total)); foreach (var device in _devices) { var eventSender = new EventSender( device: device, config: _simulatorConfiguration, serializer: Serializer.ToJsonUTF8 ); var deviceTask = SimulateDeviceAsync( device: device, produceEventsForScenario: produceEventsForScenario, sendEventsAsync: eventSender.SendAsync, waitBeforeStarting: TimeSpan.FromTicks(warmupPerDevice * device.StartupOrder), totalCount: _observableTotalCount, token: token ); simulationTasks.Add(deviceTask); } await Task.WhenAll(simulationTasks.ToArray()).ConfigureAwait(false); _observableTotalCount.OnCompleted(); ScenarioSimulatorEventSource.Log.SimulationEnded(_hostName); }
public async Task RunSimulationAsync(string scenario, CancellationToken token) { //TODO: we need to find a friendlier way to show this. if (!_devices.Any()) { throw new InvalidOperationException("No devices found. Please execute device provisioning first."); } ScenarioSimulatorEventSource.Log.SimulationStarted(_hostName, scenario); var produceEventsForScenario = SimulationScenarios.GetScenarioByName(scenario); var simulationTasks = new List<Task>(); var warmup = _simulatorConfiguration.WarmUpDuration; var warmupPerDevice = warmup.Ticks / _devices.Count; ObserveScenarioOuput(_eventsSentCount); foreach (var device in _devices) { var eventSender = new EventSender( device: device, config: _simulatorConfiguration, serializer: Serializer.ToJsonUTF8 ); var deviceTask = SimulateDeviceAsync( device: device, produceEventsForScenario: produceEventsForScenario, sendEventsAsync: eventSender.SendAsync, waitBeforeStarting: TimeSpan.FromTicks(warmupPerDevice * device.StartupOrder), totalCount: _eventsSentCount, token: token ); simulationTasks.Add(deviceTask); } await Task.WhenAll(simulationTasks.ToArray()).ConfigureAwait(false); _eventsSentCount.OnCompleted(); ScenarioSimulatorEventSource.Log.SimulationEnded(_hostName); }
public async Task RunSimulationAsync(string scenario, CancellationToken token) { ScenarioSimulatorEventSource.Log.SimulationStarted(_hostName, scenario); var produceEventsForScenario = SimulationScenarios.GetScenarioByName(scenario); var simulationTasks = new List<Task>(); var warmup = _simulatorConfiguration.WarmupDuration; var warmupPerDevice = warmup.Ticks / _devicesPerInstance; var messagingFactories = Enumerable.Range(0, _simulatorConfiguration.SenderCountPerInstance) .Select(i => MessagingFactory.CreateFromConnectionString(_simulatorConfiguration.EventHubConnectionString)) .ToArray(); _observableTotalCount .Sum() .Subscribe(total => ScenarioSimulatorEventSource.Log.FinalEventCountForAllDevices(total)); _observableTotalCount .Buffer(TimeSpan.FromMinutes(5)) .Scan(0, (total, next) => total + next.Sum()) .Subscribe(total => ScenarioSimulatorEventSource.Log.CurrentEventCountForAllDevices(total)); try { for (int i = 0; i < _devicesPerInstance; i++) { // Use the short form of the host or instance name to generate the vehicle ID var deviceId = String.Format("{0}-{1}", ConfigurationHelper.InstanceName, i); var eventSender = new EventSender( messagingFactory: messagingFactories[i % messagingFactories.Length], config: _simulatorConfiguration, serializer: Serializer.ToJsonUTF8 ); var deviceTask = SimulateDeviceAsync( deviceId: deviceId, produceEventsForScenario: produceEventsForScenario, sendEventsAsync: eventSender.SendAsync, waitBeforeStarting: TimeSpan.FromTicks(warmupPerDevice * i), totalCount: _observableTotalCount, token: token ); simulationTasks.Add(deviceTask); } await Task.WhenAll(simulationTasks.ToArray()); _observableTotalCount.OnCompleted(); } finally { // cannot await on a finally block to do CloseAsync foreach (var factory in messagingFactories) { factory.Close(); } } ScenarioSimulatorEventSource.Log.SimulationEnded(_hostName); }