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
{
public static async Task Main()
{
Thread.CurrentThread.Name = nameof(Main);
using (var conn = await ConnectionMultiplexer.ConnectAsync("127.0.0.1:6379,syncTimeout=2000"))
{
int expected = 0;
try static class Program
{
static void Main()
{ {
// 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 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;
Console.WriteLine(await s);
const int COUNT = 10000; const int COUNT = 10000;
/// <summary>
/// Run INCRBY lots of times
/// </summary>
[Benchmark(Description = "INCRBY", OperationsPerInvoke = COUNT)]
public int Execute()
{
var rand = new Random(12345); var rand = new Random(12345);
RedisKey counter = "counter"; RedisKey counter = "counter";
var watch = Stopwatch.StartNew();
db.KeyDelete(counter, CommandFlags.FireAndForget); db.KeyDelete(counter, CommandFlags.FireAndForget);
int expected = 0;
for (int i = 0; i < COUNT; i++) for (int i = 0; i < COUNT; i++)
{ {
int x = rand.Next(50); int x = rand.Next(50);
expected += x; expected += x;
db.StringIncrement(counter, x, CommandFlags.FireAndForget); db.StringIncrement(counter, x, CommandFlags.FireAndForget);
} }
int actual = (int)await db.StringGetAsync(counter); int actual = (int)db.StringGet(counter);
watch.Stop(); if (actual != expected) throw new InvalidOperationException(
Console.WriteLine($"{expected} vs {actual}, {watch.ElapsedMilliseconds}ms for {COUNT} incrby"); $"expected: {expected}, actual: {actual}");
} return actual;
catch (Exception ex)
{
Console.WriteLine($"expected when fail: {expected}");
Console.WriteLine(ex.Message);
}
}
} }
} }
} }
...@@ -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