add benchmark project

This commit is contained in:
Renjaya Raga Zenta 2025-07-31 00:35:43 +07:00
parent 1456503a3f
commit 72c644a37e
8 changed files with 198 additions and 2 deletions

1
.gitignore vendored
View file

@ -3,3 +3,4 @@ obj/
/packages/ /packages/
riderModule.iml riderModule.iml
/_ReSharper.Caches/ /_ReSharper.Caches/
*Artifacts/

View file

@ -0,0 +1,19 @@
<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.15.2" />
<PackageReference Include="NSubstitute" Version="5.3.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Oh.My.Stitcher\Oh.My.Stitcher.csproj" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,45 @@
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using NetVips;
using Oh.My.Stitcher.NetVips;
namespace Oh.My.Stitcher.Benchmark;
[SimpleJob(RuntimeMoniker.Net80)]
[MemoryDiagnoser]
public class OperationBenchmark
{
// CHANGE ME
private const string TILES_DIRECTORY = "/home/formulatrix/Downloads/tiles1705";
[Benchmark(Baseline = true)] // Mark this as the baseline for comparison
public void Standard_Operation()
{
const string rect = "A1:AE55";
if( !Tile.TryParseRect(rect, out int minRow, out int maxRow, out int minCol, out int maxCol) )
throw new ArgumentException($"Invalid canvas_rect: '{rect}'");
for( int row = minRow; row <= maxRow; row++ )
for( int col = minCol; col <= maxCol; col++ )
{
string path = Tile.FullPath(TILES_DIRECTORY, row, col);
using var i = ( Operation.Call("pngload", path) as Image )!;
}
}
[Benchmark]
public void Fast_Operation()
{
const string rect = "A1:AE55";
if( !Tile.TryParseRect(rect, out int minRow, out int maxRow, out int minCol, out int maxCol) )
throw new ArgumentException($"Invalid canvas_rect: '{rect}'");
Span<byte> pathBuffer = stackalloc byte[512];
for( int row = minRow; row <= maxRow; row++ )
for( int col = minCol; col <= maxCol; col++ )
{
int length = Tile.FullPathFast(TILES_DIRECTORY, row, col, pathBuffer);
using var i = ( OperationHacks.Call("pngload", pathBuffer.Slice(0, length + 1)) as Image )!;
}
}
}

View file

@ -0,0 +1,41 @@
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
namespace Oh.My.Stitcher.Benchmark;
[SimpleJob(RuntimeMoniker.Net80)]
[MemoryDiagnoser]
public class PathCreationBenchmark
{
// CHANGE ME
private const string TILES_DIRECTORY = "/home/formulatrix/Downloads/tiles1705";
[Benchmark(Baseline = true)] // Mark this as the baseline for comparison
public void Standard_PathCombine()
{
const string rect = "A1:AE55";
if( !Tile.TryParseRect(rect, out int minRow, out int maxRow, out int minCol, out int maxCol) )
throw new ArgumentException($"Invalid canvas_rect: '{rect}'");
for( int row = minRow; row <= maxRow; row++ )
for( int col = minCol; col <= maxCol; col++ )
{
Tile.FullPath(TILES_DIRECTORY, row, col);
}
}
[Benchmark]
public void Fast_PathCombine()
{
const string rect = "A1:AE55";
if( !Tile.TryParseRect(rect, out int minRow, out int maxRow, out int minCol, out int maxCol) )
throw new ArgumentException($"Invalid canvas_rect: '{rect}'");
Span<byte> pathBuffer = stackalloc byte[512];
for( int row = minRow; row <= maxRow; row++ )
for( int col = minCol; col <= maxCol; col++ )
{
Tile.FullPathFast(TILES_DIRECTORY, row, col, pathBuffer);
}
}
}

View file

@ -0,0 +1,13 @@
using BenchmarkDotNet.Running;
namespace Oh.My.Stitcher.Benchmark;
static class Program
{
static void Main(string[] _)
{
BenchmarkRunner.Run<PathCreationBenchmark>();
BenchmarkRunner.Run<OperationBenchmark>();
BenchmarkRunner.Run<UsageBenchmark>();
}
}

View file

@ -0,0 +1,62 @@
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using NetVips;
using NSubstitute;
namespace Oh.My.Stitcher.Benchmark;
[SimpleJob(RuntimeMoniker.Net80)]
[MemoryDiagnoser]
public class UsageBenchmark
{
// CHANGE ME
private const string TILES_DIRECTORY = "/home/formulatrix/Downloads/tiles1705";
[Benchmark(Baseline = true)] // Mark this as the baseline for comparison
public void Standard_Usage()
{
Stitch request = new()
{
CanvasRect = "A1:AE55", CropOffset = new CropOffset(0, 0), CropSize = new CropSize(1, 1), OutputScale = 1
};
Image? image = null;
List<Image> images = [];
IMemoryCache cache = Substitute.For<IMemoryCache>();
ILogger logger = Substitute.For<ILogger>();
try
{
Tile.TryCreate(in request, TILES_DIRECTORY, images, logger, cache, out image, out string? _, out string? _);
}
finally
{
image?.Dispose();
foreach( Image img in images )
img.Dispose();
}
}
[Benchmark]
public void Fast_Usage()
{
Stitch request = new()
{
CanvasRect = "A1:AE55", CropOffset = new CropOffset(0, 0), CropSize = new CropSize(1, 1), OutputScale = 1
};
Image? image = null;
List<Image> images = [];
IMemoryCache cache = Substitute.For<IMemoryCache>();
ILogger logger = Substitute.For<ILogger>();
try
{
Tile.TryCreateFast(in request, TILES_DIRECTORY, images, logger, cache, out image, out string? _, out string? _);
}
finally
{
image?.Dispose();
foreach( Image img in images )
img.Dispose();
}
}
}

View file

@ -4,6 +4,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{AEE9B1D3-6AD
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Oh.My.Stitcher", "src\Oh.My.Stitcher\Oh.My.Stitcher.csproj", "{9AB5F809-0D6A-4906-AB89-DC797FB7CF42}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Oh.My.Stitcher", "src\Oh.My.Stitcher\Oh.My.Stitcher.csproj", "{9AB5F809-0D6A-4906-AB89-DC797FB7CF42}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Oh.My.Stitcher.Benchmark", "src\Oh.My.Stitcher.Benchmark\Oh.My.Stitcher.Benchmark.csproj", "{68A9786C-87C0-488C-9DA4-29909671F4F0}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "vendor", "vendor", "{2F99FC95-31EA-42FD-BA0B-948B7DB33D86}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetVips", "vendor\NetVips\NetVips.csproj", "{A27A7D25-4601-45A5-ACA0-7DE44AE7FD33}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -12,12 +17,21 @@ Global
EndGlobalSection EndGlobalSection
GlobalSection(NestedProjects) = preSolution GlobalSection(NestedProjects) = preSolution
{9AB5F809-0D6A-4906-AB89-DC797FB7CF42} = {AEE9B1D3-6AD8-4EEE-800B-2873B0BB78DD} {9AB5F809-0D6A-4906-AB89-DC797FB7CF42} = {AEE9B1D3-6AD8-4EEE-800B-2873B0BB78DD}
{A9CC8F78-CB38-4986-9480-5FB4556F1356} = {AEE9B1D3-6AD8-4EEE-800B-2873B0BB78DD} {68A9786C-87C0-488C-9DA4-29909671F4F0} = {AEE9B1D3-6AD8-4EEE-800B-2873B0BB78DD}
{A27A7D25-4601-45A5-ACA0-7DE44AE7FD33} = {2F99FC95-31EA-42FD-BA0B-948B7DB33D86}
EndGlobalSection EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution GlobalSection(ProjectConfigurationPlatforms) = postSolution
{9AB5F809-0D6A-4906-AB89-DC797FB7CF42}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {9AB5F809-0D6A-4906-AB89-DC797FB7CF42}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9AB5F809-0D6A-4906-AB89-DC797FB7CF42}.Debug|Any CPU.Build.0 = Debug|Any CPU {9AB5F809-0D6A-4906-AB89-DC797FB7CF42}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9AB5F809-0D6A-4906-AB89-DC797FB7CF42}.Release|Any CPU.ActiveCfg = Release|Any CPU {9AB5F809-0D6A-4906-AB89-DC797FB7CF42}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9AB5F809-0D6A-4906-AB89-DC797FB7CF42}.Release|Any CPU.Build.0 = Release|Any CPU {9AB5F809-0D6A-4906-AB89-DC797FB7CF42}.Release|Any CPU.Build.0 = Release|Any CPU
{68A9786C-87C0-488C-9DA4-29909671F4F0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{68A9786C-87C0-488C-9DA4-29909671F4F0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{68A9786C-87C0-488C-9DA4-29909671F4F0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{68A9786C-87C0-488C-9DA4-29909671F4F0}.Release|Any CPU.Build.0 = Release|Any CPU
{A27A7D25-4601-45A5-ACA0-7DE44AE7FD33}.Debug|Any CPU.ActiveCfg = Debug|x64
{A27A7D25-4601-45A5-ACA0-7DE44AE7FD33}.Debug|Any CPU.Build.0 = Debug|x64
{A27A7D25-4601-45A5-ACA0-7DE44AE7FD33}.Release|Any CPU.ActiveCfg = Release|x64
{A27A7D25-4601-45A5-ACA0-7DE44AE7FD33}.Release|Any CPU.Build.0 = Release|x64
EndGlobalSection EndGlobalSection
EndGlobal EndGlobal

View file

@ -1,2 +1,3 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> <wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/UserDictionary/Words/=pngload/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Stitcher/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary> <s:Boolean x:Key="/Default/UserDictionary/Words/=Stitcher/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>