Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IComparable for complex params #2304

Merged
merged 7 commits into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions docs/articles/samples/IntroComparableComplexParam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
uid: BenchmarkDotNet.Samples.IntroComparableComplexParam
---

## Sample: IntroComparableComplexParam

You can implement `IComparable` (the non generic version) on your complex parameter class if you want custom ordering behavior for your parameter.

One use case for this is having a parameter class that overrides `ToString()`, but also providing a custom ordering behavior that isn't the alphabetical order of the result of `ToString()`.

### Source code

[!code-csharp[IntroComparableComplexParam.cs](../../../samples/BenchmarkDotNet.Samples/IntroComparableComplexParam.cs)]

### Links

* @docs.parameterization
* The permanent link to this sample: @BenchmarkDotNet.Samples.IntroComparableComplexParam

---
2 changes: 2 additions & 0 deletions docs/articles/samples/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
href: IntroCategoryBaseline.md
- name: IntroColdStart
href: IntroColdStart.md
- name: IntroComparableComplexParam
href: IntroComparableComplexParam.md
- name: IntroConfigSource
href: IntroConfigSource.md
- name: IntroConfigUnion
Expand Down
37 changes: 37 additions & 0 deletions samples/BenchmarkDotNet.Samples/IntroComparableComplexParam.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using BenchmarkDotNet.Attributes;

namespace BenchmarkDotNet.Samples
{
public class IntroComparableComplexParam
{
[ParamsSource(nameof(ValuesForA))]
public ComplexParam A { get; set; }

public IEnumerable<ComplexParam> ValuesForA => new[] { new ComplexParam(1, "First"), new ComplexParam(2, "Second") };

[Benchmark]
public object Benchmark() => A;

// Only non generic IComparable is required to provide custom order behavior, but implementing IComparable<> too is customary.
public class ComplexParam : IComparable<ComplexParam>, IComparable
{
public ComplexParam(int value, string name)
{
Value = value;
Name = name;
}

public int Value { get; set; }

public string Name { get; set; }

public override string ToString() => Name;

public int CompareTo(ComplexParam other) => other == null ? 1 : Value.CompareTo(other.Value);

public int CompareTo(object obj) => obj is ComplexParam other ? CompareTo(other) : throw new ArgumentException();
}
}
}
38 changes: 9 additions & 29 deletions src/BenchmarkDotNet/Parameters/ParameterComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,52 +7,32 @@ internal class ParameterComparer : IComparer<ParameterInstances>
{
public static readonly ParameterComparer Instance = new ParameterComparer();

// We will only worry about common, basic types, i.e. int, long, double, etc
// (e.g. you can't write [Params(10.0m, 20.0m, 100.0m, 200.0m)], the compiler won't let you!)
private static readonly Comparer PrimitiveComparer = new Comparer().
Add((string x, string y) => string.CompareOrdinal(x, y)).
Add((int x, int y) => x.CompareTo(y)).
Add((long x, long y) => x.CompareTo(y)).
Add((short x, short y) => x.CompareTo(y)).
Add((float x, float y) => x.CompareTo(y)).
Add((double x, double y) => x.CompareTo(y));

public int Compare(ParameterInstances x, ParameterInstances y)
{
if (x == null && y == null) return 0;
if (x != null && y == null) return 1;
if (x == null) return -1;
for (int i = 0; i < Math.Min(x.Count, y.Count); i++)
{
int compareTo = PrimitiveComparer.CompareTo(x[i]?.Value, y[i]?.Value);
var compareTo = CompareValues(x[i]?.Value, y[i]?.Value);
if (compareTo != 0)
return compareTo;
}
return string.CompareOrdinal(x.DisplayInfo, y.DisplayInfo);
}

private class Comparer
private int CompareValues(object x, object y)
{
private readonly Dictionary<Type, Func<object, object, int>> comparers =
new Dictionary<Type, Func<object, object, int>>();

public Comparer Add<T>(Func<T, T, int> compareFunc)
{
comparers.Add(typeof(T), (x, y) => compareFunc((T)x, (T)y));
return this;
}

public int CompareTo(object x, object y)
// Detect IComparable implementations.
// This works for all primitive types in addition to user types that implement IComparable.
if (x != null && y != null && x.GetType() == y.GetType() &&
x is IComparable xComparable)
{
return x != null && y != null && x.GetType() == y.GetType() && comparers.TryGetValue(GetComparisonType(x), out var comparer)
? comparer(x, y)
: string.CompareOrdinal(x?.ToString(), y?.ToString());
return xComparable.CompareTo(y);
}

private static Type GetComparisonType(object x) =>
x.GetType().IsEnum
? x.GetType().GetEnumUnderlyingType()
: x.GetType();
// Anything else.
return string.CompareOrdinal(x?.ToString(), y?.ToString());
}
}
}
77 changes: 77 additions & 0 deletions tests/BenchmarkDotNet.Tests/ParameterComparerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,82 @@ public void AlphaNumericComparisionTest()
Assert.Equal(1000, sortedData[2].Items[0].Value);
Assert.Equal(2000, sortedData[3].Items[0].Value);
}

[Fact]
public void IComparableComparisionTest()
{
var comparer = ParameterComparer.Instance;

var originalData = new[]
{
new ParameterInstances(new[]
{
new ParameterInstance(sharedDefinition, new ComplexParameter(1, "first"), null)
}),
new ParameterInstances(new[]
{
new ParameterInstance(sharedDefinition, new ComplexParameter(3, "third"), null)
}),
new ParameterInstances(new[]
{
new ParameterInstance(sharedDefinition, new ComplexParameter(2, "second"), null)
}),
new ParameterInstances(new[]
{
new ParameterInstance(sharedDefinition, new ComplexParameter(4, "fourth"), null)
})
};

var sortedData = originalData.OrderBy(d => d, comparer).ToArray();

// Check that we sort by numeric value, not string order!!
Assert.Equal(1, ((ComplexParameter)sortedData[0].Items[0].Value).Value);
Assert.Equal(2, ((ComplexParameter)sortedData[1].Items[0].Value).Value);
Assert.Equal(3, ((ComplexParameter)sortedData[2].Items[0].Value).Value);
Assert.Equal(4, ((ComplexParameter)sortedData[3].Items[0].Value).Value);
}

private class ComplexParameter : IComparable<ComplexParameter>, IComparable
{
public ComplexParameter(int value, string name)
{
Value = value;
Name = name;
}

public int Value { get; }

public string Name { get; }

public override string ToString()
{
return Name;
}

public int CompareTo(ComplexParameter other)
{
if (other == null)
{
return 1;
}

return Value.CompareTo(other.Value);
}

public int CompareTo(object obj)
{
if (obj == null)
{
return 1;
}

if (obj is not ComplexParameter other)
{
throw new ArgumentException();
}

return CompareTo(other);
}
}
}
}