Calculates and formats running totals, averages, etc, for an arbitrary statistic.
Пример #1
0
        public void TimeSpanStats()
        {
            Stats.Formatter format    = d => TimeSpan.FromMilliseconds(d).ToString(    @"ss\.fffffff");
            Stats.Formatter formatAvg = d => TimeSpan.FromMilliseconds(d).ToString(@"mm\:ss\.fffffff");

            Stats stats = new Stats(ExpectedIterations, format, formatAvg);

            for (int i = 0; i < ExpectedIterations; i++)
            {
                string report = stats.ReportAndGetSummary(TimeSpan.FromSeconds(i).TotalMilliseconds);
                IList<int> reported = Enumerable.Range(0, i + 1).ToList();
                Assert.AreEqual(	"Current: "  + format   (TimeSpan.FromSeconds(i                                      ).TotalMilliseconds) + "; " +
                                        "Total: "    + format   (TimeSpan.FromSeconds(reported.Sum()                         ).TotalMilliseconds) + "; " +
                                        "Min: "      + format   (TimeSpan.FromSeconds(0                                      ).TotalMilliseconds) + "; " +
                                        "Max: "      + format   (TimeSpan.FromSeconds(i                                      ).TotalMilliseconds) + "; " +
                                        "Avg: "      + formatAvg(TimeSpan.FromSeconds(reported.Average()                     ).TotalMilliseconds) + "; " +
                                        "Expected: " + formatAvg(TimeSpan.FromSeconds(reported.Average() * ExpectedIterations).TotalMilliseconds),
                                        report);
            }
        }
Пример #2
0
        public void Int32Stats()
        {
            Stats.Formatter format = d => d.ToString("n");
            Stats.Formatter formatAvg = format;

            Stats stats = new Stats(ExpectedIterations, format, formatAvg);

            for (int i = 0; i < ExpectedIterations; i++)
            {
                string report = stats.ReportAndGetSummary(i);
                IList<int> reported = Enumerable.Range(0, i + 1).ToList();
                Assert.AreEqual(	"Current: "  + format   (i                                      ) + "; " +
                                        "Total: "    + format   (reported.Sum()                         ) + "; " +
                                        "Min: "      + format   (0                                      ) + "; " +
                                        "Max: "      + format   (i                                      ) + "; " +
                                        "Avg: "      + formatAvg(reported.Average()                     ) + "; " +
                                        "Expected: " + formatAvg(reported.Average() * ExpectedIterations),
                                        report);
            }
        }
Пример #3
0
        private Stats Benchmark_ReportIndividualIterations(int numberOfIterations, Stats stats)
        {
            for (int i = 0; i < numberOfIterations; i++)
            {
                Stopwatch stopwatch = Stopwatch.StartNew();

                Action.Invoke();

                stopwatch.Stop();

                stats.Report(stopwatch.ElapsedMilliseconds);
            }

            return stats;
        }
Пример #4
0
        /// <summary>
        /// Benchmarks and caches (in <see cref="ExecutionTime"/>) the execution time stats of the algorithm.
        /// </summary>
        /// <param name="numberOfIterations">The number of iterations to run.</param>
        /// <param name="reportIndividualIterations">
        /// <para>If set to <c>true</c>, reports individual iteration stats; if <c>false</c>, reports average iteration stats.</para>
        /// <para>If the algorithm runs really fast, the floating point calculations will come out to zero, so you will want to set this to <c>false</c>.</para>
        /// </param>
        /// <returns>The execution time stats of the algorithm.</returns>
        public Stats BenchmarkAndCacheExecutionTime(int numberOfIterations, bool reportIndividualIterations)
        {
            //			ExecutionTime = new Stats(numberOfIterations, d => TimeSpan.FromMilliseconds(d).ToString(@"ss\.fffffff") + " sec", d => (d / 1000).ToString() + " sec");
            Stats.Formatter formatter =
                d =>
                {
                    string format = (d >= 1000 * 60 ? (d >= 1000 * 60 * 60 ? (d >= 1000 * 60 * 60 * 24 ? @"dd\." : "") + @"hh\:" : "") + @"mm\:" : "") + @"ss\.fff";
                    return TimeSpan.FromMilliseconds(d).ToString(format);
                };
            ExecutionTime = new Stats(numberOfIterations, formatter, formatter);

            ExecutionTime = reportIndividualIterations
                                    ? Benchmark_ReportIndividualIterations(numberOfIterations, ExecutionTime)
                                    : Benchmark                           (numberOfIterations, ExecutionTime);

            return ExecutionTime;
        }
Пример #5
0
        private Stats Benchmark(int numberOfIterations, Stats stats)
        {
            Stopwatch stopwatch = Stopwatch.StartNew();

            for (int i = 0; i < numberOfIterations; i++)
            {
                Action.Invoke();
            }

            stopwatch.Stop();

            stats.Report(stopwatch.ElapsedMilliseconds);

            stats.TotalIterations = numberOfIterations;
            stats.Min = stats.Max = stats.Average;

            return stats;
        }