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 configure benchmark class name in outputs #1651

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
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
{
[BenchmarkName("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,16 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace BenchmarkDotNet.Attributes
{
[AttributeUsage(AttributeTargets.Class)]
public class BenchmarkNameAttribute : Attribute
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think DescriptionAttribute would be a better name. It matches the Description property of BenchmarkAttribute.

{
public BenchmarkNameAttribute(){ }
public BenchmarkNameAttribute(string name)
=> Name = name;

public string Name { get; set; }
}
}
4 changes: 4 additions & 0 deletions src/BenchmarkDotNet/Extensions/ReflectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ internal static string GetCorrectCSharpTypeName(this Type type, bool includeName
/// </summary>
private static string GetDisplayName(this TypeInfo typeInfo)
{
var customAttr = typeInfo.GetCustomAttribute<BenchmarkNameAttribute>();
if (customAttr != null)
return customAttr.Name;

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

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
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,16 @@ [Benchmark] public void Bar() { }

/* JobBaseline */

[RankColumn, LogicalGroupColumn, BaselineColumn]
[SimpleJob(id: "Job1"), SimpleJob(id: "Job2")]
[BenchmarkDotNet.Attributes.BenchmarkName(Name = "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