-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds ConsoleTest/Baseline for some heavier benchmarks (#2022)
This adds some console tests for performance work.
- Loading branch information
1 parent
a2a5ac6
commit f1a84f9
Showing
4 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |