-
-
Notifications
You must be signed in to change notification settings - Fork 983
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
Custom names for argument/parameter values #1634
Comments
What if BDN has this special
[Edit] It could even include an implicit converter from tuple to make it cleaner:
|
Or another option, maybe better, is BDN to have a special enumerable type just for this:
This would be easier to implement than checking the generic types, and I think also a little cleaner in benchmark code. |
Custom names for argument/parameter values is something I also miss. I have to hand-edit my result tables to make sense of my results. |
You don't have to inherit every You simply need var config = DefaultConfig.Instance
.WithSummaryStyle(
DefaultConfig.Instance.SummaryStyle.WithMaxParameterColumnWidth(50));
BenchmarkRunner.Run<BenchmarkClass>(config);
public class PrettyEnumerable<T> : IEnumerable
{
private readonly IEnumerable<T> _value;
public PrettyEnumerable(IEnumerable<T> value) => _value = value;
public IEnumerator GetEnumerator() => _value.GetEnumerator();
public override string ToString() => string.Join(" ", _value);
public static implicit operator List<T>(PrettyEnumerable<T> prettyEnumerable) =>
prettyEnumerable._value.ToList();
}
public static class IEnumerableExtensions
{
public static PrettyEnumerable<T> Prettify<T>(this IEnumerable<T> enumerable)
{
return new PrettyEnumerable<T>(enumerable);
}
}
public class BenchmarkClass
{
[Benchmark]
[ArgumentsSource(nameof(Source))]
public void Benchmark(List<int> argument) { }
// non-generic IEnumerable not working for some reason
public IEnumerable<PrettyEnumerable<int>> Source()
{
// you can write implicit casting from List<int> to PrettyEnumerable<int> instead calling Prettify(),
yield return new List<int> { 100, 500 }.Prettify();
// but you cannot write implicit interface(IEnumerable) casting
yield return Enumerable.Range(0, 20).Prettify();
}
} |
Unfortunately, creating a custom type is not possible in some cases. For instance, if your parameter is an array and you want to use it as a |
Another possible way i would see it would be
|
Hello,
I recently came across the following issue while writing benchmarks.
Say I have a benchmark that receives one argument of type
List<int>
through anArgumentsSource
, for example:The summary for this benchmark would look like this (max column width increased, measurement results are random):
Since the text displayed in the
argument
column is the same for both cases, and only tells of each argument's type, there's no trivial way to distinguish between them and know which exact value was passed to each case. This might not be the exact case in this specific example, but this is the general idea.Note that this is relevant not just to
List<T>
, but to any non-primitive type, and also when usingParamsSource
instead ofArgumentsSource
.I would like to have the aforementioned ability, and one way I thought of doing this is by attaching a custom name to each value in the arguments' source. For example, if I attach the name
"One Two"
tonew List<int> {1, 2}
and"One Three"
tonew List<int> {1, 3}
, the summary could look like this:As far as I understood from looking at the source code and existing issues, there's currently no simple way of doing this.
I could, for example, create a type inheriting from
List<int>
for each value and overrideToString()
with the name I want it to have, but the more values I add the more effort this would take. Furthermore, if I wanted to use asealed
type as an argument, this would not be possible.Another way of distinguishing between cases that's currently possible (which I'm aware of) is by accounting for the order by which the cases appear in the summary. However, I imagine this could get confusing when working with many values or with values of different types.
I've already thought of a way to implement this (both for arguments and params), and I'm willing to open a PR, but beforehand I'd like to know if there's anything I missed (e.g. another way of doing what I described above) and if this is a feature you think fits for this library.
The text was updated successfully, but these errors were encountered: