Skip to content

Commit

Permalink
Adds ConsoleTest/Baseline for some heavier benchmarks (#2022)
Browse files Browse the repository at this point in the history
This adds some console tests for performance work.
  • Loading branch information
NickCraver authored Mar 6, 2022
1 parent a2a5ac6 commit f1a84f9
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 0 deletions.
14 changes: 14 additions & 0 deletions StackExchange.Redis.sln
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Docker", "Docker", "{A9F81D
tests\RedisConfigs\Docker\supervisord.conf = tests\RedisConfigs\Docker\supervisord.conf
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleTest", "tests\ConsoleTest\ConsoleTest.csproj", "{A0F89B8B-32A3-4C28-8F1B-ADE343F16137}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleTestBaseline", "tests\ConsoleTestBaseline\ConsoleTestBaseline.csproj", "{69A0ACF2-DF1F-4F49-B554-F732DCA938A3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -176,6 +180,14 @@ Global
{8FB98E7D-DAE2-4465-BD9A-104000E0A2D4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8FB98E7D-DAE2-4465-BD9A-104000E0A2D4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8FB98E7D-DAE2-4465-BD9A-104000E0A2D4}.Release|Any CPU.Build.0 = Release|Any CPU
{A0F89B8B-32A3-4C28-8F1B-ADE343F16137}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A0F89B8B-32A3-4C28-8F1B-ADE343F16137}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A0F89B8B-32A3-4C28-8F1B-ADE343F16137}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A0F89B8B-32A3-4C28-8F1B-ADE343F16137}.Release|Any CPU.Build.0 = Release|Any CPU
{69A0ACF2-DF1F-4F49-B554-F732DCA938A3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{69A0ACF2-DF1F-4F49-B554-F732DCA938A3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{69A0ACF2-DF1F-4F49-B554-F732DCA938A3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{69A0ACF2-DF1F-4F49-B554-F732DCA938A3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -196,6 +208,8 @@ Global
{153A10E4-E668-41AD-9E0F-6785CE7EED66} = {3AD17044-6BFF-4750-9AC2-2CA466375F2A}
{D58114AE-4998-4647-AFCA-9353D20495AE} = {E25031D3-5C64-430D-B86F-697B66816FD8}
{A9F81DA3-DA82-423E-A5DD-B11C37548E06} = {96E891CD-2ED7-4293-A7AB-4C6F5D8D2B05}
{A0F89B8B-32A3-4C28-8F1B-ADE343F16137} = {73A5C363-CA1F-44C4-9A9B-EF791A76BA6A}
{69A0ACF2-DF1F-4F49-B554-F732DCA938A3} = {73A5C363-CA1F-44C4-9A9B-EF791A76BA6A}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {193AA352-6748-47C1-A5FC-C9AA6B5F000B}
Expand Down
13 changes: 13 additions & 0 deletions tests/ConsoleTest/ConsoleTest.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

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

<ItemGroup>
<ProjectReference Include="..\..\src\StackExchange.Redis\StackExchange.Redis.csproj" />
</ItemGroup>
</Project>
122 changes: 122 additions & 0 deletions tests/ConsoleTest/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
using StackExchange.Redis;
using System.Diagnostics;
using System.Reflection;

Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();

var options = ConfigurationOptions.Parse("localhost");
//options.SocketManager = SocketManager.ThreadPool;
var connection = ConnectionMultiplexer.Connect(options);

var startTime = DateTime.UtcNow;
var startCpuUsage = Process.GetCurrentProcess().TotalProcessorTime;

var scenario = args?.Length > 0 ? args[0] : "parallel";

switch (scenario)
{
case "parallel":
Console.WriteLine("Parallel task test...");
ParallelTasks(connection);
break;
case "mass-insert":
Console.WriteLine("Mass insert test...");
MassInsert(connection);
break;
case "mass-publish":
Console.WriteLine("Mass publish test...");
MassPublish(connection);
break;
default:
Console.WriteLine("Scenario " + scenario + " is not recognized");
break;
}

stopwatch.Stop();

Console.WriteLine("");
Console.WriteLine($"Done. {stopwatch.ElapsedMilliseconds} ms");

var endTime = DateTime.UtcNow;
var endCpuUsage = Process.GetCurrentProcess().TotalProcessorTime;
var cpuUsedMs = (endCpuUsage - startCpuUsage).TotalMilliseconds;
var totalMsPassed = (endTime - startTime).TotalMilliseconds;
var cpuUsageTotal = cpuUsedMs / (Environment.ProcessorCount * totalMsPassed);
Console.WriteLine("Avg CPU: " + (cpuUsageTotal * 100));
Console.WriteLine("Lib Version: " + GetLibVersion());

static void MassInsert(ConnectionMultiplexer connection)
{
const int NUM_INSERTIONS = 100000;
int matchErrors = 0;

var database = connection.GetDatabase(0);

for (int i = 0; i < NUM_INSERTIONS; i++)
{
var key = $"StackExchange.Redis.Test.{i}";
var value = i.ToString();

database.StringSet(key, value);
var retrievedValue = database.StringGet(key);

if (retrievedValue != value)
{
matchErrors++;
}

if (i > 0 && i % 5000 == 0)
{
Console.WriteLine(i);
}
}

Console.WriteLine($"Match errors: {matchErrors}");
}

static void ParallelTasks(ConnectionMultiplexer connection)
{
static void ParallelRun(int taskId, ConnectionMultiplexer connection)
{
Console.Write($"{taskId} Started, ");
var database = connection.GetDatabase(0);

for (int i = 0; i < 100000; i++)
{
database.StringSet(i.ToString(), i.ToString());
}

Console.Write($"{taskId} Insert completed, ");

for (int i = 0; i < 100000; i++)
{
var result = database.StringGet(i.ToString());
}
Console.Write($"{taskId} Completed, ");
}

var taskList = new List<Task>();
for (int i = 0; i < 10; i++)
{
var i1 = i;
var task = new Task(() => ParallelRun(i1, connection));
task.Start();
taskList.Add(task);
}
Task.WaitAll(taskList.ToArray());
}

static void MassPublish(ConnectionMultiplexer connection)
{
var subscriber = connection.GetSubscriber();
Parallel.For(0, 1000, _ => subscriber.Publish("cache-events:cache-testing", "hey"));
}

static string GetLibVersion()
{
var assembly = typeof(ConnectionMultiplexer).Assembly;
return (Attribute.GetCustomAttribute(assembly, typeof(AssemblyFileVersionAttribute)) as AssemblyFileVersionAttribute)?.Version
?? assembly.GetName().Version?.ToString()
?? "Unknown";
}
19 changes: 19 additions & 0 deletions tests/ConsoleTestBaseline/ConsoleTestBaseline.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

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

<ItemGroup>
<Compile Include="..\ConsoleTest\Program.cs" Link="Program.cs" />
</ItemGroup>

<ItemGroup>
<!--<PackageReference Include="StackExchange.Redis" Version="1.2.6" />-->
<!--<PackageReference Include="StackExchange.Redis" Version="2.2.88" />-->
<PackageReference Include="StackExchange.Redis" Version="2.5.43" />
</ItemGroup>
</Project>

0 comments on commit f1a84f9

Please sign in to comment.