コード例 #1
0
        public async Task RunOneMessageEmulationAsync(string scenarioName, CancellationToken token)
        {
            Logger.SimulationStarted(_hostName, scenarioName);

            var produceMessagesForScenario = SimulationScenarios.GetScenarioByName(scenarioName);

            var messagingFactory = MessagingFactory
                                   .CreateFromConnectionString(_simulatorConfiguration.EventHubConnectionString);

            var messageSender = new MessageSender(
                messagingFactory: messagingFactory,
                config: _simulatorConfiguration,
                serializer: Serializer.ToJsonUTF8,
                telemetryPublisher: _instrumentationPublisher
                );

            try
            {
                var scenario = produceMessagesForScenario();
                var car      = new Car("Single Message Car", scenario, messageSender.SendAsync);
                await car.RunOneMessageSimulationAsync(token);
            }
            finally
            {
                messagingFactory.Close();
            }

            Logger.SimulationEnded(_hostName);
        }
コード例 #2
0
        public async Task RunEmulationAsync(string scenario, CancellationToken token)
        {
            Logger.SimulationStarted(_hostName, scenario);

            var produceMessagesForScenario = SimulationScenarios.GetScenarioByName(scenario);

            var emulationTasks = new List <Task>();

            var warmup       = _simulatorConfiguration.WarmupDuration;
            var warmupPerCar = warmup.Ticks / _carsPerInstance;

            var messagingFactories =
                Enumerable.Range(0, _simulatorConfiguration.SenderCountPerInstance)
                .Select(i => MessagingFactory.CreateFromConnectionString(_simulatorConfiguration.EventHubConnectionString))
                .ToArray();

            _observableTotalCount
            .Sum()
            .Subscribe(total => Logger.Info("Final total count for all cars is {0}", total));

            _observableTotalCount
            .Buffer(TimeSpan.FromMinutes(5))
            .Scan(0, (total, next) => total + next.Sum())
            .Subscribe(total => Logger.Info("Current count for all cars is {0}", total));

            try
            {
                for (int i = 0; i < _carsPerInstance; i++)
                {
                    // Use the short form of the host or instance name to generate the vehicle ID
                    var carId = String.Format("{0}-{1}", ConfigurationHelper.InstanceName, i);

                    var messageSender = new MessageSender(
                        messagingFactory: messagingFactories[i % messagingFactories.Length],
                        config: _simulatorConfiguration,
                        serializer: Serializer.ToJsonUTF8,
                        telemetryPublisher: _instrumentationPublisher
                        );

                    var carTask = SimulateCarAsync(
                        deviceId: carId,
                        produceMessagesForScenario: produceMessagesForScenario,
                        sendMessageAsync: messageSender.SendAsync,
                        waitBeforeStarting: TimeSpan.FromTicks(warmupPerCar * i),
                        totalCount: _observableTotalCount,
                        token: token
                        );

                    emulationTasks.Add(carTask);
                }

                await Task.WhenAll(emulationTasks.ToArray());

                _observableTotalCount.OnCompleted();
            }
            finally
            {
                // cannot await on a finally block to do CloseAsync
                foreach (var factory in messagingFactories)
                {
                    factory.Close();
                }
            }

            Logger.SimulationEnded(_hostName);
        }