|
1 | | -using StackExchange.Redis; |
2 | | - |
3 | | -// example server: |
4 | | -// docker run -d --name redis-tls -p 6379:3000 -p 6380:4430 -e TLS_ENABLED=yes -v ./my-tls:/redis/work/tls -e "TLS_CLIENT_CNS=MyUser1 MyUser2 MyUser3" redislabs/client-libs-test:custom-21183968220-debian-amd64 |
5 | | -// |
6 | | -// mimic: |
7 | | -// redis-cli |
8 | | -// -p 6380 |
9 | | -// --tls |
10 | | -// --cacert /home/marc/tls/my-tls/ca.crt |
11 | | -// --cert /home/marc/tls/my-tls/MyUser2.crt |
12 | | -// --key /home/marc/tls/my-tls/MyUser2.key client info |
13 | | -string certRoot = "/home/marc/tls/my-tls/"; |
14 | | -var config = ConfigurationOptions.Parse("localhost:6380"); |
15 | | -config.SetUserPemCertificate(// automatically enables TLS |
16 | | - userCertificatePath: Path.Combine(certRoot, "MyUser2.crt"), |
17 | | - userKeyPath: Path.Combine(certRoot, "MyUser2.key")); |
18 | | -config.TrustIssuer(Path.Combine(certRoot, "ca.crt")); |
19 | | - |
20 | | -await using var conn = await ConnectionMultiplexer.ConnectAsync(config, Console.Out); |
21 | | - |
22 | | -// prove we are connected as MyUser2 |
23 | | -var info = (string?)await conn.GetDatabase().ExecuteAsync("CLIENT", "INFO"); |
24 | | -Console.WriteLine(); |
25 | | -Console.WriteLine(info); |
| 1 | +using System.Diagnostics; |
| 2 | +using System.Reflection; |
| 3 | +using StackExchange.Redis; |
| 4 | + |
| 5 | +Stopwatch stopwatch = new Stopwatch(); |
| 6 | +stopwatch.Start(); |
| 7 | + |
| 8 | +var options = ConfigurationOptions.Parse("127.0.0.1"); |
| 9 | +#if !SEREDIS_BASELINE |
| 10 | +options.HighIntegrity = false; // as needed |
| 11 | +Console.WriteLine($"{nameof(options.HighIntegrity)}: {options.HighIntegrity}"); |
| 12 | +#endif |
| 13 | + |
| 14 | +// options.SocketManager = SocketManager.ThreadPool; |
| 15 | +Console.WriteLine("Connecting..."); |
| 16 | +var connection = ConnectionMultiplexer.Connect(options); |
| 17 | +Console.WriteLine("Connected"); |
| 18 | +connection.ConnectionFailed += Connection_ConnectionFailed; |
| 19 | + |
| 20 | +void Connection_ConnectionFailed(object? sender, ConnectionFailedEventArgs e) |
| 21 | +{ |
| 22 | + Console.Error.WriteLine($"CONNECTION FAILED: {e.ConnectionType}, {e.FailureType}, {e.Exception}"); |
| 23 | +} |
| 24 | + |
| 25 | +var startTime = DateTime.UtcNow; |
| 26 | +var startCpuUsage = Process.GetCurrentProcess().TotalProcessorTime; |
| 27 | + |
| 28 | +var scenario = args?.Length > 0 ? args[0] : "mass-insert-async"; |
| 29 | + |
| 30 | +switch (scenario) |
| 31 | +{ |
| 32 | + case "parallel": |
| 33 | + Console.WriteLine("Parallel task test..."); |
| 34 | + ParallelTasks(connection); |
| 35 | + break; |
| 36 | + case "mass-insert": |
| 37 | + Console.WriteLine("Mass insert test..."); |
| 38 | + MassInsert(connection); |
| 39 | + break; |
| 40 | + case "mass-insert-async": |
| 41 | + Console.WriteLine("Mass insert (async/pipelined) test..."); |
| 42 | + await MassInsertAsync(connection); |
| 43 | + break; |
| 44 | + case "mass-publish": |
| 45 | + Console.WriteLine("Mass publish test..."); |
| 46 | + MassPublish(connection); |
| 47 | + break; |
| 48 | + default: |
| 49 | + Console.WriteLine("Scenario " + scenario + " is not recognized"); |
| 50 | + break; |
| 51 | +} |
| 52 | + |
| 53 | +stopwatch.Stop(); |
| 54 | + |
| 55 | +Console.WriteLine(""); |
| 56 | +Console.WriteLine($"Done. {stopwatch.ElapsedMilliseconds} ms"); |
| 57 | + |
| 58 | +var endTime = DateTime.UtcNow; |
| 59 | +var endCpuUsage = Process.GetCurrentProcess().TotalProcessorTime; |
| 60 | +var cpuUsedMs = (endCpuUsage - startCpuUsage).TotalMilliseconds; |
| 61 | +var totalMsPassed = (endTime - startTime).TotalMilliseconds; |
| 62 | +var cpuUsageTotal = cpuUsedMs / (Environment.ProcessorCount * totalMsPassed); |
| 63 | +Console.WriteLine("Avg CPU: " + (cpuUsageTotal * 100)); |
| 64 | +Console.WriteLine("Lib Version: " + GetLibVersion()); |
| 65 | + |
| 66 | +static void MassInsert(ConnectionMultiplexer connection) |
| 67 | +{ |
| 68 | + const int NUM_INSERTIONS = 100_000; |
| 69 | + const int BATCH = 5000; |
| 70 | + int matchErrors = 0; |
| 71 | + |
| 72 | + var database = connection.GetDatabase(0); |
| 73 | + |
| 74 | + for (int i = 0; i < NUM_INSERTIONS; i++) |
| 75 | + { |
| 76 | + var key = $"StackExchange.Redis.Test.{i}"; |
| 77 | + var value = i.ToString(); |
| 78 | + |
| 79 | + database.StringSet(key, value); |
| 80 | + var retrievedValue = database.StringGet(key); |
| 81 | + |
| 82 | + if (retrievedValue != value) |
| 83 | + { |
| 84 | + matchErrors++; |
| 85 | + } |
| 86 | + |
| 87 | + if (i > 0 && i % BATCH == 0) |
| 88 | + { |
| 89 | + Console.WriteLine(i); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + Console.WriteLine($"Match errors: {matchErrors}"); |
| 94 | +} |
| 95 | + |
| 96 | +static async Task MassInsertAsync(ConnectionMultiplexer connection) |
| 97 | +{ |
| 98 | + const int NUM_INSERTIONS = 100_000; |
| 99 | + const int BATCH = 5000; |
| 100 | + int matchErrors = 0; |
| 101 | + |
| 102 | + var database = connection.GetDatabase(0); |
| 103 | + |
| 104 | + var outstanding = new List<(Task, Task<RedisValue>, string)>(BATCH); |
| 105 | + |
| 106 | + for (int i = 0; i < NUM_INSERTIONS; i++) |
| 107 | + { |
| 108 | + var key = $"StackExchange.Redis.Test.{i}"; |
| 109 | + var value = i.ToString(); |
| 110 | + |
| 111 | + var set = database.StringSetAsync(key, value); |
| 112 | + var get = database.StringGetAsync(key); |
| 113 | + |
| 114 | + outstanding.Add((set, get, value)); |
| 115 | + |
| 116 | + if (i > 0 && i % BATCH == 0) |
| 117 | + { |
| 118 | + matchErrors += await ValidateAsync(outstanding); |
| 119 | + Console.WriteLine(i); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + matchErrors += await ValidateAsync(outstanding); |
| 124 | + |
| 125 | + Console.WriteLine($"Match errors: {matchErrors}"); |
| 126 | + |
| 127 | + static async Task<int> ValidateAsync(List<(Task, Task<RedisValue>, string)> outstanding) |
| 128 | + { |
| 129 | + int matchErrors = 0; |
| 130 | + foreach (var row in outstanding) |
| 131 | + { |
| 132 | + var s = await row.Item2; |
| 133 | + await row.Item1; |
| 134 | + if (s != row.Item3) |
| 135 | + { |
| 136 | + matchErrors++; |
| 137 | + } |
| 138 | + } |
| 139 | + outstanding.Clear(); |
| 140 | + return matchErrors; |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +static void ParallelTasks(ConnectionMultiplexer connection) |
| 145 | +{ |
| 146 | + static void ParallelRun(int taskId, ConnectionMultiplexer connection) |
| 147 | + { |
| 148 | + Console.Write($"{taskId} Started, "); |
| 149 | + var database = connection.GetDatabase(0); |
| 150 | + |
| 151 | + for (int i = 0; i < 100000; i++) |
| 152 | + { |
| 153 | + database.StringSet(i.ToString(), i.ToString()); |
| 154 | + } |
| 155 | + |
| 156 | + Console.Write($"{taskId} Insert completed, "); |
| 157 | + |
| 158 | + for (int i = 0; i < 100000; i++) |
| 159 | + { |
| 160 | + var result = database.StringGet(i.ToString()); |
| 161 | + } |
| 162 | + Console.Write($"{taskId} Completed, "); |
| 163 | + } |
| 164 | + |
| 165 | + var taskList = new List<Task>(); |
| 166 | + for (int i = 0; i < 10; i++) |
| 167 | + { |
| 168 | + var i1 = i; |
| 169 | + var task = new Task(() => ParallelRun(i1, connection)); |
| 170 | + task.Start(); |
| 171 | + taskList.Add(task); |
| 172 | + } |
| 173 | + Task.WaitAll(taskList.ToArray()); |
| 174 | +} |
| 175 | + |
| 176 | +static void MassPublish(ConnectionMultiplexer connection) |
| 177 | +{ |
| 178 | + var subscriber = connection.GetSubscriber(); |
| 179 | + Parallel.For(0, 1000, _ => subscriber.Publish(new RedisChannel("cache-events:cache-testing", RedisChannel.PatternMode.Literal), "hey")); |
| 180 | +} |
| 181 | + |
| 182 | +static string GetLibVersion() |
| 183 | +{ |
| 184 | + var assembly = typeof(ConnectionMultiplexer).Assembly; |
| 185 | + return (Attribute.GetCustomAttribute(assembly, typeof(AssemblyFileVersionAttribute)) as AssemblyFileVersionAttribute)?.Version |
| 186 | + ?? assembly.GetName().Version?.ToString() |
| 187 | + ?? "Unknown"; |
| 188 | +} |
0 commit comments