|
| 1 | +using System.Diagnostics; |
| 2 | +using StackExchange.Redis; |
| 3 | + |
| 4 | +#if !RELEASE |
| 5 | +Console.WriteLine("Warning: not running in release mode; results may be sub-optimal."); |
| 6 | +#endif |
| 7 | +using Benchmarker bench = new() |
| 8 | +{ |
| 9 | + // This is the operation (or operations) that we're benchmarking; you can change this to whatever you want to benchmark; |
| 10 | + // it should be representative of your real workload, in a database that is in a representative state (e.g. has the right indexes, |
| 11 | + // data volume, etc). |
| 12 | + Work = db => db.PingAsync(), |
| 13 | + // optional: set Connections, PipelineDepth, etc |
| 14 | + Clients = 100, |
| 15 | + PipelineDepth = 20, // not suitable for all workloads, note! for pure query workloads this might need to be 1 |
| 16 | +}; |
| 17 | +var count = bench.RepeatCount; |
| 18 | + |
| 19 | +Console.WriteLine($"Running {count} times, {bench.Clients} clients, {bench.IterationsPerClient} iterations each, pipeline depth {bench.PipelineDepth}"); |
| 20 | +for (int i = 0; i < count; i++) |
| 21 | +{ |
| 22 | + var watch = Stopwatch.StartNew(); |
| 23 | + await bench.RunAsync(); |
| 24 | + watch.Stop(); |
| 25 | + var opsPerSecond = (bench.IterationsPerClient * bench.Clients) / watch.Elapsed.TotalSeconds; |
| 26 | + Console.WriteLine($"Run {i + 1} of {count} completed in {watch.ElapsedMilliseconds}ms, {opsPerSecond:N0} ops/s"); |
| 27 | +} |
| 28 | + |
| 29 | +internal sealed class Benchmarker : IDisposable |
| 30 | +{ |
| 31 | + public int Connections { get; set; } = 1; |
| 32 | + public int IterationsPerClient { get; set; } = 10_000; |
| 33 | + public int PipelineDepth { get; set; } = 1; |
| 34 | + public int RepeatCount { get; set; } = 10; |
| 35 | + private int? _clients; |
| 36 | + public int Clients |
| 37 | + { |
| 38 | + get => _clients ?? Connections; |
| 39 | + set => _clients = value; |
| 40 | + } |
| 41 | + |
| 42 | + public required Func<IDatabase, Task> Work { get; set; } |
| 43 | + |
| 44 | + // optional run-once setup |
| 45 | + public Func<IDatabase, Task>? Init { get; set; } |
| 46 | + |
| 47 | + private IDatabase[] _conns = []; |
| 48 | + |
| 49 | + void IDisposable.Dispose() |
| 50 | + { |
| 51 | + foreach (var db in _conns) |
| 52 | + { |
| 53 | + db?.Multiplexer?.Dispose(); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + private async Task ConnectAllAsync() |
| 58 | + { |
| 59 | + Console.WriteLine($"Connecting..."); |
| 60 | + var arr = new IDatabase[Connections]; |
| 61 | + for (int i = 0; i < arr.Length; i++) |
| 62 | + { |
| 63 | + var db = arr[i] = await ConnectAsync(); |
| 64 | + var id = await db.ExecuteAsync("client", "id"); |
| 65 | + Console.WriteLine($"Client {i} connected, id: {id}"); |
| 66 | + } |
| 67 | + _conns = arr; |
| 68 | + if (Init is not null) |
| 69 | + { |
| 70 | + Console.WriteLine("Initializing..."); |
| 71 | + await Init(_conns[0]); |
| 72 | + Console.WriteLine("Initialized"); |
| 73 | + } |
| 74 | + } |
| 75 | + public async Task RunAsync() |
| 76 | + { |
| 77 | + // spin up the connections, if not already done |
| 78 | + if (_conns.Length is 0) await ConnectAllAsync(); |
| 79 | + |
| 80 | + Task[] clients = new Task[Clients]; |
| 81 | + for (int i = 0; i < clients.Length; i++) |
| 82 | + { |
| 83 | + // round-robin the connections to clients |
| 84 | + var db = _conns[i % _conns.Length]; // explicit local to avoid capture-context problems |
| 85 | + clients[i] = Task.Run(() => RunClientAsync(db)); // intentionally not awaited - concurrency |
| 86 | + } |
| 87 | + await Task.WhenAll(clients); |
| 88 | + } |
| 89 | + |
| 90 | + private async Task RunClientAsync(IDatabase db) |
| 91 | + { |
| 92 | + // run DoWorkAsync in a loop using a pipeline of depth PipelineDepth - just track the |
| 93 | + // last outstanding operation so we can await it when the pipe is full, or at the end |
| 94 | + int count = IterationsPerClient, maxDepth = PipelineDepth, depth = 0; |
| 95 | + var work = Work; |
| 96 | + Task? last = null; |
| 97 | + for (int i = 0; i < count; i++) |
| 98 | + { |
| 99 | + var pending = work(db); // intentionally not awaited - pipeline |
| 100 | + if (last is not null) |
| 101 | + { |
| 102 | + _ = last.ContinueWith(static t => GC.KeepAlive(t.Exception), TaskContinuationOptions.OnlyOnFaulted); |
| 103 | + } |
| 104 | + |
| 105 | + if (++depth >= maxDepth) |
| 106 | + { |
| 107 | + // pipeline is full; pause until the last one completes |
| 108 | + await pending; |
| 109 | + last = null; |
| 110 | + } |
| 111 | + else |
| 112 | + { |
| 113 | + // leave it outstanding |
| 114 | + last = pending; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + if (last is not null) |
| 119 | + { |
| 120 | + await last; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + private async Task<IDatabase> ConnectAsync() |
| 125 | + { |
| 126 | + var options = new ConfigurationOptions |
| 127 | + { |
| 128 | + EndPoints = { { "127.0.0.1", 6379 } }, |
| 129 | + TieBreaker = "", |
| 130 | + AllowAdmin = true, |
| 131 | + Protocol = RedisProtocol.Resp3, |
| 132 | + // turn off pub-sub |
| 133 | + CommandMap = CommandMap.Create(new HashSet<string>() { "SUBSCRIBE" }, available: false), |
| 134 | + ConfigurationChannel = "", |
| 135 | + }; |
| 136 | + var muxer = await ConnectionMultiplexer.ConnectAsync(options); |
| 137 | + return muxer.GetDatabase(); |
| 138 | + } |
| 139 | +} |
0 commit comments