forked from StackExchange/StackExchange.Redis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPerformance.cs
96 lines (92 loc) · 3.95 KB
/
Performance.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System;
using System.Diagnostics;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using StackExchange.Redis;
namespace Tests
{
[TestFixture]
public class Performance
{
[Test]
public void VerifyPerformanceImprovement()
{
int asyncTimer, sync, op = 0, asyncFaF, syncFaF;
using (var muxer= Config.GetUnsecuredConnection())
{
// do these outside the timings, just to ensure the core methods are JITted etc
for (int db = 0; db < 5; db++)
{
muxer.GetDatabase(db).KeyDeleteAsync("perftest");
}
var timer = Stopwatch.StartNew();
for (int i = 0; i < 100; i++)
{
// want to test multiplex scenario; test each db, but to make it fair we'll
// do in batches of 10 on each
for (int db = 0; db < 5; db++)
{
var conn = muxer.GetDatabase(db);
for (int j = 0; j < 10; j++)
conn.StringIncrementAsync("perftest");
}
}
asyncFaF = (int)timer.ElapsedMilliseconds;
Task<RedisValue>[] final = new Task<RedisValue>[5];
for (int db = 0; db < 5; db++)
final[db] = muxer.GetDatabase(db).StringGetAsync("perftest");
muxer.WaitAll(final);
timer.Stop();
asyncTimer = (int)timer.ElapsedMilliseconds;
Console.WriteLine("async to completion (local): {0}ms", timer.ElapsedMilliseconds);
for (int db = 0; db < 5; db++)
Assert.AreEqual(1000, (long)final[db].Result, "async, db:" + db);
}
using (var conn = new Redis(Config.LocalHost, 6379))
{
// do these outside the timings, just to ensure the core methods are JITted etc
for (int db = 0; db < 5; db++)
{
conn.Db = db;
conn.Remove("perftest");
}
var timer = Stopwatch.StartNew();
for (int i = 0; i < 100; i++)
{
// want to test multiplex scenario; test each db, but to make it fair we'll
// do in batches of 10 on each
for (int db = 0; db < 5; db++)
{
conn.Db = db;
op++;
for (int j = 0; j < 10; j++)
{
conn.Increment("perftest");
op++;
}
}
}
syncFaF = (int)timer.ElapsedMilliseconds;
string[] final = new string[5];
for (int db = 0; db < 5; db++)
{
conn.Db = db;
final[db] = Encoding.ASCII.GetString(conn.Get("perftest"));
}
timer.Stop();
sync = (int)timer.ElapsedMilliseconds;
Console.WriteLine("sync to completion (local): {0}ms", timer.ElapsedMilliseconds);
for (int db = 0; db < 5; db++)
Assert.AreEqual("1000", final[db], "async, db:" + db);
}
int effectiveAsync = ((10 * asyncTimer) + 3) / 10;
int effectiveSync = ((10 * sync) + (op * 3)) / 10;
Console.WriteLine("async to completion with assumed 0.3ms LAN latency: " + effectiveAsync);
Console.WriteLine("sync to completion with assumed 0.3ms LAN latency: " + effectiveSync);
Console.WriteLine("fire-and-forget: {0}ms sync vs {1}ms async ", syncFaF, asyncFaF);
Assert.Less(effectiveAsync, effectiveSync, "Everything");
Assert.Less(asyncFaF, syncFaF, "Fire and Forget");
}
}
}