/// <summary>
        /// Runs the benchmark.
        /// </summary>
        public void Run(bool displayTimings = false, bool displayEvents = false)
        {
            // TODO: fix Task based or either Thread based benchmarking...
            List <Task> tasks = new List <Task>();

            Console.WriteLine(Stopwatch.IsHighResolution
                ? "Operations timed using the system's high-resolution performance counter."
                : "Operations timed using the DateTime class.");
            Console.WriteLine($"  Timer frequency in ticks per second = {Stopwatch.Frequency}");
            Console.WriteLine("  Timer is accurate within {0} nanoseconds", (1000L * 1000L * 1000L) / Stopwatch.Frequency);
            Console.WriteLine("Running...");
            foreach (IProgram program in _programs)
            {
                if (!_concurrentDictionary.ContainsKey(program.GetType()))
                {
                    _concurrentDictionary.TryAdd(program.GetType(), new List <Benchmark>());
                }
                int j = 1;
                for (int i = 0; i < _runs + WarmupRuns; i++)
                {
                    int j1 = j == 9 ? (j = 1) : j;
                    tasks.Add(Task.Run(() =>
                    {
                        IProgram newProgram = (IProgram)Activator.CreateInstance(program.GetType());
                        BenchmarkAction(newProgram.GetType(), newProgram.Setup, j1, false, false, displayTimings, displayEvents)
                        .ContinueWith(task =>
                                      BenchmarkAction(newProgram.GetType(), newProgram.Run, j1, false, false, displayTimings, displayEvents))
                        .ConfigureAwait(false);
                    }));
                    j++;
                }
            }

            Task.WaitAll(tasks.ToArray());

            foreach (IProgram program in _programs)
            {
                program.Setup();
                program.Run();
                DisplayProgramInformation(program);
                Console.WriteLine($"Runs: {_runs:N0}");
                DisplayBenchmarkInformation(program, actionName: ((Action)program.Setup).Method.Name);
                DisplayBenchmarkInformation(program, actionName: ((Action)program.Run).Method.Name);
                Console.WriteLine("Finished!");
            }
        }
Beispiel #2
0
        private async Task ShutdownAsync()
        {
            if (Interlocked.Exchange(ref _isShuttingDown, 1) == 1)
            {
                return;
            }
            if (_program == null)
            {
                _loggingInitializer?.Dispose();
                Environment.Exit(0);
            }
            else
            {
                _log.Info("Shutting down");

                var task = TaskRunner.RunInBackground(_program.ShutdownAsync);

                var completed = await Task.WhenAny(task, Task.Delay(ShutdownTimeout)).ConfigureAwait(false);

                if (completed != task)
                {
                    _log.Error("Program {0} failed to shutdown gracefully withing the given timeout {1} sec", _program.GetType(), ShutdownTimeout.TotalSeconds);
                    _loggingInitializer?.Dispose();
                    Environment.Exit(1);
                }
                if (task.IsFaulted)
                {
                    _log.Error(task.Exception.ExtractInner(), "Exception while shutting down program {0}", _program.GetType());
                    _loggingInitializer?.Dispose();
                    Environment.Exit(1);
                }
            }
        }
 /// <summary>
 /// Gets the benchmarks for the given <see cref="IProgram"/> and action that has ran.
 /// </summary>
 /// <param name="program">The program that the benchmarks need to be get for.</param>
 /// <param name="actionName">The action name within the program the benchmarks need to be get for.</param>
 /// <returns>The benchmarks for the given <see cref="IProgram"/> and action that has ran.</returns>
 private IEnumerable <Benchmark> GetBenchmarksFor(IProgram program, string actionName)
 {
     return(_concurrentDictionary[program.GetType()].Where(bench => bench.Action == actionName));
 }