Ejemplo n.º 1
0
    /// <summary>
    ///   Starts a background task for each test and role that needs to be run in this process, and waits for all
    ///   test runs to completed before finishing the run.
    /// </summary>
    ///
    /// <param name="opts">The parsed command line inputs.</param>
    ///
    private static async Task RunOptions(Options opts)
    {
        // See if there are environment variables available to use in the .env file
        var environment               = new Dictionary <string, string>();
        var appInsightsKey            = String.Empty;
        var eventHubsConnectionString = String.Empty;

        var environmentFile = Environment.GetEnvironmentVariable("ENV_FILE");

        if (!(string.IsNullOrEmpty(environmentFile)))
        {
            environment = EnvironmentReader.LoadFromFile(environmentFile);
        }

        environment.TryGetValue(EnvironmentVariables.ApplicationInsightsKey, out appInsightsKey);
        environment.TryGetValue(EnvironmentVariables.EventHubsConnectionString, out eventHubsConnectionString);

        // If not, and this is an interactive run, try and get them from the user.

        eventHubsConnectionString = PromptForResources("Event Hubs Connection String", "all test runs", eventHubsConnectionString, opts.Interactive);
        appInsightsKey            = PromptForResources("Application Insights Instrumentation Key", "all test runs", appInsightsKey, opts.Interactive);

        // If a job index is provided, a single role is started, otherwise, all specified roles within the
        // test scenario runs are run in parallel.

        var roleConfiguration = new RoleConfiguration();
        var testScenarioTasks = new List <Task>();
        var metrics           = new Metrics(appInsightsKey);

        var testConfiguration = new TestConfiguration();

        testConfiguration.EventHubsConnectionString = eventHubsConnectionString;

        var cancellationSource = new CancellationTokenSource();
        var runDuration        = TimeSpan.FromHours(testConfiguration.DurationInHours);

        cancellationSource.CancelAfter(runDuration);

        using var azureEventListener = new AzureEventSourceListener((args, level) => metrics.Client.TrackTrace($"EventWritten: {args.ToString()} Level: {level}."), EventLevel.Warning);

        // If running the Event Producer Test scenario, gather resources and add a scenario run to the task list.
        if (opts.Test == RoleConfiguration.EventProducerTest || opts.Test == RoleConfiguration.EventProducerTestShort || opts.All)
        {
            // Get the needed resources for the event producer test: an event hub
            var eventHubName = String.Empty;
            environment.TryGetValue(EnvironmentVariables.EventHubEventProducerTest, out eventHubName);
            eventHubName = PromptForResources("Event Hub", RoleConfiguration.EventProducerTest, eventHubName, opts.Interactive);
            testConfiguration.EventHub = eventHubName;

            roleConfiguration.TestScenarioRoles.TryGetValue(RoleConfiguration.EventProducerTest, out var roleList);
            testScenarioTasks.Add(RunScenario(roleList, testConfiguration, roleConfiguration, metrics, opts.Role, RoleConfiguration.EventProducerTest, cancellationSource.Token));
        }

        // If running the Buffered Producer Test scenario, gather resources and add a scenario run to the task list.
        if (opts.Test == RoleConfiguration.BufferedProducerTest || opts.Test == RoleConfiguration.BufferedProducerTestShort || opts.All)
        {
            // Get the needed resources for the buffered producer test: an event hub
            var eventHubName = String.Empty;
            environment.TryGetValue(EnvironmentVariables.EventHubBufferedProducerTest, out eventHubName);
            eventHubName = PromptForResources("Event Hub", RoleConfiguration.BufferedProducerTest, eventHubName, opts.Interactive);
            testConfiguration.EventHub = eventHubName;

            roleConfiguration.TestScenarioRoles.TryGetValue(RoleConfiguration.BufferedProducerTest, out var roleList);
            testScenarioTasks.Add(RunScenario(roleList, testConfiguration, roleConfiguration, metrics, opts.Role, RoleConfiguration.BufferedProducerTest, cancellationSource.Token));
        }

        // If running the Burst Buffered Producer Test scenario, gather resources and add a scenario run to the task list.
        if (opts.Test == RoleConfiguration.BurstBufferedProducerTest || opts.Test == RoleConfiguration.BurstBufferedProducerTestShort || opts.All)
        {
            // Get the needed resources for the buffered producer test: an event hub
            var eventHubName = String.Empty;
            environment.TryGetValue(EnvironmentVariables.EventHubBurstBufferedProducerTest, out eventHubName);
            eventHubName = PromptForResources("Event Hub", "Burst Buffered Producer Test", eventHubName, opts.Interactive);
            testConfiguration.EventHub = eventHubName;

            roleConfiguration.TestScenarioRoles.TryGetValue(RoleConfiguration.BurstBufferedProducerTest, out var roleList);
            testScenarioTasks.Add(RunScenario(roleList, testConfiguration, roleConfiguration, metrics, opts.Role, RoleConfiguration.BurstBufferedProducerTest, cancellationSource.Token));
        }

        // If running the Processor Test scenario, gather resources and add a scenario run to the task list.
        if (opts.Test == RoleConfiguration.EventProcessorTest || opts.Test == RoleConfiguration.EventProcessorTestShort || opts.All)
        {
            // Get the needed resources for the processor test: an event hub, storage account, and blob container name
            var eventHubName            = String.Empty;
            var storageBlob             = String.Empty;
            var storageConnectionString = String.Empty;
            environment.TryGetValue(EnvironmentVariables.EventHubProcessorTest, out eventHubName);
            eventHubName = PromptForResources("Event Hub", RoleConfiguration.EventProcessorTest, eventHubName, opts.Interactive);
            testConfiguration.EventHub = eventHubName;

            environment.TryGetValue(EnvironmentVariables.StorageBlobProcessorTest, out storageBlob);
            storageBlob = PromptForResources("Storage Blob Name", RoleConfiguration.EventProcessorTest, storageBlob, opts.Interactive);
            testConfiguration.BlobContainer = storageBlob;

            environment.TryGetValue(EnvironmentVariables.StorageAccountProcessorTest, out storageConnectionString);
            storageConnectionString = PromptForResources("Storage Account Connection String", RoleConfiguration.EventProcessorTest, storageConnectionString, opts.Interactive);
            testConfiguration.StorageConnectionString = storageConnectionString;

            roleConfiguration.TestScenarioRoles.TryGetValue(RoleConfiguration.EventProcessorTest, out var roleList);
            testScenarioTasks.Add(RunScenario(roleList, testConfiguration, roleConfiguration, metrics, opts.Role, RoleConfiguration.BurstBufferedProducerTest, cancellationSource.Token));
        }

        // Wait for all scenario runs to finish before returning.
        await Task.WhenAll(testScenarioTasks).ConfigureAwait(false);
    }
Ejemplo n.º 2
0
        /// <summary>
        ///   Starts a background task for each test that needs to be run, and waits for all
        ///   test runs to completed before returning.
        /// </summary>
        ///
        /// <param name="opts">The parsed command line inputs.</param>
        ///
        private static async Task RunOptions(Options opts)
        {
            var environment                          = new Dictionary <string, string>();
            var appInsightsKey                       = String.Empty;
            var eventHubsConnectionString            = String.Empty;
            var eventProducerTestConfig              = new EventProducerTestConfig();
            var bufferedProducerTestConfig           = new BufferedProducerTestConfig();
            var burstBufferedProducerTestConfig      = new BufferedProducerTestConfig();
            var concurrentBufferedProducerTestConfig = new BufferedProducerTestConfig();

            var testRunTasks = new List <Task>();

            // Determine which tests should be run based on the command line args

            if (opts.All)
            {
                eventProducerTestConfig.Run              = true;
                bufferedProducerTestConfig.Run           = true;
                burstBufferedProducerTestConfig.Run      = true;
                concurrentBufferedProducerTestConfig.Run = true;
            }

            foreach (var testRun in opts.Tests)
            {
                if (testRun == "EventProd" || testRun == "EventProducerTest")
                {
                    eventProducerTestConfig.Run = true;
                }
                if (testRun == "BuffProd" || testRun == "BufferedProducerTest")
                {
                    bufferedProducerTestConfig.Run = true;
                }
                if (testRun == "BurstBuffProd" || testRun == "BurstBufferedProducerTest")
                {
                    burstBufferedProducerTestConfig.Run = true;
                }
                if (testRun == "ConcurBuffProd" || testRun == "ConcurrentBufferedProducerTest")
                {
                    concurrentBufferedProducerTestConfig.Run = true;
                }
            }

            // See if there are environment variables available to use in the .env file

            var environmentFile = Environment.GetEnvironmentVariable("ENV_FILE");

            if (!(string.IsNullOrEmpty(environmentFile)))
            {
                environment = EnvironmentReader.LoadFromFile(environmentFile);
            }

            environment.TryGetValue(EnvironmentVariables.ApplicationInsightsKey, out appInsightsKey);
            environment.TryGetValue(EnvironmentVariables.EventHubsConnectionString, out eventHubsConnectionString);

            // Prompt for needed resources if interactive mode is enabled

            if (opts.Interactive)
            {
                eventHubsConnectionString = _promptForResources("Event Hubs connection string", "all test runs", eventHubsConnectionString);
                appInsightsKey            = _promptForResources("Application Insights key", "all test runs", appInsightsKey);
            }

            // Run the event producer test

            if (eventProducerTestConfig.Run)
            {
                eventProducerTestConfig.InstrumentationKey        = appInsightsKey;
                eventProducerTestConfig.EventHubsConnectionString = eventHubsConnectionString;

                environment.TryGetValue(EnvironmentVariables.EventHubEventProducerTest, out eventProducerTestConfig.EventHub);

                if (opts.Interactive)
                {
                    eventProducerTestConfig.EventHub = _promptForResources("Event Hub name", "event producer test", eventProducerTestConfig.EventHub);
                }

                var testRun = new EventProducerTest(eventProducerTestConfig);
                testRunTasks.Add(testRun.Run());
            }

            // Run the buffered producer test

            if (bufferedProducerTestConfig.Run)
            {
                bufferedProducerTestConfig.InstrumentationKey        = appInsightsKey;
                bufferedProducerTestConfig.EventHubsConnectionString = eventHubsConnectionString;

                environment.TryGetValue(EnvironmentVariables.EventHubBufferedProducerTest, out bufferedProducerTestConfig.EventHub);

                if (opts.Interactive)
                {
                    bufferedProducerTestConfig.EventHub = _promptForResources("Event Hub name", "buffered producer test", bufferedProducerTestConfig.EventHub);
                }

                bufferedProducerTestConfig.ConcurrentSends = 1;

                var testRun = new BufferedProducerTest(bufferedProducerTestConfig);
                testRunTasks.Add(testRun.Run());
            }

            // Run the burst buffered producer test

            if (burstBufferedProducerTestConfig.Run)
            {
                burstBufferedProducerTestConfig.InstrumentationKey        = appInsightsKey;
                burstBufferedProducerTestConfig.EventHubsConnectionString = eventHubsConnectionString;

                environment.TryGetValue(EnvironmentVariables.EventHubBurstBufferedProducerTest, out burstBufferedProducerTestConfig.EventHub);

                if (opts.Interactive)
                {
                    burstBufferedProducerTestConfig.EventHub = _promptForResources("Event Hub name", "burst buffered producer test", burstBufferedProducerTestConfig.EventHub);
                }

                burstBufferedProducerTestConfig.ConcurrentSends         = 5;
                burstBufferedProducerTestConfig.ProducerPublishingDelay = TimeSpan.FromMinutes(15);

                var testRun = new BufferedProducerTest(burstBufferedProducerTestConfig);
                testRunTasks.Add(testRun.Run());
            }

            // Run the concurrent buffered producer test

            if (concurrentBufferedProducerTestConfig.Run)
            {
                concurrentBufferedProducerTestConfig.InstrumentationKey        = appInsightsKey;
                concurrentBufferedProducerTestConfig.EventHubsConnectionString = eventHubsConnectionString;

                environment.TryGetValue(EnvironmentVariables.EventHubConcurrentBufferedProducerTest, out concurrentBufferedProducerTestConfig.EventHub);

                if (opts.Interactive)
                {
                    concurrentBufferedProducerTestConfig.EventHub = _promptForResources("Event Hub name", "concurrent buffered producer test", concurrentBufferedProducerTestConfig.EventHub);
                }

                concurrentBufferedProducerTestConfig.ConcurrentSends = 5;

                var testRun = new BufferedProducerTest(concurrentBufferedProducerTestConfig);
                testRunTasks.Add(testRun.Run());
            }

            // Wait for all test runs to complete

            await Task.WhenAll(testRunTasks).ConfigureAwait(false);
        }