Example #1
0
        /// <summary>
        /// Save results from an executed scenario in XML format
        /// </summary>
        /// <param name="scenario">The scenario to save results for</param>
        /// <param name="fileNameWithoutExtension">The filename (without extension) to use to save results</param>
        public void WriteXmlResults(ScenarioBenchmark scenario, string fileNameWithoutExtension)
        {
            var xmlFileName = $"{fileNameWithoutExtension}.xml";

            scenario.Serialize(xmlFileName);
            LogFileSaved(xmlFileName);
        }
Example #2
0
        public void RunScenario(ProcessStartInfo processStartInfo, Action <ScenarioTestConfiguration> preIterationDel,
                                Action <ScenarioTestConfiguration> postIterationDel, Func <ScenarioTestConfiguration, ScenarioBenchmark> teardownDel)
        {
            int iterations = _scenarioTestConfiguration.Iterations;
            int timeout    = (int)(_scenarioTestConfiguration.TimeoutPerIteration.TotalMilliseconds);

            for (int i = 0; i < iterations; i++)
            {
                preIterationDel(_scenarioTestConfiguration);
                using (var p = new Process())
                {
                    p.StartInfo = processStartInfo;
                    p.Start();
                    if (p.WaitForExit(timeout) == false)
                    {
                        p.Kill();
                        Console.WriteLine("Timeouted!");
                        return;
                    }
                }
                postIterationDel(_scenarioTestConfiguration);
            }

            ScenarioBenchmark scenarioBenchmark = teardownDel(_scenarioTestConfiguration);

            if (scenarioBenchmark == null)
            {
                throw new InvalidOperationException("The Teardown Delegate should return a valid instance of ScenarioBenchmark.");
            }

            string scenarioNamespace = scenarioBenchmark.Namespace;

            scenarioBenchmark.Serialize(Configuration.RunId + "-" + scenarioNamespace + ".xml");

            var mdFileName  = Configuration.RunId + "-" + scenarioNamespace + "-Statistics.md";
            var csvFileName = Configuration.RunId + "-" + scenarioNamespace + "-Statistics.csv";

            var dt      = scenarioBenchmark.GetStatistics();
            var mdTable = MarkdownHelper.GenerateMarkdownTable(dt);

            MarkdownHelper.Write(mdFileName, mdTable);
            WriteInfoLine($"Markdown file saved to \"{mdFileName}\"");
            Console.WriteLine(mdTable);

            dt.WriteToCSV(csvFileName);
            WriteInfoLine($"Statistics written to \"{csvFileName}\"");
        }
        /// <summary>
        /// Executes the benchmark scenario specified by the parameter
        /// containing the process start information.<br/>
        /// The process component will wait, for the benchmark scenario to exit,
        /// the time specified on the configuration argument.<br/>
        /// If the benchmark scenario has not exited, then it will immediately
        /// stop the associated process, and a TimeoutException will be thrown.
        /// </summary>
        /// <param name="processStartInfo">The ProcessStartInfo that contains the information that is used to start the benchmark scenario process.</param>
        /// <param name="preIterationDelegate">The action that will be executed before every benchmark scenario execution.</param>
        /// <param name="postIterationDelegate">The action that will be executed after every benchmark scenario execution.</param>
        /// <param name="teardownDelegate">The action that will be executed after running all benchmark scenario iterations.</param>
        /// <param name="scenarioConfiguration">ScenarioConfiguration object that defined the scenario execution.</param>
        public void RunScenario(
            ProcessStartInfo processStartInfo,
            Action preIterationDelegate,
            Action postIterationDelegate,
            Func <ScenarioBenchmark> teardownDelegate,
            ScenarioConfiguration scenarioConfiguration)
        {
            if (processStartInfo == null)
            {
                throw new ArgumentNullException($"{nameof(processStartInfo)} cannot be null.");
            }
            if (teardownDelegate == null)
            {
                throw new ArgumentNullException($"{nameof(teardownDelegate)} cannot be null.");
            }
            if (scenarioConfiguration == null)
            {
                throw new ArgumentNullException($"{nameof(scenarioConfiguration)} cannot be null.");
            }

            // Make a copy of the user input to avoid potential bugs due to changes behind the scenes.
            var configuration = new ScenarioConfiguration(scenarioConfiguration);

            for (int i = 0; i < configuration.Iterations; ++i)
            {
                preIterationDelegate?.Invoke();

                // TODO: Start scenario profiling.

                using (var process = new Process())
                {
                    process.StartInfo = processStartInfo;
                    process.Start();
                    if (process.WaitForExit((int)(configuration.TimeoutPerIteration.TotalMilliseconds)) == false)
                    {
                        process.Kill();
                        throw new TimeoutException("Running benchmark scenario has timed out.");
                    }

                    // Check for the exit code.
                    if (!configuration.SuccessExitCodes.Contains(process.ExitCode))
                    {
                        throw new Exception($"'{processStartInfo.FileName}' exited with an invalid exit code: {process.ExitCode}");
                    }
                }

                // TODO: Stop scenario profiling.

                postIterationDelegate?.Invoke();
            }

            ScenarioBenchmark scenarioBenchmark = teardownDelegate();

            if (scenarioBenchmark == null)
            {
                throw new InvalidOperationException("The Teardown Delegate should return a valid instance of ScenarioBenchmark.");
            }
            scenarioBenchmark.Serialize(Configuration.RunId + "-" + scenarioBenchmark.Namespace + ".xml");

            var dt      = scenarioBenchmark.GetStatistics();
            var mdTable = MarkdownHelper.GenerateMarkdownTable(dt);

            var mdFileName = $"{Configuration.RunId}-{scenarioBenchmark.Namespace}-Statistics.md";

            MarkdownHelper.Write(mdFileName, mdTable);
            WriteInfoLine($"Markdown file saved to \"{mdFileName}\"");
            Console.WriteLine(MarkdownHelper.ToTrimmedTable(mdTable));

            var csvFileName = $"{Configuration.RunId}-{scenarioBenchmark.Namespace}-Statistics.csv";

            dt.WriteToCSV(csvFileName);
            WriteInfoLine($"Statistics written to \"{csvFileName}\"");
        }