Debian Codegraph @

external/rx/Rx/NET/Source/Tests.System.Reactive/Stress/Linq/FromEvent.cs

raw
1 // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
2
3 #if STRESS
4 using System;
5 using System.Collections.Generic;
6 using System.Linq;
7 using System.Reactive.Disposables;
8 using System.Reactive.Linq;
9 using System.Reflection;
10 using System.Threading;
11
12 namespace ReactiveTests.Stress.Linq
13 {
14 public class FromEvent
15 {
16 private static Lazy<Random> s_rand = new Lazy<Random>();
17
18 /// <summary>
19 /// Multiple threads are subscribing to a FromEventPattern sequence and disposing their subscriptions.
20 /// While this is going on, one consumer does not want to be disturbed while receiving the sequence.
21 ///
22 /// Runs a set of combinations of the RefCount_* tests.
23 /// </summary>
24 public static void RefCount_Mix()
25 {
26 Console.Title = MethodInfo.GetCurrentMethod().Name + " - 0% complete";
27
28 for (int i = 1; i <= 100; i++)
29 {
30 var repeatCount = 10;
31
32 foreach (var msgCount in new[] { 100, 1000, 10000, 100000 })
33 {
34 // concurrency level {10, 20, ..., 100}
35 RefCount_ConcurrencyLevel_Linear(msgCount, repeatCount, 10, 100, 10);
36
37 // concurrency level {100, 200, ..., 1000}
38 RefCount_ConcurrencyLevel_Linear(msgCount, repeatCount, 100, 1000, 100);
39
40 // concurrency level {1, 2, 4, ..., 65536}
41 RefCount_ConcurrencyLevel_Exponential(msgCount, repeatCount, 1, 65536, 2);
42 }
43
44 foreach (var maxMsgCount in new[] { 10, 100, 1000, 10000, 100000 })
45 {
46 foreach (var maxConcurrency in new[] { 10, 100, 1000, 10000, 100000 })
47 {
48 RefCount_Rand(repeatCount, maxMsgCount, maxConcurrency);
49 }
50 }
51
52 Console.Title = MethodInfo.GetCurrentMethod().Name + " - " + i + "% complete";
53 }
54 }
55
56 /// <summary>
57 /// Multiple threads are subscribing to a FromEventPattern sequence and disposing their subscriptions.
58 /// While this is going on, one consumer does not want to be disturbed while receiving the sequence.
59 /// Subscriptions are happening on the ThreadPool, possibly causing (expected) time gaps.
60 ///
61 /// Runs a set of combinations of the RefCount_* tests.
62 /// </summary>
63 public static void RefCountWithPost_Mix()
64 {
65 Console.Title = MethodInfo.GetCurrentMethod().Name + " - 0% complete";
66
67 for (int i = 1; i <= 100; i++)
68 {
69 var repeatCount = 10;
70
71 foreach (var msgCount in new[] { 100, 1000, 10000, 100000 })
72 {
73 // concurrency level {10, 20, ..., 100}
74 RefCountWithPost_ConcurrencyLevel_Linear(msgCount, repeatCount, 10, 100, 10);
75
76 // concurrency level {100, 200, ..., 1000}
77 RefCountWithPost_ConcurrencyLevel_Linear(msgCount, repeatCount, 100, 1000, 100);
78
79 // concurrency level {1, 2, 4, ..., 65536}
80 RefCountWithPost_ConcurrencyLevel_Exponential(msgCount, repeatCount, 1, 65536, 2);
81 }
82
83 foreach (var maxMsgCount in new[] { 10, 100, 1000, 10000, 100000 })
84 {
85 foreach (var maxConcurrency in new[] { 10, 100, 1000, 10000, 100000 })
86 {
87 RefCountWithPost_Rand(repeatCount, maxMsgCount, maxConcurrency);
88 }
89 }
90
91 Console.Title = MethodInfo.GetCurrentMethod().Name + " - " + i + "% complete";
92 }
93 }
94
95 /// <summary>
96 /// Multiple threads are subscribing to a FromEventPattern sequence and disposing their subscriptions.
97 /// While this is going on, one consumer does not want to be disturbed while receiving the sequence.
98 ///
99 /// Uses random parameters for the number of messages and the level of concurrency.
100 /// </summary>
101 /// <param name="n">Number of iterations.</param>
102 /// <param name="maxN">Maximum number of message.</param>
103 /// <param name="maxM">Maximum level of concurrency.</param>
104 public static void RefCount_Rand(int n, int maxN, int maxM)
105 {
106 RefCount_(RefCount_Rand_Params(n, maxN, maxM));
107 }
108
109 /// <summary>
110 /// Multiple threads are subscribing to a FromEventPattern sequence and disposing their subscriptions.
111 /// While this is going on, one consumer does not want to be disturbed while receiving the sequence.
112 /// Subscriptions are happening on the ThreadPool, possibly causing (expected) time gaps.
113 ///
114 /// Uses random parameters for the number of messages and the level of concurrency.
115 /// </summary>
116 /// <param name="n">Number of iterations.</param>
117 /// <param name="maxN">Maximum number of message.</param>
118 /// <param name="maxM">Maximum level of concurrency.</param>
119 public static void RefCountWithPost_Rand(int n, int maxN, int maxM)
120 {
121 RefCountWithPost_(RefCount_Rand_Params(n, maxN, maxM));
122 }
123
124 private static IEnumerable<Tuple<int, int>> RefCount_Rand_Params(int n, int maxN, int maxM)
125 {
126 for (int i = 0; i < n; i++)
127 {
128 var N = s_rand.Value.Next(1, maxN);
129 var M = s_rand.Value.Next(1, maxM);
130
131 yield return new Tuple<int, int>(N, M);
132 }
133 }
134
135 /// <summary>
136 /// Multiple threads are subscribing to a FromEventPattern sequence and disposing their subscriptions.
137 /// While this is going on, one consumer does not want to be disturbed while receiving the sequence.
138 ///
139 /// Uses linear increments for the concurrency level.
140 /// </summary>
141 /// <param name="N">Number of messages.</param>
142 /// <param name="n">Number of iterations.</param>
143 /// <param name="min">Minimum level of concurrency.</param>
144 /// <param name="max">Maximum level of concurrency.</param>
145 /// <param name="step">Additive step size to increase level of concurrency.</param>
146 public static void RefCount_ConcurrencyLevel_Linear(int N, int n, int min, int max, int step)
147 {
148 RefCount_(RefCount_ConcurrencyLevel_Linear_Params(N, n, min, max, step));
149 }
150
151 /// <summary>
152 /// Multiple threads are subscribing to a FromEventPattern sequence and disposing their subscriptions.
153 /// While this is going on, one consumer does not want to be disturbed while receiving the sequence.
154 /// Subscriptions are happening on the ThreadPool, possibly causing (expected) time gaps.
155 ///
156 /// Uses linear increments for the concurrency level.
157 /// </summary>
158 /// <param name="N">Number of messages.</param>
159 /// <param name="n">Number of iterations.</param>
160 /// <param name="min">Minimum level of concurrency.</param>
161 /// <param name="max">Maximum level of concurrency.</param>
162 /// <param name="step">Additive step size to increase level of concurrency.</param>
163 public static void RefCountWithPost_ConcurrencyLevel_Linear(int N, int n, int min, int max, int step)
164 {
165 RefCountWithPost_(RefCount_ConcurrencyLevel_Linear_Params(N, n, min, max, step));
166 }
167
168 private static IEnumerable<Tuple<int, int>> RefCount_ConcurrencyLevel_Linear_Params(int N, int n, int min, int max, int step)
169 {
170 for (int i = 0; i < n; i++)
171 {
172 for (int M = min; M <= max; M += step)
173 {
174 yield return new Tuple<int, int>(N, M);
175 }
176 }
177 }
178
179 /// <summary>
180 /// Multiple threads are subscribing to a FromEventPattern sequence and disposing their subscriptions.
181 /// While this is going on, one consumer does not want to be disturbed while receiving the sequence.
182 ///
183 /// Uses exponential increments for the concurrency level.
184 /// </summary>
185 /// <param name="N">Number of messages.</param>
186 /// <param name="n">Number of iterations.</param>
187 /// <param name="min">Minimum level of concurrency.</param>
188 /// <param name="max">Maximum level of concurrency.</param>
189 /// <param name="step">Multiplicative step size to increase level of concurrency.</param>
190 public static void RefCount_ConcurrencyLevel_Exponential(int N, int n, int min, int max, int step)
191 {
192 RefCount_(RefCount_ConcurrencyLevel_Exponential_Params(N, n, min, max, step));
193 }
194
195 /// <summary>
196 /// Multiple threads are subscribing to a FromEventPattern sequence and disposing their subscriptions.
197 /// While this is going on, one consumer does not want to be disturbed while receiving the sequence.
198 /// Subscriptions are happening on the ThreadPool, possibly causing (expected) time gaps.
199 ///
200 /// Uses exponential increments for the concurrency level.
201 /// </summary>
202 /// <param name="N">Number of messages.</param>
203 /// <param name="n">Number of iterations.</param>
204 /// <param name="min">Minimum level of concurrency.</param>
205 /// <param name="max">Maximum level of concurrency.</param>
206 /// <param name="step">Multiplicative step size to increase level of concurrency.</param>
207 public static void RefCountWithPost_ConcurrencyLevel_Exponential(int N, int n, int min, int max, int step)
208 {
209 RefCountWithPost_(RefCount_ConcurrencyLevel_Exponential_Params(N, n, min, max, step));
210 }
211
212 private static IEnumerable<Tuple<int, int>> RefCount_ConcurrencyLevel_Exponential_Params(int N, int n, int min, int max, int step)
213 {
214 for (int i = 0; i < n; i++)
215 {
216 for (int M = min; M <= max; M *= step)
217 {
218 yield return new Tuple<int, int>(N, M);
219 }
220 }
221 }
222
223 private static void RefCount_(IEnumerable<Tuple<int, int>> parameters)
224 {
225 foreach (var p in parameters)
226 {
227 var N = p.Item1;
228 var M = p.Item2;
229
230 Console.Write("N = {0}, M = {1} - ", N, M);
231
232 var bar = new Bar();
233
234 var foo = Observable.FromEventPattern<FooEventArgs>(h => { Console.Write("+"); bar.Foo += h; }, h => { bar.Foo -= h; Console.Write("-"); });
235
236 var res = new List<int>();
237 var n = 0;
238 var e = new ManualResetEvent(false);
239
240 var cd = new CountdownEvent(M * 2);
241 for (int i = 0; i < M; i++)
242 {
243 var f = new SingleAssignmentDisposable();
244
245 ThreadPool.QueueUserWorkItem(_ =>
246 {
247 f.Disposable = foo.Subscribe(__ => { Console.Write("!"); });
248 cd.Signal();
249 });
250
251 ThreadPool.QueueUserWorkItem(_ =>
252 {
253 f.Dispose();
254 cd.Signal();
255 });
256 }
257
258 Console.Write("{SB}");
259
260 var d = foo.Subscribe(x =>
261 {
262 //Console.Write("&");
263
264 if (++n == N)
265 e.Set();
266
267 res.Add(x.EventArgs.Qux);
268 });
269
270 Console.Write("{SE}");
271
272 var t = new Thread(() =>
273 {
274 Console.Write("{TB}");
275
276 for (int i = 0; i < N; i++)
277 bar.OnFoo(i);
278
279 Console.Write("{TE}");
280 });
281
282 t.Start();
283 t.Join();
284
285 cd.Wait();
286
287 e.WaitOne();
288 d.Dispose();
289
290 if (!res.SequenceEqual(Enumerable.Range(0, N)))
291 {
292 Console.WriteLine("Panic!");
293 break;
294 }
295
296 Console.WriteLine(".");
297 }
298 }
299
300 private static void RefCountWithPost_(IEnumerable<Tuple<int, int>> parameters)
301 {
302 var worker = new Thread(() =>
303 {
304 SynchronizationContext.SetSynchronizationContext(new MySyncCtx());
305
306 foreach (var p in parameters)
307 {
308 var N = p.Item1;
309 var M = p.Item2;
310
311 Console.Write("N = {0}, M = {1} - ", N, M);
312
313 var bar = new Bar();
314
315 var foo = Observable.FromEventPattern<FooEventArgs>(h => { /*Console.Write("+");*/ bar.Foo += h; }, h => { bar.Foo -= h; /*Console.Write("-"); */});
316
317 var e = new ManualResetEvent(false);
318
319 var cd = new CountdownEvent(M * 2);
320 for (int i = 0; i < M; i++)
321 {
322 var f = new SingleAssignmentDisposable();
323
324 ThreadPool.QueueUserWorkItem(_ =>
325 {
326 f.Disposable = foo.Subscribe(__ => { /*Console.Write("!");*/ });
327 cd.Signal();
328 });
329
330 ThreadPool.QueueUserWorkItem(_ =>
331 {
332 f.Dispose();
333 cd.Signal();
334 });
335 }
336
337 var hasObserved = 0;
338
339 Console.Write("{SB}");
340
341 var d = foo.Subscribe(x =>
342 {
343 //
344 // [on BARTDE-M6500 with CPU and RAM pressure]
345 //
346 // Up to 8K concurrent observers, we typically don't see a time gap (expected worst-case behavior).
347 // The code below uses an event to check the desired behavior of eventually tuning in to the event stream.
348 //
349 Console.Write("&" + x.EventArgs.Qux);
350 e.Set();
351 Interlocked.Exchange(ref hasObserved, 1);
352 });
353
354 Console.Write("{SE}");
355
356 var t = new Thread(() =>
357 {
358 Console.Write("{TB}");
359
360 var i = 0;
361 while (Thread.VolatileRead(ref hasObserved) == 0)
362 bar.OnFoo(i++);
363
364 Console.Write("{TE}");
365 });
366
367 t.Start();
368 t.Join();
369
370 cd.Wait();
371
372 e.WaitOne();
373 d.Dispose();
374
375 Console.WriteLine(".");
376 }
377 });
378
379 worker.Start();
380 worker.Join();
381 }
382
383 class Bar
384 {
385 public event EventHandler<FooEventArgs> Foo;
386
387 public void OnFoo(int x)
388 {
389 var foo = Foo;
390 if (foo != null)
391 foo(this, new FooEventArgs { Qux = x });
392 }
393 }
394
395 class FooEventArgs : EventArgs
396 {
397 public int Qux { get; set; }
398 }
399
400 class MySyncCtx : SynchronizationContext
401 {
402 public override void Post(SendOrPostCallback d, object state)
403 {
404 ThreadPool.QueueUserWorkItem(_ =>
405 {
406 d(state);
407 });
408 }
409 }
410 }
411 }
412 #endif