Пример #1
0
        internal static CompetitionState Run(
            [NotNull] Type benchmarkType,
            [NotNull] ICompetitionConfig competitionConfig)
        {
            var competitionState = InitCompetitionState(benchmarkType, competitionConfig);
            var runLogger        = new MessageLogger(competitionState.Config, MessageSource.Runner);

            try
            {
                LogCompetitionHeader(competitionState);

                using (var mutex = new Mutex(false, $"Global\\{typeof(CompetitionCore).FullName}"))
                {
                    var lockTaken = false;
                    try
                    {
                        var timeout = competitionState.Options.RunOptions.Concurrent == ConcurrentRunBehavior.Lock
                                                        ? _totalWaitTimeout
                                                        : TimeSpan.Zero;

                        lockTaken = SpinWait(mutex, timeout, _spinWaitRunTimeout, runLogger);
                        if (CheckPreconditions(competitionState, lockTaken, runLogger))
                        {
                            RunCore(competitionState, runLogger);
                        }
                    }
                    finally
                    {
                        if (lockTaken)
                        {
                            mutex.ReleaseMutex();
                        }
                    }
                }
            }
            catch (TargetInvocationException ex)
            {
                runLogger.WriteExceptionMessage(
                    MessageSeverity.ExecutionError,
                    $"Benchmark {competitionState.BenchmarkType.Name} failed.", ex.InnerException ?? ex);
            }
            catch (Exception ex)
            {
                runLogger.WriteExceptionMessage(
                    MessageSeverity.ExecutionError,
                    $"Benchmark {competitionState.BenchmarkType.Name} failed.", ex);
            }
            finally
            {
                LoggerHelpers.FlushLoggers(competitionState.Config);
                competitionState.CompetitionCompleted();
            }

            return(competitionState);
        }
Пример #2
0
        private CompetitionState RunCore(
            [NotNull] Type benchmarkType,
            [CanBeNull] ICompetitionConfig competitionConfig,
            [CanBeNull] CompetitionFeatures competitionFeatures)
        {
            Code.NotNull(benchmarkType, nameof(benchmarkType));

            competitionConfig = CreateBenchmarkConfig(benchmarkType, competitionConfig, competitionFeatures);

            var hostLogger = competitionConfig.GetLoggers().OfType <HostLogger>().Single();

            var previousDirectory = Environment.CurrentDirectory;
            var currentDirectory  = GetOutputDirectory(benchmarkType.Assembly);

            if (currentDirectory == previousDirectory)
            {
                currentDirectory  = null;
                previousDirectory = null;
            }

            CompetitionState competitionState = null;

            try
            {
                SetCurrentDirectoryIfNotNull(currentDirectory);
                try
                {
                    competitionState = CompetitionCore.Run(benchmarkType, competitionConfig);

                    ProcessRunComplete(competitionState);
                }
                finally
                {
                    ReportHostLogger(hostLogger, competitionState?.LastRunSummary);
                }

                using (FilteringLogger.BeginLogImportant(competitionState.Config))
                {
                    ReportMessagesToUser(competitionState);
                }
            }
            finally
            {
                LoggerHelpers.FlushLoggers(competitionConfig);
                SetCurrentDirectoryIfNotNull(previousDirectory);
            }

            return(competitionState);
        }
Пример #3
0
        internal static CompetitionState Run(
            [NotNull] Type benchmarkType,
            [NotNull] ICompetitionConfig competitionConfig)
        {
            Code.NotNull(benchmarkType, nameof(benchmarkType));
            Code.NotNull(competitionConfig, nameof(competitionConfig));
            var runStateSlots = competitionConfig.GetValidators().OfType <RunStateSlots>();

            if (runStateSlots.Count() != 1)
            {
                throw CodeExceptions.Argument(
                          nameof(competitionConfig),
                          $"The competition config should include single instance of {nameof(RunStateSlots)} validator.");
            }

            var competitionState = RunState[competitionConfig];

            try
            {
                competitionState.FirstTimeInit(benchmarkType, competitionConfig);
                var logger = competitionState.Logger;

                using (BeginLogImportant(competitionConfig))
                {
                    logger.WriteSeparatorLine(benchmarkType.Name, true);
                    logger.WriteLineHelp($"{LogInfoPrefix} {benchmarkType.GetShortAssemblyQualifiedName()}");
                }

                using (var mutex = new Mutex(false, $"Global\\{typeof(CompetitionCore).FullName}"))
                {
                    var lockTaken = false;
                    try
                    {
                        var timeout = competitionState.Options.RunOptions.Concurrent == ConcurrentRunBehavior.Lock
                                                        ? TotalWaitTimeout
                                                        : TimeSpan.Zero;

                        lockTaken = SpinWait(mutex, timeout, SpinWaitRunTimeout, competitionState);
                        if (CheckPreconditions(benchmarkType, lockTaken, competitionState))
                        {
                            RunCore(benchmarkType, competitionState);
                        }
                    }
                    finally
                    {
                        if (lockTaken)
                        {
                            mutex.ReleaseMutex();
                        }
                    }
                }
            }
            catch (TargetInvocationException ex)
            {
                competitionState.WriteExceptionMessage(
                    MessageSource.Runner, MessageSeverity.ExecutionError,
                    $"Benchmark {benchmarkType.Name} failed.", ex.InnerException ?? ex);
            }
            catch (Exception ex)
            {
                competitionState.WriteExceptionMessage(
                    MessageSource.Runner, MessageSeverity.ExecutionError,
                    $"Benchmark {benchmarkType.Name} failed.", ex);
            }
            finally
            {
                LoggerHelpers.FlushLoggers(competitionConfig);
            }

            competitionState.CompetitionCompleted();

            return(competitionState);
        }