From 1b3dca6164b72e94e5b95117f3ead72b2104755a Mon Sep 17 00:00:00 2001 From: Padmanabh Gupta Date: Tue, 8 Oct 2024 18:26:44 +0530 Subject: [PATCH 1/7] test garnet benchmark --- .../GarnetClientSample.csproj | 1 + .../GarnetClientSample/GarnetClientSamples.cs | 41 ++++++++++--------- samples/GarnetClientSample/Program.cs | 33 +++++++++++++-- 3 files changed, 51 insertions(+), 24 deletions(-) diff --git a/samples/GarnetClientSample/GarnetClientSample.csproj b/samples/GarnetClientSample/GarnetClientSample.csproj index 720409f792..01efb6c39a 100644 --- a/samples/GarnetClientSample/GarnetClientSample.csproj +++ b/samples/GarnetClientSample/GarnetClientSample.csproj @@ -12,6 +12,7 @@ + diff --git a/samples/GarnetClientSample/GarnetClientSamples.cs b/samples/GarnetClientSample/GarnetClientSamples.cs index 19882557cb..5f4f26901c 100644 --- a/samples/GarnetClientSample/GarnetClientSamples.cs +++ b/samples/GarnetClientSample/GarnetClientSamples.cs @@ -19,27 +19,33 @@ public class GarnetClientSamples readonly string address; readonly int port; readonly bool useTLS; - + GarnetClient gc; public GarnetClientSamples(string address, int port, bool useTLS) { this.address = address; this.port = port; this.useTLS = useTLS; + + gc = new GarnetClient(address, port, GetSslOpts()); + } - public async Task RunAll() + public void RunAll() { - await PingAsync(); - await SetGetAsync(); + //await PingAsync(); + //await SetGetAsync(); + string origValue = "abcdefg"; + gc.StringSet("mykey", origValue, (c, s) => { if (s != "OK") throw new Exception("SetGetSync: Error"); }); + SetGetSync(); - await IncrAsync(); - await IncrByAsync(99); - await DecrByAsync(99); - await DecrAsync("test", 5); - await IncrNoKeyAsync(); - await ExistsAsync(); - await DeleteAsync(); - await SetGetMemoryAsync(); + //await IncrAsync(); + //await IncrByAsync(99); + //await DecrByAsync(99); + //await DecrAsync("test", 5); + //await IncrNoKeyAsync(); + //await ExistsAsync(); + //await DeleteAsync(); + //await SetGetMemoryAsync(); } async Task PingAsync() @@ -69,16 +75,11 @@ async Task SetGetAsync() void SetGetSync() { - using var db = new GarnetClient(address, port, GetSslOpts()); - db.Connect(); + - string origValue = "abcdefg"; - db.StringSet("mykey", origValue, (c, s) => { if (s != "OK") throw new Exception("SetGetSync: Error"); }); - ManualResetEventSlim e = new(); - db.StringGet("mykey", (c, s) => { if (s != origValue) throw new Exception("SetGetSync: Error"); e.Set(); }); - e.Wait(); - Console.WriteLine("SetGetSync: Success"); + gc.StringGet("mykey", (c, s) => { }); + //Console.WriteLine("SetGetSync: Success"); } async Task IncrAsync() diff --git a/samples/GarnetClientSample/Program.cs b/samples/GarnetClientSample/Program.cs index f5beac657d..abfcbaea36 100644 --- a/samples/GarnetClientSample/Program.cs +++ b/samples/GarnetClientSample/Program.cs @@ -1,7 +1,11 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. +using System.Collections.Generic; +using System.Net; using System.Threading.Tasks; +using BenchmarkDotNet.Attributes; +using BenchmarkDotNet.Running; namespace GarnetClientSample { @@ -10,15 +14,36 @@ namespace GarnetClientSample /// class Program { - static readonly string address = "127.0.0.1"; - static readonly int port = 6379; - static readonly bool useTLS = false; + static async Task Main() { - await new GarnetClientSamples(address, port, useTLS).RunAll(); + BenchmarkRunner.Run(); + // await new SERedisSamples(address, port).RunAll(); } + + } + public class SampleGet + { + static readonly string address = "127.0.0.1"; + static readonly int port = 6379; + static readonly bool useTLS = false; + GarnetClientSamples samples; + + [GlobalSetup] + public void Setup() + { + samples = new GarnetClientSamples(address, port, useTLS); + } + [Benchmark] + public void RunGet() + { + for (int i = 0; i < 100; i++) + { + samples.RunAll(); + } + } } } \ No newline at end of file From 43c5c131470755507b516173a22d2b046f484802 Mon Sep 17 00:00:00 2001 From: Padmanabh Gupta Date: Mon, 25 Nov 2024 23:33:41 +0530 Subject: [PATCH 2/7] Revert "test garnet benchmark" This reverts commit 1b3dca6164b72e94e5b95117f3ead72b2104755a. --- .../GarnetClientSample.csproj | 1 - .../GarnetClientSample/GarnetClientSamples.cs | 41 +++++++++---------- samples/GarnetClientSample/Program.cs | 33 ++------------- 3 files changed, 24 insertions(+), 51 deletions(-) diff --git a/samples/GarnetClientSample/GarnetClientSample.csproj b/samples/GarnetClientSample/GarnetClientSample.csproj index 01efb6c39a..720409f792 100644 --- a/samples/GarnetClientSample/GarnetClientSample.csproj +++ b/samples/GarnetClientSample/GarnetClientSample.csproj @@ -12,7 +12,6 @@ - diff --git a/samples/GarnetClientSample/GarnetClientSamples.cs b/samples/GarnetClientSample/GarnetClientSamples.cs index 5f4f26901c..19882557cb 100644 --- a/samples/GarnetClientSample/GarnetClientSamples.cs +++ b/samples/GarnetClientSample/GarnetClientSamples.cs @@ -19,33 +19,27 @@ public class GarnetClientSamples readonly string address; readonly int port; readonly bool useTLS; - GarnetClient gc; + public GarnetClientSamples(string address, int port, bool useTLS) { this.address = address; this.port = port; this.useTLS = useTLS; - - gc = new GarnetClient(address, port, GetSslOpts()); - } - public void RunAll() + public async Task RunAll() { - //await PingAsync(); - //await SetGetAsync(); - string origValue = "abcdefg"; - gc.StringSet("mykey", origValue, (c, s) => { if (s != "OK") throw new Exception("SetGetSync: Error"); }); - + await PingAsync(); + await SetGetAsync(); SetGetSync(); - //await IncrAsync(); - //await IncrByAsync(99); - //await DecrByAsync(99); - //await DecrAsync("test", 5); - //await IncrNoKeyAsync(); - //await ExistsAsync(); - //await DeleteAsync(); - //await SetGetMemoryAsync(); + await IncrAsync(); + await IncrByAsync(99); + await DecrByAsync(99); + await DecrAsync("test", 5); + await IncrNoKeyAsync(); + await ExistsAsync(); + await DeleteAsync(); + await SetGetMemoryAsync(); } async Task PingAsync() @@ -75,11 +69,16 @@ async Task SetGetAsync() void SetGetSync() { - + using var db = new GarnetClient(address, port, GetSslOpts()); + db.Connect(); + string origValue = "abcdefg"; + db.StringSet("mykey", origValue, (c, s) => { if (s != "OK") throw new Exception("SetGetSync: Error"); }); - gc.StringGet("mykey", (c, s) => { }); - //Console.WriteLine("SetGetSync: Success"); + ManualResetEventSlim e = new(); + db.StringGet("mykey", (c, s) => { if (s != origValue) throw new Exception("SetGetSync: Error"); e.Set(); }); + e.Wait(); + Console.WriteLine("SetGetSync: Success"); } async Task IncrAsync() diff --git a/samples/GarnetClientSample/Program.cs b/samples/GarnetClientSample/Program.cs index abfcbaea36..f5beac657d 100644 --- a/samples/GarnetClientSample/Program.cs +++ b/samples/GarnetClientSample/Program.cs @@ -1,11 +1,7 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. -using System.Collections.Generic; -using System.Net; using System.Threading.Tasks; -using BenchmarkDotNet.Attributes; -using BenchmarkDotNet.Running; namespace GarnetClientSample { @@ -13,37 +9,16 @@ namespace GarnetClientSample /// Use Garnet with GarnetClient and StackExchange.Redis clients /// class Program - { - - - static async Task Main() - { - BenchmarkRunner.Run(); - - - // await new SERedisSamples(address, port).RunAll(); - } - - } - public class SampleGet { static readonly string address = "127.0.0.1"; static readonly int port = 6379; static readonly bool useTLS = false; - GarnetClientSamples samples; - [GlobalSetup] - public void Setup() - { - samples = new GarnetClientSamples(address, port, useTLS); - } - [Benchmark] - public void RunGet() + static async Task Main() { - for (int i = 0; i < 100; i++) - { - samples.RunAll(); - } + await new GarnetClientSamples(address, port, useTLS).RunAll(); + + // await new SERedisSamples(address, port).RunAll(); } } } \ No newline at end of file From f3f35210eb613b7b9e7a10d6103e040615fe18ff Mon Sep 17 00:00:00 2001 From: Padmanabh Gupta Date: Mon, 25 Nov 2024 23:40:48 +0530 Subject: [PATCH 3/7] Fix race condition in disposing of metrics --- libs/server/Metrics/Latency/GarnetLatencyMetricsSession.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/libs/server/Metrics/Latency/GarnetLatencyMetricsSession.cs b/libs/server/Metrics/Latency/GarnetLatencyMetricsSession.cs index ca20dc4f28..5e16efd4ce 100644 --- a/libs/server/Metrics/Latency/GarnetLatencyMetricsSession.cs +++ b/libs/server/Metrics/Latency/GarnetLatencyMetricsSession.cs @@ -18,6 +18,8 @@ internal sealed class GarnetLatencyMetricsSession public int PriorVersion => 1 - Version; public LatencyMetricsEntrySession[] metrics; + private SingleWriterMultiReaderLock disposeLock; + public GarnetLatencyMetricsSession(GarnetServerMonitor monitor) { this.monitor = monitor; @@ -30,7 +32,9 @@ public void Return() { metrics[(int)cmd].Return(); } + disposeLock.WriteLock(); metrics = null; + disposeLock.WriteUnlock(); } private void Init() @@ -80,7 +84,9 @@ public void ResetAll() public void Reset(LatencyMetricsType cmd) { int idx = (int)cmd; + disposeLock.WriteLock(); metrics[idx].latency[PriorVersion].Reset(); + disposeLock.WriteUnlock(); } } } \ No newline at end of file From a7c0ade824ee5b398334c7e84e893f27d14a299f Mon Sep 17 00:00:00 2001 From: Padmanabh Gupta Date: Mon, 25 Nov 2024 23:42:23 +0530 Subject: [PATCH 4/7] check for null --- libs/server/Metrics/Latency/GarnetLatencyMetricsSession.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/libs/server/Metrics/Latency/GarnetLatencyMetricsSession.cs b/libs/server/Metrics/Latency/GarnetLatencyMetricsSession.cs index 5e16efd4ce..7e22d5a988 100644 --- a/libs/server/Metrics/Latency/GarnetLatencyMetricsSession.cs +++ b/libs/server/Metrics/Latency/GarnetLatencyMetricsSession.cs @@ -85,7 +85,10 @@ public void Reset(LatencyMetricsType cmd) { int idx = (int)cmd; disposeLock.WriteLock(); - metrics[idx].latency[PriorVersion].Reset(); + if(metrics != null) + { + metrics[idx].latency[PriorVersion].Reset(); + } disposeLock.WriteUnlock(); } } From 518a8b47295ea66bc6669996679c71c0263e8634 Mon Sep 17 00:00:00 2001 From: Padmanabh Gupta Date: Tue, 26 Nov 2024 10:08:54 +0530 Subject: [PATCH 5/7] fix spacing --- libs/server/Metrics/Latency/GarnetLatencyMetricsSession.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/server/Metrics/Latency/GarnetLatencyMetricsSession.cs b/libs/server/Metrics/Latency/GarnetLatencyMetricsSession.cs index 7e22d5a988..41cc104374 100644 --- a/libs/server/Metrics/Latency/GarnetLatencyMetricsSession.cs +++ b/libs/server/Metrics/Latency/GarnetLatencyMetricsSession.cs @@ -85,7 +85,7 @@ public void Reset(LatencyMetricsType cmd) { int idx = (int)cmd; disposeLock.WriteLock(); - if(metrics != null) + if (metrics != null) { metrics[idx].latency[PriorVersion].Reset(); } From afc901770d89e43357ff2b68291455008a4a997e Mon Sep 17 00:00:00 2001 From: Padmanabh Gupta Date: Wed, 27 Nov 2024 12:13:17 +0530 Subject: [PATCH 6/7] add try finally blocks --- .../Latency/GarnetLatencyMetricsSession.cs | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/libs/server/Metrics/Latency/GarnetLatencyMetricsSession.cs b/libs/server/Metrics/Latency/GarnetLatencyMetricsSession.cs index 41cc104374..e90eea0472 100644 --- a/libs/server/Metrics/Latency/GarnetLatencyMetricsSession.cs +++ b/libs/server/Metrics/Latency/GarnetLatencyMetricsSession.cs @@ -32,9 +32,15 @@ public void Return() { metrics[(int)cmd].Return(); } - disposeLock.WriteLock(); - metrics = null; - disposeLock.WriteUnlock(); + try + { + disposeLock.WriteLock(); + metrics = null; + } + finally + { + disposeLock.WriteUnlock(); + } } private void Init() @@ -84,12 +90,18 @@ public void ResetAll() public void Reset(LatencyMetricsType cmd) { int idx = (int)cmd; - disposeLock.WriteLock(); - if (metrics != null) + try + { + disposeLock.WriteLock(); + if (metrics != null) + { + metrics[idx].latency[PriorVersion].Reset(); + } + } + finally { - metrics[idx].latency[PriorVersion].Reset(); + disposeLock.WriteUnlock(); } - disposeLock.WriteUnlock(); } } } \ No newline at end of file From 3c2d820c06ac96b2d52e7c270aa1d446f787d5a9 Mon Sep 17 00:00:00 2001 From: Padmanabh Gupta Date: Wed, 27 Nov 2024 14:00:25 +0530 Subject: [PATCH 7/7] fix whitespace --- libs/server/Metrics/Latency/GarnetLatencyMetricsSession.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/server/Metrics/Latency/GarnetLatencyMetricsSession.cs b/libs/server/Metrics/Latency/GarnetLatencyMetricsSession.cs index e90eea0472..1d19ed6568 100644 --- a/libs/server/Metrics/Latency/GarnetLatencyMetricsSession.cs +++ b/libs/server/Metrics/Latency/GarnetLatencyMetricsSession.cs @@ -38,7 +38,7 @@ public void Return() metrics = null; } finally - { + { disposeLock.WriteUnlock(); } }