Skip to content

Commit b995ebc

Browse files
NickCravermgravell
andauthored
Tests: Add benchmark suite for easily measuring improvements (#2931)
* Tests: Add benchmark suite for easily measuring improvements * - fix mono - add double parse - add double format * limit netfx to windows * tyop --------- Co-authored-by: Marc Gravell <[email protected]>
1 parent 45f7af4 commit b995ebc

File tree

9 files changed

+130
-1
lines changed

9 files changed

+130
-1
lines changed

StackExchange.Redis.sln

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1313
Directory.Build.props = Directory.Build.props
1414
Directory.Build.targets = Directory.Build.targets
1515
Directory.Packages.props = Directory.Packages.props
16+
tests\RedisConfigs\docker-compose.yml = tests\RedisConfigs\docker-compose.yml
1617
global.json = global.json
1718
NuGet.Config = NuGet.Config
1819
README.md = README.md
1920
docs\ReleaseNotes.md = docs\ReleaseNotes.md
2021
Shared.ruleset = Shared.ruleset
2122
version.json = version.json
22-
tests\RedisConfigs\docker-compose.yml = tests\RedisConfigs\docker-compose.yml
2323
EndProjectSection
2424
EndProject
2525
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "RedisConfigs", "RedisConfigs", "{96E891CD-2ED7-4293-A7AB-4C6F5D8D2B05}"
@@ -120,6 +120,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleTestBaseline", "test
120120
EndProject
121121
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "docs", "docs\docs.csproj", "{1DC43E76-5372-4C7F-A433-0602273E87FC}"
122122
EndProject
123+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StackExchange.Redis.Benchmarks", "tests\StackExchange.Redis.Benchmarks\StackExchange.Redis.Benchmarks.csproj", "{59889284-FFEE-82E7-94CB-3B43E87DA6CF}"
124+
EndProject
123125
Global
124126
GlobalSection(SolutionConfigurationPlatforms) = preSolution
125127
Debug|Any CPU = Debug|Any CPU
@@ -174,6 +176,10 @@ Global
174176
{1DC43E76-5372-4C7F-A433-0602273E87FC}.Debug|Any CPU.Build.0 = Debug|Any CPU
175177
{1DC43E76-5372-4C7F-A433-0602273E87FC}.Release|Any CPU.ActiveCfg = Release|Any CPU
176178
{1DC43E76-5372-4C7F-A433-0602273E87FC}.Release|Any CPU.Build.0 = Release|Any CPU
179+
{59889284-FFEE-82E7-94CB-3B43E87DA6CF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
180+
{59889284-FFEE-82E7-94CB-3B43E87DA6CF}.Debug|Any CPU.Build.0 = Debug|Any CPU
181+
{59889284-FFEE-82E7-94CB-3B43E87DA6CF}.Release|Any CPU.ActiveCfg = Release|Any CPU
182+
{59889284-FFEE-82E7-94CB-3B43E87DA6CF}.Release|Any CPU.Build.0 = Release|Any CPU
177183
EndGlobalSection
178184
GlobalSection(SolutionProperties) = preSolution
179185
HideSolutionNode = FALSE
@@ -195,6 +201,7 @@ Global
195201
{A9F81DA3-DA82-423E-A5DD-B11C37548E06} = {96E891CD-2ED7-4293-A7AB-4C6F5D8D2B05}
196202
{A0F89B8B-32A3-4C28-8F1B-ADE343F16137} = {73A5C363-CA1F-44C4-9A9B-EF791A76BA6A}
197203
{69A0ACF2-DF1F-4F49-B554-F732DCA938A3} = {73A5C363-CA1F-44C4-9A9B-EF791A76BA6A}
204+
{59889284-FFEE-82E7-94CB-3B43E87DA6CF} = {73A5C363-CA1F-44C4-9A9B-EF791A76BA6A}
198205
EndGlobalSection
199206
GlobalSection(ExtensibilityGlobals) = postSolution
200207
SolutionGuid = {193AA352-6748-47C1-A5FC-C9AA6B5F000B}

src/StackExchange.Redis/StackExchange.Redis.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
</ItemGroup>
4444

4545
<ItemGroup>
46+
<InternalsVisibleTo Include="StackExchange.Redis.Benchmarks" />
4647
<InternalsVisibleTo Include="StackExchange.Redis.Server" />
4748
<InternalsVisibleTo Include="StackExchange.Redis.Tests" />
4849
</ItemGroup>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System.Runtime.InteropServices;
2+
using BenchmarkDotNet.Columns;
3+
using BenchmarkDotNet.Configs;
4+
using BenchmarkDotNet.Diagnosers;
5+
using BenchmarkDotNet.Environments;
6+
using BenchmarkDotNet.Jobs;
7+
using BenchmarkDotNet.Validators;
8+
9+
namespace StackExchange.Redis.Benchmarks
10+
{
11+
internal class CustomConfig : ManualConfig
12+
{
13+
protected virtual Job Configure(Job j) => j;
14+
15+
public CustomConfig()
16+
{
17+
AddDiagnoser(MemoryDiagnoser.Default);
18+
AddColumn(StatisticColumn.OperationsPerSecond);
19+
AddValidator(JitOptimizationsValidator.FailOnError);
20+
21+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
22+
{
23+
AddJob(Configure(Job.Default.WithRuntime(ClrRuntime.Net481)));
24+
}
25+
AddJob(Configure(Job.Default.WithRuntime(CoreRuntime.Core80)));
26+
}
27+
}
28+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using System.Net;
3+
using BenchmarkDotNet.Attributes;
4+
5+
namespace StackExchange.Redis.Benchmarks
6+
{
7+
[Config(typeof(CustomConfig))]
8+
public class FormatBenchmarks
9+
{
10+
[GlobalSetup]
11+
public void Setup() { }
12+
13+
[Benchmark]
14+
[Arguments("64")]
15+
[Arguments("-1")]
16+
[Arguments("0")]
17+
[Arguments("123442")]
18+
public long ParseInt64(string s) => Format.ParseInt64(s);
19+
20+
[Benchmark]
21+
[Arguments("64")]
22+
[Arguments("-1")]
23+
[Arguments("0")]
24+
[Arguments("123442")]
25+
public long ParseInt32(string s) => Format.ParseInt32(s);
26+
27+
[Benchmark]
28+
[Arguments("64")]
29+
[Arguments("-1")]
30+
[Arguments("0")]
31+
[Arguments("123442")]
32+
[Arguments("-inf")]
33+
[Arguments("nan")]
34+
public double ParseDouble(string s) => Format.TryParseDouble(s, out var val) ? val : double.NaN;
35+
36+
private byte[] buffer = new byte[128];
37+
38+
[Benchmark]
39+
[Arguments(64D)]
40+
[Arguments(-1D)]
41+
[Arguments(0D)]
42+
[Arguments(123442D)]
43+
[Arguments(double.NegativeInfinity)]
44+
[Arguments(double.NaN)]
45+
public int FormatDouble(double value) => Format.FormatDouble(value, buffer.AsSpan());
46+
47+
[Benchmark]
48+
[Arguments("host.com", -1)]
49+
[Arguments("host.com", 0)]
50+
[Arguments("host.com", 65345)]
51+
public EndPoint ParseEndPoint(string host, int port) => Format.ParseEndPoint(host, port);
52+
}
53+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.Reflection;
2+
using BenchmarkDotNet.Running;
3+
4+
namespace StackExchange.Redis.Benchmarks
5+
{
6+
internal static class Program
7+
{
8+
private static void Main(string[] args) => BenchmarkSwitcher.FromAssembly(typeof(Program).GetTypeInfo().Assembly).Run(args);
9+
}
10+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using BenchmarkDotNet.Jobs;
2+
3+
namespace StackExchange.Redis.Benchmarks
4+
{
5+
internal class SlowConfig : CustomConfig
6+
{
7+
protected override Job Configure(Job j)
8+
=> j.WithLaunchCount(1)
9+
.WithWarmupCount(1)
10+
.WithIterationCount(5);
11+
}
12+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<Description>StackExchange.Redis MicroBenchmark Suite</Description>
4+
<TargetFrameworks>net481;net8.0</TargetFrameworks>
5+
<Configuration>Release</Configuration>
6+
<OutputType>Exe</OutputType>
7+
<SignAssembly>true</SignAssembly>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="BenchmarkDotNet" />
12+
13+
<ProjectReference Include="..\..\src\StackExchange.Redis\StackExchange.Redis.csproj" />
14+
</ItemGroup>
15+
</Project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dotnet run --framework net8.0 -c Release %*
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
dotnet run --framework net8.0 -c Release "$@"

0 commit comments

Comments
 (0)