diff --git a/Sqids.sln b/Sqids.sln index 8be0ec9..189fda5 100644 --- a/Sqids.sln +++ b/Sqids.sln @@ -12,6 +12,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sqids.Tests", "test\Sqids.T EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sqids.CodeGeneration", "src\Sqids.CodeGeneration\Sqids.CodeGeneration.csproj", "{04A8CCFC-1496-4F22-8399-922367E9260B}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sqids.Benchmarks", "benchmarks\Sqids.Benchmarks\Sqids.Benchmarks.csproj", "{44F4CB35-FD25-4D58-DB03-F1539F678FF2}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -30,6 +32,10 @@ Global {04A8CCFC-1496-4F22-8399-922367E9260B}.Debug|Any CPU.Build.0 = Debug|Any CPU {04A8CCFC-1496-4F22-8399-922367E9260B}.Release|Any CPU.ActiveCfg = Release|Any CPU {04A8CCFC-1496-4F22-8399-922367E9260B}.Release|Any CPU.Build.0 = Release|Any CPU + {44F4CB35-FD25-4D58-DB03-F1539F678FF2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {44F4CB35-FD25-4D58-DB03-F1539F678FF2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {44F4CB35-FD25-4D58-DB03-F1539F678FF2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {44F4CB35-FD25-4D58-DB03-F1539F678FF2}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/benchmarks/Sqids.Benchmarks/.editorconfig b/benchmarks/Sqids.Benchmarks/.editorconfig new file mode 100644 index 0000000..340c997 --- /dev/null +++ b/benchmarks/Sqids.Benchmarks/.editorconfig @@ -0,0 +1,2 @@ +[*.{cs,vb}] +dotnet_diagnostic.CA1050.severity = none \ No newline at end of file diff --git a/benchmarks/Sqids.Benchmarks/Program.cs b/benchmarks/Sqids.Benchmarks/Program.cs new file mode 100644 index 0000000..1fd2bc0 --- /dev/null +++ b/benchmarks/Sqids.Benchmarks/Program.cs @@ -0,0 +1,67 @@ +using BenchmarkDotNet.Attributes; +using BenchmarkDotNet.Configs; +using BenchmarkDotNet.Environments; +using BenchmarkDotNet.Jobs; +using BenchmarkDotNet.Running; + +BenchmarkRunner.Run(); + +// Define the Custom Config +public class PackageComparisonConfig : ManualConfig +{ + public PackageComparisonConfig() { + var packageVersions = new[] { "3.2.0", "3.1.0" }; + + var runtimes = new Runtime[] + { + ClrRuntime.Net48, + CoreRuntime.Core70, + CoreRuntime.Core80 + }; + + foreach (var version in packageVersions) { + foreach (var runtime in runtimes) { + AddJob(Job.Default + .WithRuntime(runtime) + .WithMsBuildArguments($"/p:BenchmarksPackageVersion={version}") + .WithId($"{runtime.Name} - v{version}")); + } + } + } +} + +[Config(typeof(PackageComparisonConfig))] +[MemoryDiagnoser] +public class SquidsBenchmarks +{ +#if NET7_0_OR_GREATER + private readonly Sqids.SqidsEncoder sqid = new(); +#else + private readonly Sqids.SqidsEncoder sqid = new(); +#endif + +#if WITH_LONG_SUPPORT + static readonly long longValue = (long)int.MaxValue; +#else + static readonly int intValue = int.MaxValue; +#endif + + static readonly string EncodedValue = "UKrsQ1F"; + + [Benchmark] + public string SqidEncode() { +#if WITH_LONG_SUPPORT + var encoded = sqid.Encode(longValue); +#else + var encoded = sqid.Encode(intValue); +#endif + return encoded; + } + + [Benchmark] + public void SqidDecode() { + var decoded = sqid.Decode(EncodedValue)[0]; + if (decoded != int.MaxValue) + throw new Exception("Decoded value does not match original"); + } +} \ No newline at end of file diff --git a/benchmarks/Sqids.Benchmarks/Sqids.Benchmarks.csproj b/benchmarks/Sqids.Benchmarks/Sqids.Benchmarks.csproj new file mode 100644 index 0000000..0ce2a42 --- /dev/null +++ b/benchmarks/Sqids.Benchmarks/Sqids.Benchmarks.csproj @@ -0,0 +1,31 @@ + + + + Exe + latest + net48;net7.0;net8.0 + true + enable + enable + + 3.2.0 + + + + $(DefineConstants);WITH_LONG_SUPPORT + + + + + + + + + + + + + + + +