forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRedisMqServerServerBenchmarks.cs
More file actions
192 lines (156 loc) · 5.7 KB
/
Copy pathRedisMqServerServerBenchmarks.cs
File metadata and controls
192 lines (156 loc) · 5.7 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using System;
using System.Diagnostics;
using System.Threading;
using NUnit.Framework;
using ServiceStack.Messaging.Redis;
using ServiceStack.Redis;
using ServiceStack.Text;
namespace ServiceStack.Server.Tests.Benchmarks
{
[Ignore]
[Explicit]
[TestFixture, Category("Benchmarks")]
public class RedisMqServerServerBenchmarks
{
public class Incr
{
public int Value { get; set; }
}
public class IncrBlocking
{
public int Value { get; set; }
}
private static RedisMqServer CreateMqHostServer()
{
var redisFactory = TestConfig.BasicClientManger;
try
{
redisFactory.Exec(redis => redis.FlushAll());
}
catch (RedisException rex)
{
Debug.WriteLine("WARNING: Redis not started? \n" + rex.Message);
}
var mqHost = new RedisMqServer(redisFactory);
return mqHost;
}
[Test]
public void Can_receive_and_process_same_reply_responses()
{
var mqHost = CreateMqHostServer();
var called = 0;
mqHost.RegisterHandler<Incr>(m =>
{
called++;
return new Incr { Value = m.GetBody().Value + 1 };
}, noOfThreads: 3);
mqHost.Start();
var mqClient = mqHost.CreateMessageQueueClient();
mqClient.Publish(new Incr { Value = 1 });
Thread.Sleep(10000);
Debug.WriteLine("Times called: " + called);
}
[Test]
public void Can_receive_and_process_same_reply_responses_blocking()
{
var mqHost = CreateMqHostServer();
var called = 0;
mqHost.RegisterHandler<IncrBlocking>(m =>
{
called++;
mqHost.CreateMessageQueueClient().Publish(new IncrBlocking { Value = m.GetBody().Value + 1 });
Thread.Sleep(100);
return null;
}, noOfThreads:5);
mqHost.Start();
var mqClient = mqHost.CreateMessageQueueClient();
mqClient.Publish(new IncrBlocking { Value = 1 });
Thread.Sleep(10000);
Debug.WriteLine("Times called: " + called);
}
[Test]
public void Can_receive_and_process_same_reply_responses_blocking_and_non_blocking()
{
var mqHost = CreateMqHostServer();
var nonBlocking = 0;
var blocking = 0;
mqHost.RegisterHandler<Incr>(m =>
{
nonBlocking++;
return new Incr { Value = m.GetBody().Value + 1 };
}, 1); //Non-blocking less no of threads the better
mqHost.RegisterHandler<IncrBlocking>(m =>
{
blocking++;
mqHost.CreateMessageQueueClient().Publish(new IncrBlocking { Value = m.GetBody().Value + 1 });
Thread.Sleep(100);
return null;
}, 5); //Blocking, more threads == better
mqHost.Start();
var mqClient = mqHost.CreateMessageQueueClient();
mqClient.Publish(new Incr { Value = 1 });
mqClient.Publish(new IncrBlocking { Value = 1 });
Thread.Sleep(10000);
Debug.WriteLine("Times called: non-blocking: {0}, blocking: {1}".Fmt(nonBlocking, blocking));
}
[Test]
public void Test_Blocking_messages_throughput()
{
var mqHost = CreateMqHostServer();
var blocking = 0;
const int BlockFor = 100;
const int NoOfThreads = 5;
const int SendEvery = BlockFor / NoOfThreads / 4;
mqHost.RegisterHandler<IncrBlocking>(m =>
{
blocking++;
Thread.Sleep(BlockFor);
return null;
}, NoOfThreads);
mqHost.Start();
var startedAt = DateTime.Now;
var mqClient = mqHost.CreateMessageQueueClient();
while (DateTime.Now - startedAt < TimeSpan.FromSeconds(10))
{
mqClient.Publish(new IncrBlocking { Value = 1 });
Thread.Sleep(SendEvery);
}
Debug.WriteLine("Times called: blocking: {0}".Fmt(blocking));
}
[Test]
public void Test_Blocking_and_NonBlocking_messages_throughput()
{
var mqHost = CreateMqHostServer();
var nonBlocking = 0;
var blocking = 0;
const int BlockFor = 100;
const int NoOfThreads = 5;
const int SendBlockingMsgEvery = BlockFor / NoOfThreads / 4;
mqHost.RegisterHandler<Incr>(m =>
{
nonBlocking++;
return null;
}, 3);
mqHost.RegisterHandler<IncrBlocking>(m =>
{
blocking++;
Thread.Sleep(BlockFor);
return null;
}, NoOfThreads);
mqHost.Start();
var mqClient = mqHost.CreateMessageQueueClient();
var stopWatch = Stopwatch.StartNew();
long lastBlockingSentAtMs = 0;
while (stopWatch.ElapsedMilliseconds < 10 * 1000)
{
mqClient.Publish(new Incr { Value = 1 });
while (stopWatch.ElapsedMilliseconds - lastBlockingSentAtMs > SendBlockingMsgEvery)
{
mqClient.Publish(new IncrBlocking { Value = 1 });
lastBlockingSentAtMs = stopWatch.ElapsedMilliseconds;
}
}
Debug.WriteLine("Times called: non-blocking: {0}, blocking: {1}".Fmt(nonBlocking, blocking));
}
}
}