Пример #1
0
        /// <summary>
        ///     Warmup phase
        /// </summary>
        private void WarmUp()
        {
            Trace.Debug($"Beginning Warmups for {BenchmarkName}");
            var warmupStopWatch = new Stopwatch();
            var targetTime      = Settings.RunTime;

            Contract.Assert(targetTime != TimeSpan.Zero);
            var runCount = 0L;
            var runTime  = 0L;

            /* Pre-Warmup */


            Trace.Debug("----- BEGIN PRE-WARMUP -----");
            /* Estimate */
            Allocate(); // allocate all collectors needed
            PreRun();

            try
            {
                if (Settings.RunMode == RunMode.Throughput)
                {
                    Trace.Debug(
                        $"Throughput mode: estimating how many invocations of {BenchmarkName} will take {targetTime.TotalSeconds}s");

                    var estimateCount = 3;
                    var runEstimates  = new long[estimateCount];
                    var timeEstimates = new long[estimateCount];
                    for (var i = 0; i <= estimateCount; i++)
                    {
                        warmupStopWatch.Start();
                        while (warmupStopWatch.ElapsedTicks < targetTime.Ticks)
                        {
                            Invoker.InvokeRun(_currentRun.Context);
                            runCount++;
                        }
                        warmupStopWatch.Stop();

                        if (i > 0)
                        {
                            runEstimates[i - 1]  = runCount;
                            timeEstimates[i - 1] = warmupStopWatch.ElapsedTicks;
                        }

                        runCount = 0;
                        warmupStopWatch.Reset();
                    }

                    runCount = (long)Math.Ceiling(runEstimates.Average());
                    runTime  = (long)Math.Ceiling(timeEstimates.Average());

                    Trace.Debug(
                        $"Throughput mode: executed {runCount} instances of {BenchmarkName} in roughly {targetTime.TotalSeconds}s. Using that figure for benchmark.");
                }
                else
                {
                    warmupStopWatch.Start();
                    Invoker.InvokeRun(_currentRun.Context);
                    runCount++;
                    warmupStopWatch.Stop();

                    // elapsed time
                    runTime = warmupStopWatch.ElapsedTicks;
                }
            }
            catch (Exception ex)
            {
                HandleBenchmarkRunException(ex, $"Error occurred during ${BenchmarkName} RUN.");
            }

            PostRun();
            Complete(true);

            // check to see if pre-warmup threw an exception
            var faulted = _currentRun.IsFaulted;

            if (faulted)
            {
                Trace.Error($"Error occurred during pre-warmup. Exiting and producing dump...");

                /*
                 * Normally we don't ever queue up the warmup into the final stats, but we do it
                 * in failure cases so we can capture the exception thrown during warmup into
                 * the final report we're going to deliver to the end-user.
                 */
                CompletedRuns.Enqueue(_currentRun.ToReport(TimeSpan.Zero));

                return;
            }

            Trace.Debug("----- END PRE-WARMUP -----");


            WarmupData = new WarmupData(runTime, runCount);

            if (!Settings.SkipWarmups)
            {
                Trace.Debug("----- BEGIN WARMUPS -----");
                var i = _warmupCount;

                /* Warmup to force CPU caching */
                while (i > 0 && !_currentRun.IsFaulted)
                {
                    RunSingleBenchmark();
                    i--;
                }

                Trace.Debug("----- END WARMUPS -----");
            }
            else
            {
                Trace.Debug("----- SKIPPING WARMUPS -----");
            }
        }
Пример #2
0
        /// <summary>
        ///     Warmup phase
        /// </summary>
        private void WarmUp()
        {
            var warmupStopWatch = new Stopwatch();
            var targetTime      = Settings.RunTime;

            Contract.Assert(targetTime != TimeSpan.Zero);
            var runCount = 0L;

            /* Pre-Warmup */
            RunSingleBenchmark();

            // check to see if pre-warmup threw an exception
            var faulted = _currentRun.IsFaulted;

            if (faulted)
            {
                /*
                 * Normally we don't ever queue up the warmup into the final stats, but we do it
                 * in failure cases so we can capture the exception thrown during warmup into
                 * the final report we're going to deliver to the end-user.
                 */
                CompletedRuns.Enqueue(_currentRun.ToReport(TimeSpan.Zero));

                return;
            }

            /* Esimate */
            Allocate(); // allocate all collectors needed
            PreRun();

            if (Settings.RunMode == RunMode.Throughput)
            {
                warmupStopWatch.Start();
                while (warmupStopWatch.ElapsedTicks < targetTime.Ticks)
                {
                    Invoker.InvokeRun(_currentRun.Context);
                    runCount++;
                }
                warmupStopWatch.Stop();
            }
            else
            {
                warmupStopWatch.Start();
                Invoker.InvokeRun(_currentRun.Context);
                runCount++;
                warmupStopWatch.Stop();
            }

            PostRun();
            Complete();

            // elapsed time
            var runTime = warmupStopWatch.ElapsedTicks;

            WarmupData = new WarmupData(runTime, runCount);

            var i = WarmupCount;

            /* Warmup to force CPU caching */
            while (i > 0 && !_currentRun.IsFaulted)
            {
                RunSingleBenchmark();
                i--;
            }
        }