Commit 5ee8505b authored by Marc Gravell's avatar Marc Gravell

convert main code to benchmarkdotnet

parent cad9dacd
...@@ -11,6 +11,10 @@ ...@@ -11,6 +11,10 @@
<LangVersion>latest</LangVersion> <LangVersion>latest</LangVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.10.14" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\StackExchange.Redis\StackExchange.Redis.csproj" /> <ProjectReference Include="..\StackExchange.Redis\StackExchange.Redis.csproj" />
<!--<PackageReference Include="StackExchange.Redis" Version="1.2.7-alpha-00002" />--> <!--<PackageReference Include="StackExchange.Redis" Version="1.2.7-alpha-00002" />-->
......
using System; using System;
using System.Diagnostics; using BenchmarkDotNet.Attributes;
using System.Threading; using BenchmarkDotNet.Columns;
using System.Threading.Tasks; using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Validators;
using StackExchange.Redis; using StackExchange.Redis;
namespace BasicTest namespace BasicTest
{ {
internal static class Program
static class Program
{ {
public static async Task Main() static void Main()
{ {
Thread.CurrentThread.Name = nameof(Main); // tell BenchmarkDotNet not to force GC.Collect after benchmark iteration
using (var conn = await ConnectionMultiplexer.ConnectAsync("127.0.0.1:6379,syncTimeout=2000")) // (single iteration contains of multiple (usually millions) of invocations)
{ // it can influence the allocation-heavy Task<T> benchmarks
int expected = 0; var gcMode = new GcMode { Force = false };
try
{
conn.ConnectionFailed += (sender, e) => Console.WriteLine($"{e.ConnectionType}, {e.FailureType}: {e.Exception.Message}"); var customConfig = ManualConfig
var db = conn.GetDatabase(3); .Create(DefaultConfig.Instance) // copies all exporters, loggers and basic stuff
.With(JitOptimizationsValidator.FailOnError) // Fail if not release mode
.With(MemoryDiagnoser.Default) // use memory diagnoser
.With(StatisticColumn.OperationsPerSecond) // add ops/s
.With(Job.Default.With(gcMode));
var batch = db.CreateBatch(); var summary = BenchmarkRunner.Run<Benchmark>(customConfig);
var del = batch.KeyDeleteAsync("abc"); Console.WriteLine(summary);
var set = batch.StringSetAsync("abc", "Does SE.Redis work on System.IO.Pipelines?"); }
var s = batch.StringGetAsync("abc"); }
batch.Execute(); /// <summary>
/// The tests
/// </summary>
public class Benchmark : IDisposable
{
ConnectionMultiplexer connection;
IDatabase db;
/// <summary>
/// Create
/// </summary>
public Benchmark()
{
connection = ConnectionMultiplexer.Connect("127.0.0.1:6379,syncTimeout=200000");
db = connection.GetDatabase(3);
}
void IDisposable.Dispose()
{
connection?.Dispose();
db = null;
connection = null;
}
await del;
await set; const int COUNT = 10000;
Console.WriteLine(await s);
const int COUNT = 10000; /// <summary>
var rand = new Random(12345); /// Run INCRBY lots of times
RedisKey counter = "counter"; /// </summary>
var watch = Stopwatch.StartNew(); [Benchmark(Description = "INCRBY", OperationsPerInvoke = COUNT)]
db.KeyDelete(counter, CommandFlags.FireAndForget); public int Execute()
for (int i = 0; i < COUNT; i++) {
{ var rand = new Random(12345);
int x = rand.Next(50); RedisKey counter = "counter";
expected += x; db.KeyDelete(counter, CommandFlags.FireAndForget);
db.StringIncrement(counter, x, CommandFlags.FireAndForget); int expected = 0;
} for (int i = 0; i < COUNT; i++)
int actual = (int)await db.StringGetAsync(counter); {
watch.Stop(); int x = rand.Next(50);
Console.WriteLine($"{expected} vs {actual}, {watch.ElapsedMilliseconds}ms for {COUNT} incrby"); expected += x;
} db.StringIncrement(counter, x, CommandFlags.FireAndForget);
catch (Exception ex)
{
Console.WriteLine($"expected when fail: {expected}");
Console.WriteLine(ex.Message);
}
} }
int actual = (int)db.StringGet(counter);
if (actual != expected) throw new InvalidOperationException(
$"expected: {expected}, actual: {actual}");
return actual;
} }
} }
} }
...@@ -175,8 +175,8 @@ internal SocketToken BeginConnect(EndPoint endpoint, ISocketCallback callback, C ...@@ -175,8 +175,8 @@ internal SocketToken BeginConnect(EndPoint endpoint, ISocketCallback callback, C
var formattedEndpoint = Format.ToString(endpoint); var formattedEndpoint = Format.ToString(endpoint);
var t = SocketConnection.ConnectAsync(endpoint, PipeOptions, var t = SocketConnection.ConnectAsync(endpoint, PipeOptions,
//SocketConnectionOptions.SyncReader | SocketConnectionOptions.SyncWriter, SocketConnectionOptions.SyncReader | SocketConnectionOptions.SyncWriter,
SocketConnectionOptions.None, // SocketConnectionOptions.None,
onConnected: conn => EndConnectAsync(conn, multiplexer, log, callback), onConnected: conn => EndConnectAsync(conn, multiplexer, log, callback),
socket: socket); socket: socket);
GC.KeepAlive(t); // make compiler happier GC.KeepAlive(t); // make compiler happier
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment