Skip to content

Commit

Permalink
Fix bugs in statistical tests, fix #2542
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreyAkinshin committed Mar 15, 2024
1 parent ad0240c commit 7a1c10d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
2 changes: 1 addition & 1 deletion samples/BenchmarkDotNet.Samples/IntroStatisticalTesting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace BenchmarkDotNet.Samples
{
[StatisticalTestColumn("1us")]
[StatisticalTestColumn("500us")]
[StatisticalTestColumn("3%")]
[SimpleJob(warmupCount: 0, iterationCount: 5)]
public class IntroStatisticalTesting
Expand Down
9 changes: 7 additions & 2 deletions src/BenchmarkDotNet/Analysers/ZeroMeasurementHelper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using BenchmarkDotNet.Mathematics;
using Perfolizer;
using Perfolizer.Horology;
using Perfolizer.Mathematics.Common;
using Perfolizer.Mathematics.GenericEstimators;
using Perfolizer.Mathematics.SignificanceTesting;
Expand All @@ -13,8 +14,12 @@ internal static class ZeroMeasurementHelper
public static bool IsNegligible(Sample results, double threshold) => HodgesLehmannEstimator.Instance.Median(results) < threshold;
public static bool IsNoticeable(Sample results, double threshold) => !IsNegligible(results, threshold);

public static bool AreIndistinguishable(double[] workload, double[] overhead, Threshold? threshold = null) =>
AreIndistinguishable(new Sample(workload), new Sample(overhead), threshold);
public static bool AreIndistinguishable(double[] workload, double[] overhead, Threshold? threshold = null)
{
var workloadSample = new Sample(workload, TimeUnit.Nanosecond);
var overheadSample = new Sample(overhead, TimeUnit.Nanosecond);
return AreIndistinguishable(workloadSample, overheadSample, threshold);
}

public static bool AreIndistinguishable(Sample workload, Sample overhead, Threshold? threshold = null)
{
Expand Down
9 changes: 4 additions & 5 deletions src/BenchmarkDotNet/Columns/StatisticalTestColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ public static StatisticalTestColumn Create(string threshold, SignificanceLevel?
Statistics current, IReadOnlyDictionary<string, Metric> currentMetrics, bool isBaseline)
{
if (baseline.Sample.Values.SequenceEqual(current.Sample.Values))
return "Base";
return "Baseline";
if (current.Sample.Size == 1 && baseline.Sample.Size == 1)
return "?";

var tost = new SimpleEquivalenceTest(MannWhitneyTest.Instance);
var comparisonResult = tost.Perform(current.Sample, baseline.Sample, Threshold, SignificanceLevel);
var test = new SimpleEquivalenceTest(MannWhitneyTest.Instance);
var comparisonResult = test.Perform(current.Sample, baseline.Sample, Threshold, SignificanceLevel);
return comparisonResult switch
{
ComparisonResult.Greater => "Slower",
Expand All @@ -55,7 +55,6 @@ public static StatisticalTestColumn Create(string threshold, SignificanceLevel?
public override bool IsNumeric => false;
public override UnitType UnitType => UnitType.Dimensionless;

public override string Legend =>
$"MannWhitney-based TOST equivalence test (threshold={Threshold}, alpha = {SignificanceLevel}";
public override string Legend => $"MannWhitney-based equivalence test (threshold={Threshold}, alpha = {SignificanceLevel})";
}
}
3 changes: 2 additions & 1 deletion src/BenchmarkDotNet/Mathematics/Statistics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using BenchmarkDotNet.Extensions;
using JetBrains.Annotations;
using Perfolizer;
using Perfolizer.Horology;
using Perfolizer.Mathematics.Common;
using Perfolizer.Mathematics.OutlierDetection;
using Perfolizer.Mathematics.QuantileEstimators;
Expand Down Expand Up @@ -45,7 +46,7 @@ public class Statistics
public Statistics(IEnumerable<int> values) :
this(values.Select(value => (double)value)) { }

public Statistics(IEnumerable<double> values) : this(new Sample(values.ToArray())) { }
public Statistics(IEnumerable<double> values) : this(new Sample(values.ToArray(), TimeUnit.Nanosecond)) { }

public Statistics(Sample sample)
{
Expand Down

0 comments on commit 7a1c10d

Please sign in to comment.