-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Add benchmark test for NameGenerator
Return string in benchmarks Test only two outer most methods
- Loading branch information
Showing
8 changed files
with
113 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using BenchmarkDotNet.Configs; | ||
using BenchmarkDotNet.Diagnosers; | ||
|
||
namespace Moniker.PerformanceTests; | ||
|
||
internal sealed class BenchmarkConfig : ManualConfig | ||
{ | ||
internal BenchmarkConfig() | ||
{ | ||
Add(DefaultConfig.Instance); | ||
|
||
var baseDirectory = AppContext.BaseDirectory; | ||
var rootFolder = baseDirectory[..baseDirectory.LastIndexOf("bin", StringComparison.OrdinalIgnoreCase)]; | ||
var runFolder = DateTime.Now.ToString("u").Replace(' ', '_').Replace(':', '-'); | ||
ArtifactsPath = $@"{rootFolder}\BenchmarkDotNet.Artifacts\{runFolder}"; | ||
|
||
AddDiagnoser(MemoryDiagnoser.Default); | ||
} | ||
} |
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,9 @@ | ||
namespace Moniker.PerformanceTests; | ||
|
||
internal static class Benchmarks | ||
{ | ||
internal static readonly Type[] All = | ||
[ | ||
typeof(NameGeneratorBenchmark) | ||
]; | ||
} |
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,21 @@ | ||
using BenchmarkDotNet.Running; | ||
using Xunit; | ||
|
||
namespace Moniker.PerformanceTests; | ||
|
||
public class Harness | ||
{ | ||
[Fact] | ||
public void MonikerBenchmark() => RunBenchmark<NameGeneratorBenchmark>(); | ||
|
||
/// <remarks> | ||
/// This method is used to enforce that benchmark types are added to <see cref="Benchmarks.All"/> | ||
/// so that they can be used directly from the command line in <see cref="Program.Main"/> as well. | ||
/// </remarks> | ||
private static void RunBenchmark<TBenchmark>() | ||
{ | ||
var targetType = typeof(TBenchmark); | ||
var benchmarkType = Benchmarks.All.Single(type => type == targetType); | ||
BenchmarkRunner.Run(benchmarkType, new BenchmarkConfig()); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
test/Moniker.PerformanceTests/Moniker.PerformanceTests.csproj
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,39 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<OutputType>Exe</OutputType> | ||
<GenerateProgramFile>false</GenerateProgramFile> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Content Include="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Compile Remove="TestResults\**" /> | ||
<EmbeddedResource Remove="TestResults\**" /> | ||
<None Remove="TestResults\**" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Compile Remove="BenchmarkDotNet.Artifacts\**" /> | ||
<EmbeddedResource Remove="BenchmarkDotNet.Artifacts\**" /> | ||
<None Remove="BenchmarkDotNet.Artifacts\**" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="BenchmarkDotNet" Version="0.14.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.0" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="xunit" Version="2.9.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Moniker\Moniker.csproj" /> | ||
</ItemGroup> | ||
</Project> |
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,12 @@ | ||
using BenchmarkDotNet.Attributes; | ||
|
||
namespace Moniker.PerformanceTests; | ||
|
||
public class NameGeneratorBenchmark | ||
{ | ||
[Benchmark] | ||
public string GenerateMoby() => NameGenerator.Generate(MonikerStyle.Moby); | ||
|
||
[Benchmark] | ||
public string GenerateMoniker() => NameGenerator.Generate(MonikerStyle.Moniker); | ||
} |
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,4 @@ | ||
using BenchmarkDotNet.Running; | ||
using Moniker.PerformanceTests; | ||
|
||
new BenchmarkSwitcher(Benchmarks.All).Run(args, new BenchmarkConfig()); |
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,3 @@ | ||
{ | ||
"shadowCopy": false | ||
} |