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

convert main code to benchmarkdotnet

parent cad9dacd
......@@ -11,6 +11,10 @@
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.10.14" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\StackExchange.Redis\StackExchange.Redis.csproj" />
<!--<PackageReference Include="StackExchange.Redis" Version="1.2.7-alpha-00002" />-->
......
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Validators;
using StackExchange.Redis;
namespace BasicTest
{
internal static class Program
static class Program
{
public static async Task Main()
static void Main()
{
Thread.CurrentThread.Name = nameof(Main);
using (var conn = await ConnectionMultiplexer.ConnectAsync("127.0.0.1:6379,syncTimeout=2000"))
{
int expected = 0;
try
{
// tell BenchmarkDotNet not to force GC.Collect after benchmark iteration
// (single iteration contains of multiple (usually millions) of invocations)
// it can influence the allocation-heavy Task<T> benchmarks
var gcMode = new GcMode { Force = false };
conn.ConnectionFailed += (sender, e) => Console.WriteLine($"{e.ConnectionType}, {e.FailureType}: {e.Exception.Message}");
var db = conn.GetDatabase(3);
var customConfig = ManualConfig
.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 del = batch.KeyDeleteAsync("abc");
var set = batch.StringSetAsync("abc", "Does SE.Redis work on System.IO.Pipelines?");
var s = batch.StringGetAsync("abc");
batch.Execute();
var summary = BenchmarkRunner.Run<Benchmark>(customConfig);
Console.WriteLine(summary);
}
}
/// <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;
Console.WriteLine(await s);
const int COUNT = 10000;
const int COUNT = 10000;
var rand = new Random(12345);
RedisKey counter = "counter";
var watch = Stopwatch.StartNew();
db.KeyDelete(counter, CommandFlags.FireAndForget);
for (int i = 0; i < COUNT; i++)
{
int x = rand.Next(50);
expected += x;
db.StringIncrement(counter, x, CommandFlags.FireAndForget);
}
int actual = (int)await db.StringGetAsync(counter);
watch.Stop();
Console.WriteLine($"{expected} vs {actual}, {watch.ElapsedMilliseconds}ms for {COUNT} incrby");
}
catch (Exception ex)
{
Console.WriteLine($"expected when fail: {expected}");
Console.WriteLine(ex.Message);
}
/// <summary>
/// Run INCRBY lots of times
/// </summary>
[Benchmark(Description = "INCRBY", OperationsPerInvoke = COUNT)]
public int Execute()
{
var rand = new Random(12345);
RedisKey counter = "counter";
db.KeyDelete(counter, CommandFlags.FireAndForget);
int expected = 0;
for (int i = 0; i < COUNT; i++)
{
int x = rand.Next(50);
expected += x;
db.StringIncrement(counter, x, CommandFlags.FireAndForget);
}
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
var formattedEndpoint = Format.ToString(endpoint);
var t = SocketConnection.ConnectAsync(endpoint, PipeOptions,
//SocketConnectionOptions.SyncReader | SocketConnectionOptions.SyncWriter,
SocketConnectionOptions.None,
SocketConnectionOptions.SyncReader | SocketConnectionOptions.SyncWriter,
// SocketConnectionOptions.None,
onConnected: conn => EndConnectAsync(conn, multiplexer, log, callback),
socket: socket);
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