Skip to content

Commit

Permalink
Added BenchmarkDotNet to easily run performance comparisons (#5553)
Browse files Browse the repository at this point in the history
that don't require Godot usage that will hopefully in the future help with deciding on different code approaches to certain algorithms
  • Loading branch information
hhyyrylainen authored Oct 2, 2024
1 parent 2ed87d7 commit c63b112
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export.cfg
/duplicate_results.xml
/.jetbrains-cache
/.attach_pid*
BenchmarkDotNet.Artifacts

# Seems really like this is needed for CI
# export_presets.cfg
Expand Down
2 changes: 1 addition & 1 deletion Thrive.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<!-- Ignore the subproject source files -->
<PropertyGroup>
<DefaultItemExcludes>
$(DefaultItemExcludes);Scripts\**\*;ThriveScriptsShared\**\*;test\code_tests\**\*;RevolutionaryGamesCommon\**\*;third_party\**\*
$(DefaultItemExcludes);Scripts\**\*;ThriveScriptsShared\**\*;test\code_tests\**\*;test\code_benchmarks\**\*;RevolutionaryGamesCommon\**\*;third_party\**\*
</DefaultItemExcludes>
</PropertyGroup>
<!-- Non-Steam build needs to skip this file -->
Expand Down
8 changes: 8 additions & 0 deletions Thrive.sln
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LauncherThriveShared", "Rev
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ThriveScriptsShared", "ThriveScriptsShared\ThriveScriptsShared.csproj", "{CE809922-0763-48C7-9037-4FDCEC542AC7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CodeBenchmark", "test\code_benchmarks\CodeBenchmark.csproj", "{8D9390BE-EB42-468C-80D7-BBB7951FD9E3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -79,6 +81,12 @@ Global
{CE809922-0763-48C7-9037-4FDCEC542AC7}.ExportDebug|Any CPU.Build.0 = Debug|Any CPU
{CE809922-0763-48C7-9037-4FDCEC542AC7}.ExportRelease|Any CPU.ActiveCfg = Debug|Any CPU
{CE809922-0763-48C7-9037-4FDCEC542AC7}.ExportRelease|Any CPU.Build.0 = Debug|Any CPU
{8D9390BE-EB42-468C-80D7-BBB7951FD9E3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8D9390BE-EB42-468C-80D7-BBB7951FD9E3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8D9390BE-EB42-468C-80D7-BBB7951FD9E3}.ExportDebug|Any CPU.ActiveCfg = Debug|Any CPU
{8D9390BE-EB42-468C-80D7-BBB7951FD9E3}.ExportDebug|Any CPU.Build.0 = Debug|Any CPU
{8D9390BE-EB42-468C-80D7-BBB7951FD9E3}.ExportRelease|Any CPU.ActiveCfg = Debug|Any CPU
{8D9390BE-EB42-468C-80D7-BBB7951FD9E3}.ExportRelease|Any CPU.Build.0 = Debug|Any CPU
EndGlobalSection
GlobalSection(MonoDevelopProperties) = preSolution
Policies = $0
Expand Down
14 changes: 14 additions & 0 deletions test/code_benchmarks/CodeBenchmark.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.14.0" />
</ItemGroup>

</Project>
48 changes: 48 additions & 0 deletions test/code_benchmarks/ComparerBenchmark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
namespace CodeBenchmark;

using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;

[SimpleJob(RuntimeMoniker.Net80)]
[MemoryDiagnoser]
public class ComparerBenchmark
{
[Params(1000, 10000)]
public int N;

private ValueTuple<int, int>[] data = [];

[GlobalSetup]
public void Setup()
{
data = new ValueTuple<int, int>[N];
var random = new Random(42);

for (int i = 0; i < data.Length; ++i)
{
data[i] = new ValueTuple<int, int>(random.Next(), random.Next());
}
}

[Benchmark]
public void GenericComparer()
{
var comparer = Comparer<ValueTuple<int, int>>.Default;

for (int i = 1; i < data.Length; ++i)
{
_ = comparer.Compare(data[i - 1], data[i]);
}
}

[Benchmark]
public void EqualityComparer()
{
var comparer = EqualityComparer<ValueTuple<int, int>>.Default;

for (int i = 1; i < data.Length; ++i)
{
_ = comparer.Equals(data[i - 1], data[i]);
}
}
}
4 changes: 4 additions & 0 deletions test/code_benchmarks/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
using BenchmarkDotNet.Running;
using CodeBenchmark;

_ = BenchmarkRunner.Run<ComparerBenchmark>();
2 changes: 2 additions & 0 deletions test/code_benchmarks/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/sh
dotnet run -c Release

0 comments on commit c63b112

Please sign in to comment.