private List<Measurement> RunAuto(long invokeCount, IterationMode iterationMode, int unrollFactor)
        {
            var measurements = new List<Measurement>(MaxIterationCount);
            var measurementsForStatistics = new List<Measurement>(MaxIterationCount);

            int iterationCounter = 0;
            bool isIdle = iterationMode.IsIdle();
            double maxErrorRelative = isIdle ? MaxIdleStdErrRelative : maxStdErrRelative;
            while (true)
            {
                iterationCounter++;
                var measurement = RunIteration(iterationMode, iterationCounter, invokeCount, unrollFactor);
                measurements.Add(measurement);
                measurementsForStatistics.Add(measurement);

                var statistics = MeasurementsStatistics.Calculate(measurementsForStatistics, removeOutliers);
                double actualError = statistics.StandardError;
                double maxError = maxErrorRelative * statistics.Mean;

                if (iterationCounter >= MinIterationCount && actualError < maxError)
                    break;

                if (iterationCounter >= MaxIterationCount || (isIdle && iterationCounter >= MaxIdleIterationCount))
                    break;
            }
            if (!IsDiagnoserAttached) WriteLine();
            return measurements;
        }
        private static bool IsWarmupFinished(List <Measurement> measurements, IterationMode iterationMode)
        {
            int n = measurements.Count;

            if (n >= MaxIterationCount || (iterationMode.IsIdle() && n >= MaxIdleItertaionCount))
            {
                return(true);
            }
            if (n < MinIterationCount)
            {
                return(false);
            }

            int dir = -1, changeCount = 0;

            for (int i = 1; i < n; i++)
            {
                int nextDir = Math.Sign(measurements[i].Nanoseconds - measurements[i - 1].Nanoseconds);
                if (nextDir != dir || nextDir == 0)
                {
                    dir = nextDir;
                    changeCount++;
                }
            }

            return(changeCount >= 4);
        }
        private List <Measurement> RunAuto(long invokeCount, IterationMode iterationMode, int unrollFactor)
        {
            int    iterationCounter = 0;
            bool   isIdle           = iterationMode.IsIdle();
            double maxErrorRelative = isIdle ? MaxIdleStdErrRelative : maxStdErrRelative;

            while (true)
            {
                iterationCounter++;
                var measurement = RunIteration(iterationMode, iterationCounter, invokeCount, unrollFactor);
                measurements.Add(measurement);

                var    statistics  = MeasurementsStatistics.Calculate(measurements, removeOutliers);
                double actualError = statistics.StandardError;
                double maxError    = maxErrorRelative * statistics.Mean;

                if (iterationCounter >= MinIterationCount && actualError < maxError)
                {
                    break;
                }

                if (iterationCounter >= MaxIterationCount || (isIdle && iterationCounter >= MaxIdleIterationCount))
                {
                    break;
                }
            }
            if (!IsDiagnoserAttached)
            {
                WriteLine();
            }
            return(measurements);
        }
Exemplo n.º 4
0
        private List <Measurement> RunAuto(long invokeCount, IterationMode iterationMode, int unrollFactor)
        {
            var    measurements     = new List <Measurement>();
            int    iterationCounter = 0;
            bool   isIdle           = iterationMode.IsIdle();
            double maxErrorRelative = isIdle ? MaxIdleStdErrRelative : Resolver.Resolve(TargetAccuracy.MaxStdErrRelative);

            while (true)
            {
                iterationCounter++;
                var measurement = RunIteration(iterationMode, iterationCounter, invokeCount, unrollFactor);
                measurements.Add(measurement);

                var statistics = new Statistics(measurements.Select(m => m.Nanoseconds));
                if (Resolver.Resolve(TargetAccuracy.RemoveOutliers))
                {
                    statistics = new Statistics(statistics.WithoutOutliers());
                }
                double actualError = statistics.StandardError;
                double maxError    = maxErrorRelative * statistics.Mean;

                if (iterationCounter >= MinIterationCount && actualError < maxError)
                {
                    break;
                }

                if (iterationCounter >= MaxIterationCount || (isIdle && iterationCounter >= MaxIdleIterationCount))
                {
                    break;
                }
            }
            WriteLine();
            return(measurements);
        }
Exemplo n.º 5
0
        private void RunCore(
            IterationMode iterationMode,
            int repeatCount,
            List <Measurement> measurements)
        {
            DebugCode.BugIf(measurements.Count > 0, "measurements not empty.");
            DebugCode.BugIf(measurements.Capacity < repeatCount, "measurements capacity not set.");

            var action = iterationMode.IsIdle() ? IdleAction : MainAction;
            var resultIterationsCount = ResultIterationsCount;

            // TODO: reenable after https://github.com/dotnet/BenchmarkDotNet/issues/302#issuecomment-262057686
            //if (!iterationMode.IsIdle())
            //	SetupAction?.Invoke();

            ForceGcCollect();
            if (ForceAllocations)             // DONTTOUCH: DO NOT merge loops as it will produce inaccurate results for microbenchmarks.
            {
                for (var i = 0; i < repeatCount; i++)
                {
                    ForceGcCollect();

                    var clock = Clock.Start();
                    action(resultIterationsCount);
                    var clockSpan = clock.Stop();

                    measurements.Add(
                        new Measurement(0, iterationMode, i + 1, 1, clockSpan.GetNanoseconds()));
                }
            }
            else
            {
                for (var i = 0; i < repeatCount; i++)
                {
                    var clock = Clock.Start();
                    action(resultIterationsCount);
                    var clockSpan = clock.Stop();

                    measurements.Add(
                        new Measurement(0, iterationMode, i + 1, 1, clockSpan.GetNanoseconds()));
                }
            }

            ForceGcCollect();
            // TODO: reenable after https://github.com/dotnet/BenchmarkDotNet/issues/302#issuecomment-262057686
            //if (!iterationMode.IsIdle())
            //	CleanupAction?.Invoke();
        }
        private static bool IsWarmupFinished(List<Measurement> measurements, IterationMode iterationMode)
        {
            int n = measurements.Count;
            if (n >= MaxIterationCount || (iterationMode.IsIdle() && n >= MaxIdleItertaionCount))
                return true;
            if (n < MinIterationCount)
                return false;

            int dir = -1, changeCount = 0;
            for (int i = 1; i < n; i++)
            {
                int nextDir = Math.Sign(measurements[i].Nanoseconds - measurements[i - 1].Nanoseconds);
                if (nextDir != dir || nextDir == 0)
                {
                    dir = nextDir;
                    changeCount++;
                }
            }

            return changeCount >= 4;
        }
        private List <Measurement> RunAuto(long invokeCount, IterationMode iterationMode, int unrollFactor)
        {
            var measurements = measurementsPool.Next();
            var measurementsForStatistics = measurementsPool.Next();

            int    iterationCounter          = 0;
            bool   isIdle                    = iterationMode.IsIdle();
            double effectiveMaxRelativeError = isIdle ? MaxIdleRelativeError : maxRelativeError;

            while (true)
            {
                iterationCounter++;
                var measurement = RunIteration(iterationMode, iterationCounter, invokeCount, unrollFactor);
                measurements.Add(measurement);
                measurementsForStatistics.Add(measurement);

                var    statistics  = MeasurementsStatistics.Calculate(measurementsForStatistics, removeOutliers);
                double actualError = statistics.ConfidenceInterval.Margin;

                double maxError1 = effectiveMaxRelativeError * statistics.Mean;
                double maxError2 = maxAbsoluteError?.Nanoseconds ?? double.MaxValue;
                double maxError  = Math.Min(maxError1, maxError2);

                if (iterationCounter >= MinIterationCount && actualError < maxError)
                {
                    break;
                }

                if (iterationCounter >= MaxIterationCount || (isIdle && iterationCounter >= MaxIdleIterationCount))
                {
                    break;
                }
            }
            if (!IsDiagnoserAttached)
            {
                WriteLine();
            }
            return(measurements);
        }