Exemplo n.º 1
0
        /// <summary>Executes the specified benchmark.</summary>
        public ExecuteResult Execute(ExecuteParameters executeParameters)
        {
            // TODO: preallocate buffer for output (no direct logging)?
            var hostLogger = LogOutput ? executeParameters.Logger : NullLogger.Instance;
            var host       = new InProcessHost(executeParameters.BenchmarkCase, hostLogger, executeParameters.Diagnoser);

            int exitCode  = -1;
            var runThread = new Thread(() => exitCode = ExecuteCore(host, executeParameters));

            if (executeParameters.BenchmarkCase.Descriptor.WorkloadMethod.GetCustomAttributes <STAThreadAttribute>(false).Any() &&
                Portability.RuntimeInformation.IsWindows())
            {
                runThread.SetApartmentState(ApartmentState.STA);
            }

            runThread.IsBackground = true;

            var timeout = HostEnvironmentInfo.GetCurrent().HasAttachedDebugger ? UnderDebuggerTimeout : ExecutionTimeout;

            runThread.Start();

            if (!runThread.Join((int)timeout.TotalMilliseconds))
            {
                throw new InvalidOperationException(
                          $"Benchmark {executeParameters.BenchmarkCase.DisplayInfo} takes too long to run. " +
                          "Prefer to use out-of-process toolchains for long-running benchmarks.");
            }

            return(ExecuteResult.FromRunResults(host.RunResults, exitCode));
        }