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

Ability to specify benchmark description in outputs #2386

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 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
22 changes: 22 additions & 0 deletions samples/BenchmarkDotNet.Samples/IntroRenameTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using BenchmarkDotNet.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace BenchmarkDotNet.Samples
{
[BenchmarkDescription("Used to be 'IntroRenameTest', now is 'My Renamed Test'")]
public class IntroRenameTest
{
// And define a method with the Benchmark attribute
[Benchmark]
public void Sleep() => Thread.Sleep(10);

// You can write a description for your method.
[Benchmark(Description = "Thread.Sleep(10)")]
public void SleepWithDescription() => Thread.Sleep(10);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace BenchmarkDotNet.Attributes
{
public class BenchmarkDescriptionAttribute : Attribute
timcassell marked this conversation as resolved.
Show resolved Hide resolved
{
public BenchmarkDescriptionAttribute(){ }
public BenchmarkDescriptionAttribute(string description)
=> Description = description;

public string Description { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/BenchmarkDotNet/Columns/TargetMethodColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace BenchmarkDotNet.Columns
public class TargetMethodColumn : IColumn
{
public static readonly IColumn Namespace = new TargetMethodColumn(Column.Namespace, benchmark => benchmark.Descriptor.Type.Namespace);
public static readonly IColumn Type = new TargetMethodColumn(Column.Type, benchmark => benchmark.Descriptor.Type.GetDisplayName());
public static readonly IColumn Type = new TargetMethodColumn(Column.Type, benchmark => benchmark.Descriptor.TypeInfo);
public static readonly IColumn Method = new TargetMethodColumn(Column.Method, benchmark => benchmark.Descriptor.WorkloadMethodDisplayInfo, true);

private readonly Func<BenchmarkCase, string> valueProvider;
Expand Down
4 changes: 4 additions & 0 deletions src/BenchmarkDotNet/Extensions/ReflectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ private static IEnumerable<string> GetNestedTypeNames(Type type, bool includeGen
/// </summary>
private static string GetDisplayName(this TypeInfo typeInfo)
{
var customAttr = typeInfo.GetCustomAttribute<BenchmarkDescriptionAttribute>();
if (customAttr != null)
return customAttr.Description;

if (!typeInfo.IsGenericType)
return typeInfo.Name;

Expand Down
15 changes: 13 additions & 2 deletions src/BenchmarkDotNet/Running/BenchmarkConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ private static IEnumerable<Descriptor> GetTargets(
Tuple<MethodInfo, TargetedAttribute>[] iterationCleanupMethods,
IConfig config)
{
var abc = type.GetCustomAttributes(typeof(BenchmarkDescriptionAttribute), false);
timcassell marked this conversation as resolved.
Show resolved Hide resolved
var classDescriptionAttribute = type.ResolveAttribute<BenchmarkDescriptionAttribute>();
return targetMethods
.Select(methodInfo => CreateDescriptor(type,
GetTargetedMatchingMethod(methodInfo, globalSetupMethods),
Expand All @@ -127,8 +129,10 @@ private static IEnumerable<Descriptor> GetTargets(
GetTargetedMatchingMethod(methodInfo, iterationSetupMethods),
GetTargetedMatchingMethod(methodInfo, iterationCleanupMethods),
methodInfo.ResolveAttribute<BenchmarkAttribute>(),
classDescriptionAttribute,
timcassell marked this conversation as resolved.
Show resolved Hide resolved
methodInfo.ResolveAttribute<BenchmarkDescriptionAttribute>(),
targetMethods,
config));
config)) ;
timcassell marked this conversation as resolved.
Show resolved Hide resolved
}

private static MethodInfo GetTargetedMatchingMethod(MethodInfo benchmarkMethod, Tuple<MethodInfo, TargetedAttribute>[] methods)
Expand All @@ -155,18 +159,25 @@ private static Descriptor CreateDescriptor(
MethodInfo iterationSetupMethod,
MethodInfo iterationCleanupMethod,
BenchmarkAttribute attr,
BenchmarkDescriptionAttribute? classDescription,
BenchmarkDescriptionAttribute? methodDescription,
MethodInfo[] targetMethods,
IConfig config)
{
var categoryDiscoverer = config.CategoryDiscoverer ?? DefaultCategoryDiscoverer.Instance;
string description = attr?.Description;
if (description is null)
description = methodDescription?.Description;
timcassell marked this conversation as resolved.
Show resolved Hide resolved
if (description is null)
description = classDescription?.Description;
timcassell marked this conversation as resolved.
Show resolved Hide resolved
var target = new Descriptor(
type,
methodInfo,
globalSetupMethod,
globalCleanupMethod,
iterationSetupMethod,
iterationCleanupMethod,
attr.Description,
description,
baseline: attr.Baseline,
categories: categoryDiscoverer.GetCategories(methodInfo),
operationsPerInvoke: attr.OperationsPerInvoke,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,16 @@ [Benchmark] public void Bar() { }

/* JobBaseline */

[RankColumn, LogicalGroupColumn, BaselineColumn]
[SimpleJob(id: "Job1"), SimpleJob(id: "Job2")]
[BenchmarkDotNet.Attributes.BenchmarkDescription(Description = "MyRenamedTestCase")]
public class JobBaseline_RenameJob_MethodsJobs
{
[Benchmark] public void Base() { }
[Benchmark] public void Foo() { }
[Benchmark] public void Bar() { }
}

[RankColumn, LogicalGroupColumn, BaselineColumn]
[SimpleJob(id: "Job1", baseline: true), SimpleJob(id: "Job2")]
public class JobBaseline_MethodsJobs
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
=== JobBaseline_RenameJob_MethodsJobs ===

BenchmarkDotNet=v0.10.x-mock, OS=Microsoft Windows NT 10.0.x.mock, VM=Hyper-V
MockIntel Core i7-6700HQ CPU 2.60GHz (Max: 3.10GHz), 1 CPU, 8 logical and 4 physical cores
Frequency=2531248 Hz, Resolution=395.0620 ns, Timer=TSC
[Host] : Clr 4.0.x.mock, 64mock RyuJIT-v4.6.x.mock CONFIGURATION
Job1 : extra output line
Job2 : extra output line


Method | Job | Mean | Error | StdDev | Rank | LogicalGroup | Baseline |
------- |----- |---------:|--------:|--------:|-----:|------------- |--------- |
Base | Job1 | 102.0 ns | 6.09 ns | 1.58 ns | 1 | * | No |
Foo | Job1 | 202.0 ns | 6.09 ns | 1.58 ns | 2 | * | No |
Bar | Job1 | 302.0 ns | 6.09 ns | 1.58 ns | 3 | * | No |
Base | Job2 | 402.0 ns | 6.09 ns | 1.58 ns | 4 | * | No |
Foo | Job2 | 502.0 ns | 6.09 ns | 1.58 ns | 5 | * | No |
Bar | Job2 | 602.0 ns | 6.09 ns | 1.58 ns | 6 | * | No |

Errors: 0
53 changes: 53 additions & 0 deletions tests/BenchmarkDotNet.Tests/Running/BenchmarkConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,26 @@ public void MethodDeclarationOrderIsPreserved()
Assert.Equal(nameof(BAC.C), info.BenchmarksCases[2].Descriptor.WorkloadMethod.Name);
}
}
[Fact]
public void DescriptorDescriptionNameOverride()
{
var description = BenchmarkConverter.TypeToBenchmarks(typeof(MethodDescriptionOverrideTests));

Assert.Equal("VoidTest", description.BenchmarksCases[0].Descriptor.WorkloadMethodDisplayInfo);
Assert.Equal("\'from Benchmark\'", description.BenchmarksCases[1].Descriptor.WorkloadMethodDisplayInfo);
Assert.Equal("OverrideFromAttribute", description.BenchmarksCases[2].Descriptor.WorkloadMethodDisplayInfo);
Assert.Equal("\'Who are the winner?\'", description.BenchmarksCases[3].Descriptor.WorkloadMethodDisplayInfo);
}
[Fact]
public void ClassDescriptorDescriptionNameOverride()
{
var description = BenchmarkConverter.TypeToBenchmarks(typeof(ClassDescriptionOverrideTests));

Assert.Equal("FromClassDescription", description.BenchmarksCases[0].Descriptor.WorkloadMethodDisplayInfo);
Assert.Equal("\'from Benchmark\'", description.BenchmarksCases[1].Descriptor.WorkloadMethodDisplayInfo);
Assert.Equal("OverrideFromAttribute", description.BenchmarksCases[2].Descriptor.WorkloadMethodDisplayInfo);
Assert.Equal("\'Who are the winner?\'", description.BenchmarksCases[3].Descriptor.WorkloadMethodDisplayInfo);
}

public class BAC
{
Expand Down Expand Up @@ -278,5 +298,38 @@ public class PrivateIterationCleanup
[IterationCleanup] private void X() { }
[Benchmark] public void A() { }
}
public class MethodDescriptionOverrideTests
{
[Benchmark]
public void VoidTest() { }

[Benchmark(Description = "from Benchmark")]
public void BenchmarkAttributeOverride() {}

[Benchmark]
[BenchmarkDescription("OverrideFromAttribute")]
public void BenchmarkDescriptionAttributeOverride() { }

[Benchmark(Description = "Who are the winner?")]
[BenchmarkDescription("OverrideFromAttribute")]
public void BothAttributeOverride() { }
}
[BenchmarkDescription("FromClassDescription")]
public class ClassDescriptionOverrideTests
{
[Benchmark]
public void VoidTest() { }

[Benchmark(Description = "from Benchmark")]
public void ClassBenchmarkAttributeOverride() { }

[Benchmark]
[BenchmarkDescription("OverrideFromAttribute")]
public void ClassBenchmarkDescriptionAttributeOverride() { }

[Benchmark(Description = "Who are the winner?")]
[BenchmarkDescription("OverrideFromAttribute")]
timcassell marked this conversation as resolved.
Show resolved Hide resolved
public void ClassBothAttributeOverride() { }
}
}
}