Skip to content

Commit

Permalink
test: Add benchmark test for NameGenerator
Browse files Browse the repository at this point in the history
Return string in benchmarks

Test only two outer most methods
  • Loading branch information
alexmg committed Aug 29, 2024
1 parent 967ce61 commit 56a7e20
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Moniker.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
GitVersion.yml = GitVersion.yml
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Moniker.PerformanceTests", "test\Moniker.PerformanceTests\Moniker.PerformanceTests.csproj", "{FF324EAB-F8AE-4B7E-B8E0-07BD24B537DD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -35,6 +37,10 @@ Global
{F0740C5A-2F02-4A51-8737-1400CC2FFE58}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F0740C5A-2F02-4A51-8737-1400CC2FFE58}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F0740C5A-2F02-4A51-8737-1400CC2FFE58}.Release|Any CPU.Build.0 = Release|Any CPU
{FF324EAB-F8AE-4B7E-B8E0-07BD24B537DD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FF324EAB-F8AE-4B7E-B8E0-07BD24B537DD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FF324EAB-F8AE-4B7E-B8E0-07BD24B537DD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FF324EAB-F8AE-4B7E-B8E0-07BD24B537DD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
19 changes: 19 additions & 0 deletions test/Moniker.PerformanceTests/BenchmarkConfig.cs
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);
}
}
9 changes: 9 additions & 0 deletions test/Moniker.PerformanceTests/Benchmarks.cs
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)
];
}
21 changes: 21 additions & 0 deletions test/Moniker.PerformanceTests/Harness.cs
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 test/Moniker.PerformanceTests/Moniker.PerformanceTests.csproj
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>
12 changes: 12 additions & 0 deletions test/Moniker.PerformanceTests/NameGeneratorBenchmark.cs
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);
}
4 changes: 4 additions & 0 deletions test/Moniker.PerformanceTests/Program.cs
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());
3 changes: 3 additions & 0 deletions test/Moniker.PerformanceTests/xunit.runner.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"shadowCopy": false
}

0 comments on commit 56a7e20

Please sign in to comment.