From 3a8c4e96e12bb94a2bda05a5862d1acda12a3acf Mon Sep 17 00:00:00 2001 From: Ian Qvist Date: Sat, 20 Jan 2024 02:09:13 +0100 Subject: [PATCH 1/2] Add benchmarks using BenchmarkDotNet It runs with all supported runtimes It uses memory diagnoser to check allocations --- Sqids.sln | 9 +++++++++ benchmark/Sqids.Benchmarks/EncodeBenchmark.cs | 19 ++++++++++++++++++ benchmark/Sqids.Benchmarks/Program.cs | 20 +++++++++++++++++++ .../Sqids.Benchmarks/Sqids.Benchmarks.csproj | 20 +++++++++++++++++++ 4 files changed, 68 insertions(+) create mode 100644 benchmark/Sqids.Benchmarks/EncodeBenchmark.cs create mode 100644 benchmark/Sqids.Benchmarks/Program.cs create mode 100644 benchmark/Sqids.Benchmarks/Sqids.Benchmarks.csproj diff --git a/Sqids.sln b/Sqids.sln index 9de449b..9e2b4e5 100644 --- a/Sqids.sln +++ b/Sqids.sln @@ -11,6 +11,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{5B69EBB5-A EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sqids.Tests", "test\Sqids.Tests\Sqids.Tests.csproj", "{26D42DEF-5A42-436C-8B80-44AA4917BFC1}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "benchmark", "benchmark", "{F165CB60-0269-4F53-8F6C-F61EF172947C}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sqids.Benchmarks", "benchmark\Sqids.Benchmarks\Sqids.Benchmarks.csproj", "{7892A604-1098-4088-A806-01787BE78521}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -28,9 +32,14 @@ Global {26D42DEF-5A42-436C-8B80-44AA4917BFC1}.Debug|Any CPU.Build.0 = Debug|Any CPU {26D42DEF-5A42-436C-8B80-44AA4917BFC1}.Release|Any CPU.ActiveCfg = Release|Any CPU {26D42DEF-5A42-436C-8B80-44AA4917BFC1}.Release|Any CPU.Build.0 = Release|Any CPU + {7892A604-1098-4088-A806-01787BE78521}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7892A604-1098-4088-A806-01787BE78521}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7892A604-1098-4088-A806-01787BE78521}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7892A604-1098-4088-A806-01787BE78521}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(NestedProjects) = preSolution {387B307E-04C6-4B8E-BE50-03FF91307070} = {FC64F776-DE51-4BFF-91D5-0ECFEEF5CCAC} {26D42DEF-5A42-436C-8B80-44AA4917BFC1} = {5B69EBB5-A05C-4DCB-9355-D010B0A093AE} + {7892A604-1098-4088-A806-01787BE78521} = {F165CB60-0269-4F53-8F6C-F61EF172947C} EndGlobalSection EndGlobal diff --git a/benchmark/Sqids.Benchmarks/EncodeBenchmark.cs b/benchmark/Sqids.Benchmarks/EncodeBenchmark.cs new file mode 100644 index 0000000..ecf5a1f --- /dev/null +++ b/benchmark/Sqids.Benchmarks/EncodeBenchmark.cs @@ -0,0 +1,19 @@ +using BenchmarkDotNet.Attributes; + +namespace Sqids.Benchmarks; + +[MemoryDiagnoser] +public class EncodeBenchmark +{ +#if NET7_0_OR_GREATER + private SqidsEncoder _encoder = new SqidsEncoder(); +#else + private SqidsEncoder _encoder = new SqidsEncoder(); +#endif + + [Benchmark] + public string Encode() => _encoder.Encode(42); + + [Benchmark] + public IReadOnlyList Decode() => _encoder.Decode("Jg"); +} diff --git a/benchmark/Sqids.Benchmarks/Program.cs b/benchmark/Sqids.Benchmarks/Program.cs new file mode 100644 index 0000000..43759a5 --- /dev/null +++ b/benchmark/Sqids.Benchmarks/Program.cs @@ -0,0 +1,20 @@ +using BenchmarkDotNet.Configs; +using BenchmarkDotNet.Environments; +using BenchmarkDotNet.Jobs; +using BenchmarkDotNet.Running; + +namespace Sqids.Benchmarks; + +public static class Program +{ + public static void Main(string[] args) + { + IConfig config = ManualConfig.CreateMinimumViable() + .AddJob(Job.Default.WithRuntime(ClrRuntime.Net472)) + .AddJob(Job.Default.WithRuntime(CoreRuntime.Core60)) + .AddJob(Job.Default.WithRuntime(CoreRuntime.Core70)) + .AddJob(Job.Default.WithRuntime(CoreRuntime.Core80)); + + BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args, config); + } +} diff --git a/benchmark/Sqids.Benchmarks/Sqids.Benchmarks.csproj b/benchmark/Sqids.Benchmarks/Sqids.Benchmarks.csproj new file mode 100644 index 0000000..f667236 --- /dev/null +++ b/benchmark/Sqids.Benchmarks/Sqids.Benchmarks.csproj @@ -0,0 +1,20 @@ + + + + + net472;net6.0;net7.0;net8.0 + 12.0 + enable + enable + exe + + + + + + + + + + + From 527af30b70cee379e3cae28459c5e484d6b9cbce Mon Sep 17 00:00:00 2001 From: Ian Qvist Date: Sat, 20 Jan 2024 20:17:13 +0100 Subject: [PATCH 2/2] Add more benchmark cases to catch performance edge-cases --- benchmark/Sqids.Benchmarks/EncodeBenchmark.cs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/benchmark/Sqids.Benchmarks/EncodeBenchmark.cs b/benchmark/Sqids.Benchmarks/EncodeBenchmark.cs index ecf5a1f..8bbf10d 100644 --- a/benchmark/Sqids.Benchmarks/EncodeBenchmark.cs +++ b/benchmark/Sqids.Benchmarks/EncodeBenchmark.cs @@ -12,8 +12,20 @@ public class EncodeBenchmark #endif [Benchmark] - public string Encode() => _encoder.Encode(42); + public string EncodeSmall() => _encoder.Encode(42); [Benchmark] - public IReadOnlyList Decode() => _encoder.Decode("Jg"); + public string EncodeBig() => _encoder.Encode(int.MaxValue); + + [Benchmark] + public string EncodeMany() => _encoder.Encode(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); + + [Benchmark] + public IReadOnlyList DecodeSmall() => _encoder.Decode("Jg"); + + [Benchmark] + public IReadOnlyList DecodeBig() => _encoder.Decode("UKrsQ1F"); + + [Benchmark] + public IReadOnlyList DecodeMany() => _encoder.Decode("hwB5vcCxfAyBnVKMtAaV"); }