generated from frankhaugen/DotnetRepoTemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d28976d
commit 947f74c
Showing
6 changed files
with
182 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Globalization; | ||
|
||
using BenchmarkDotNet.Attributes; | ||
using BenchmarkDotNet.Columns; | ||
using BenchmarkDotNet.Configs; | ||
using BenchmarkDotNet.Diagnosers; | ||
using BenchmarkDotNet.Exporters; | ||
using BenchmarkDotNet.Exporters.Csv; | ||
using BenchmarkDotNet.Exporters.Json; | ||
using BenchmarkDotNet.Extensions; | ||
using BenchmarkDotNet.Reports; | ||
using BenchmarkDotNet.Running; | ||
|
||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
|
||
using Perfolizer.Horology; | ||
|
||
namespace Frank.Testing.TestBases; | ||
|
||
public abstract class HostApplicationBenchmarkBase : HostApplicationTestBase | ||
{ | ||
/// <inheritdoc /> | ||
protected HostApplicationBenchmarkBase(ILoggerProvider loggerProvider) : base(loggerProvider, LogLevel.Information) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Run a benchmark of the specified type and return the summary. | ||
/// </summary> | ||
/// <returns></returns> | ||
protected Summary RunBenchmarks<T>(IConfig config) where T : class | ||
{ | ||
return BenchmarkRunner.Run<T>(config); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Text; | ||
|
||
using BenchmarkDotNet.Loggers; | ||
|
||
namespace Frank.Testing.TestBases; | ||
|
||
public class StringDelegateBenchmarkLogger(Action<string> output) : ILogger | ||
{ | ||
private readonly StringBuilder _stringBuilder = new(); | ||
|
||
/// <inheritdoc /> | ||
public void Write(LogKind logKind, string text) => _stringBuilder.Append(text); | ||
|
||
/// <inheritdoc /> | ||
public void WriteLine() | ||
{ | ||
output(_stringBuilder.ToString()); | ||
_stringBuilder.Clear(); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public void WriteLine(LogKind logKind, string text) => _stringBuilder.AppendLine(text); | ||
|
||
/// <inheritdoc /> | ||
public void Flush() | ||
{ | ||
output(_stringBuilder.ToString()); | ||
_stringBuilder.Clear(); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public string Id { get; set; } = string.Empty; | ||
|
||
/// <inheritdoc /> | ||
public int Priority { get; set; } = 0; | ||
} |
88 changes: 88 additions & 0 deletions
88
Frank.Testing.Tests/TestBases/HostApplicationBenchmarkBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
using System.Diagnostics; | ||
using System.Text; | ||
|
||
using BenchmarkDotNet.Analysers; | ||
using BenchmarkDotNet.Attributes; | ||
using BenchmarkDotNet.Characteristics; | ||
using BenchmarkDotNet.Columns; | ||
using BenchmarkDotNet.Configs; | ||
using BenchmarkDotNet.Diagnosers; | ||
using BenchmarkDotNet.Exporters; | ||
using BenchmarkDotNet.Exporters.Csv; | ||
using BenchmarkDotNet.Jobs; | ||
using BenchmarkDotNet.Loggers; | ||
|
||
using BenchmarkDotNetVisualizer; | ||
|
||
using Frank.Testing.Logging; | ||
using Frank.Testing.TestBases; | ||
|
||
using Xunit.Abstractions; | ||
|
||
namespace Frank.Testing.Tests.TestBases; | ||
|
||
public class HostApplicationBenchmarkBaseTests : HostApplicationBenchmarkBase | ||
{ | ||
private readonly ITestOutputHelper _testOutputHelper; | ||
|
||
/// <inheritdoc /> | ||
public HostApplicationBenchmarkBaseTests(ITestOutputHelper testOutputHelper) : base(new SimpleTestLoggerProvider(testOutputHelper)) | ||
{ | ||
_testOutputHelper = testOutputHelper; | ||
} | ||
|
||
|
||
[Fact] | ||
public async Task Test1() | ||
{ | ||
// Arrange | ||
|
||
// Act | ||
var summary = RunBenchmarks<TestBenchmark>(new DebugInProcessConfig() | ||
.AddDiagnoser(MemoryDiagnoser.Default, new DisassemblyDiagnoser(new DisassemblyDiagnoserConfig()), ThreadingDiagnoser.Default) | ||
.AddExporter(HtmlExporter.Default, CsvExporter.Default, MarkdownExporter.GitHub) | ||
.AddAnalyser(OutliersAnalyser.Default, EnvironmentAnalyser.Default, MultimodalDistributionAnalyzer.Default, ZeroMeasurementAnalyser.Default) | ||
.AddHardwareCounters(HardwareCounter.BranchMispredictions, HardwareCounter.BranchInstructions, HardwareCounter.CacheMisses, HardwareCounter.TotalCycles, HardwareCounter.Timer) | ||
.AddLogger(new StringDelegateBenchmarkLogger(_testOutputHelper.WriteLine)) | ||
); | ||
|
||
// Assert | ||
// var html = summary.GetMarkdown(new ReportMarkdownOptions() | ||
// { | ||
// Title = "TestBenchmark" | ||
// }); | ||
// | ||
// _testOutputHelper.WriteLine(html); | ||
} | ||
|
||
[HardwareCounters( | ||
HardwareCounter.BranchMispredictions, | ||
HardwareCounter.BranchInstructions)] | ||
public class TestBenchmark | ||
{ | ||
[Benchmark] | ||
public void Run() | ||
{ | ||
var result = 1f + 1f + 1f; | ||
} | ||
} | ||
} | ||
|
||
public class XUnitBenchmarkConfiguration : ManualConfig | ||
{ | ||
public XUnitBenchmarkConfiguration(ILogger? benchmarkLogger = null) | ||
{ | ||
if (benchmarkLogger != null) | ||
AddLogger(benchmarkLogger); | ||
|
||
AddJob(Job.ShortRun); | ||
AddExporter(HtmlExporter.Default, new RichMarkdownExporter(new ReportMarkdownOptions() | ||
{ | ||
Title = "Benchmark" | ||
})); | ||
AddDiagnoser(MemoryDiagnoser.Default, new DisassemblyDiagnoser(new DisassemblyDiagnoserConfig()), ThreadingDiagnoser.Default); | ||
WithOptions(ConfigOptions.DisableOptimizationsValidator); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters