Skip to content
Closed
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
16 changes: 3 additions & 13 deletions samples/BenchmarkDotNet.Samples/IntroExceptionDiagnoser.cs
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think the sample needs to change.

Copy link
Author

Choose a reason for hiding this comment

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

That's draft, sorry) Just tested new output

Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,10 @@ namespace BenchmarkDotNet.Samples
[ExceptionDiagnoser]
public class IntroExceptionDiagnoser
{
[Benchmark]
public void ThrowExceptionRandomly()
[Benchmark] public void NoThrow() { }
[Benchmark] public void Throw()
{
try
{
if (new Random().Next(0, 5) > 1)
{
throw new Exception();
}
}
catch
{
// ignored
}
try { throw new Exception(); } catch { }
}
}
}
8 changes: 6 additions & 2 deletions src/BenchmarkDotNet/Attributes/ExceptionDiagnoserAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ public class ExceptionDiagnoserAttribute : Attribute, IConfigSource
{
public IConfig Config { get; }

public ExceptionDiagnoserAttribute() => Config = ManualConfig.CreateEmpty().AddDiagnoser(ExceptionDiagnoser.Default);
/// <param name="displayExceptions">Display Exceptions column. True by default.</param>
public ExceptionDiagnoserAttribute(bool displayExceptions = true)
{
Config = ManualConfig.CreateEmpty().AddDiagnoser(new ExceptionDiagnoser(new ExceptionDiagnoserConfig(displayExceptions)));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ public class ThreadingDiagnoserAttribute : Attribute, IConfigSource
{
public IConfig Config { get; }

public ThreadingDiagnoserAttribute() => Config = ManualConfig.CreateEmpty().AddDiagnoser(ThreadingDiagnoser.Default);
/// <param name="displayWorkItemsColumn">Display Work Items column. True by default.</param>
/// <param name="displayLockContentionsColumn">Display Lock Contentions column. True by default.</param>
public ThreadingDiagnoserAttribute(bool displayWorkItemsColumn = true, bool displayLockContentionsColumn = true)
{
Config = ManualConfig.CreateEmpty().AddDiagnoser(new ThreadingDiagnoser(new ThreadingDiagnoserConfig(displayWorkItemsColumn, displayLockContentionsColumn)));
}
}
}
11 changes: 8 additions & 3 deletions src/BenchmarkDotNet/Diagnosers/ExceptionDiagnoser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ namespace BenchmarkDotNet.Diagnosers
{
public class ExceptionDiagnoser : IDiagnoser
{
public static readonly ExceptionDiagnoser Default = new ExceptionDiagnoser();
public static readonly ExceptionDiagnoser Default = new ExceptionDiagnoser(new ExceptionDiagnoserConfig(true));

private ExceptionDiagnoser() { }
public ExceptionDiagnoser(ExceptionDiagnoserConfig config) => Config = config;

public ExceptionDiagnoserConfig Config { get; }

public IEnumerable<string> Ids => new[] { nameof(ExceptionDiagnoser) };

Expand All @@ -32,7 +34,10 @@ public void Handle(HostSignal signal, DiagnoserActionParameters parameters) { }

public IEnumerable<Metric> ProcessResults(DiagnoserResults results)
{
yield return new Metric(ExceptionsFrequencyMetricDescriptor.Instance, results.ExceptionFrequency);
if (Config.DisplayExceptionsIfZeroValue || results.ExceptionFrequency != 0)
{
yield return new Metric(ExceptionsFrequencyMetricDescriptor.Instance, results.ExceptionFrequency);
}
}

public IEnumerable<ValidationError> Validate(ValidationParameters validationParameters) => Enumerable.Empty<ValidationError>();
Expand Down
15 changes: 15 additions & 0 deletions src/BenchmarkDotNet/Diagnosers/ExceptionDiagnoserConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using JetBrains.Annotations;

namespace BenchmarkDotNet.Diagnosers;

public class ExceptionDiagnoserConfig
{
/// <param name="displayExceptionsIfZeroValue">Display Exceptions column if it's value is not calculated. True by default.</param>
[PublicAPI]
public ExceptionDiagnoserConfig(bool displayExceptionsIfZeroValue = true)
{
DisplayExceptionsIfZeroValue = displayExceptionsIfZeroValue;
}

public bool DisplayExceptionsIfZeroValue { get; }
}
17 changes: 13 additions & 4 deletions src/BenchmarkDotNet/Diagnosers/ThreadingDiagnoser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ namespace BenchmarkDotNet.Diagnosers
{
public class ThreadingDiagnoser : IDiagnoser
{
public static readonly ThreadingDiagnoser Default = new ThreadingDiagnoser();
public static readonly ThreadingDiagnoser Default = new ThreadingDiagnoser(new ThreadingDiagnoserConfig(true, true));

private ThreadingDiagnoser() { }
public ThreadingDiagnoser(ThreadingDiagnoserConfig config) => Config = config;

public ThreadingDiagnoserConfig Config { get; }

public IEnumerable<string> Ids => new[] { nameof(ThreadingDiagnoser) };

Expand All @@ -33,8 +35,15 @@ public void Handle(HostSignal signal, DiagnoserActionParameters parameters) { }

public IEnumerable<Metric> ProcessResults(DiagnoserResults results)
{
yield return new Metric(CompletedWorkItemCountMetricDescriptor.Instance, results.ThreadingStats.CompletedWorkItemCount / (double)results.ThreadingStats.TotalOperations);
yield return new Metric(LockContentionCountMetricDescriptor.Instance, results.ThreadingStats.LockContentionCount / (double)results.ThreadingStats.TotalOperations);
if (Config.DisplayWorkItemsColumnIfZeroValue || results.ThreadingStats.CompletedWorkItemCount != 0)
{
yield return new Metric(CompletedWorkItemCountMetricDescriptor.Instance, results.ThreadingStats.CompletedWorkItemCount / (double)results.ThreadingStats.TotalOperations);
}

if (Config.DisplayLockContentionsColumnIfZeroValue || results.ThreadingStats.LockContentionCount != 0)
{
yield return new Metric(LockContentionCountMetricDescriptor.Instance, results.ThreadingStats.LockContentionCount / (double)results.ThreadingStats.TotalOperations);
}
}

public IEnumerable<ValidationError> Validate(ValidationParameters validationParameters)
Expand Down
18 changes: 18 additions & 0 deletions src/BenchmarkDotNet/Diagnosers/ThreadingDiagnoserConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using JetBrains.Annotations;

namespace BenchmarkDotNet.Diagnosers;

public class ThreadingDiagnoserConfig
{
/// <param name="displayWorkItemsColumnIfZeroValue">Display Work Items column if it's value is not calculated. True by default.</param>
/// <param name="displayLockContentionsColumnIfZeroValue">Display Lock Contentions column if it's value is not calculated. True by default.</param>
[PublicAPI]
public ThreadingDiagnoserConfig(bool displayWorkItemsColumnIfZeroValue = true, bool displayLockContentionsColumnIfZeroValue = true)
{
DisplayWorkItemsColumnIfZeroValue = displayWorkItemsColumnIfZeroValue;
DisplayLockContentionsColumnIfZeroValue = displayLockContentionsColumnIfZeroValue;
}
public bool DisplayWorkItemsColumnIfZeroValue { get; }

public bool DisplayLockContentionsColumnIfZeroValue { get; }
}
21 changes: 21 additions & 0 deletions tests/BenchmarkDotNet.IntegrationTests/ExceptionDiagnoserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ public void ExceptionCountIsAccurate()
});
}

[Fact]
public void ExceptionTest()
{
var config = CreateConfig();

var summary = BenchmarkRunner.Run<ExceptionCount>(config);
}

public class ExceptionCount
{
[Benchmark]
Expand All @@ -45,6 +53,19 @@ public void ThrowOneException()
[Benchmark]
public void DoNothing() { }

[Benchmark]
public void NoThrow() { }

[Benchmark]
public void Throw()
{
try
{
throw new Exception();
}
catch { }
}

[Benchmark]
public async Task ThrowFromMultipleThreads()
{
Expand Down