Welcome to mirror list, hosted at ThFree Co, Russian Federation.

FromEvent.cs « Linq « Stress « Tests.System.Reactive « Source « NET « Rx - github.com/mono/rx.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f8677953cad79d738110d5d5f9b5fcd53cb4c043 (plain)
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.

#if STRESS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Reflection;
using System.Threading;

namespace ReactiveTests.Stress.Linq
{
    public class FromEvent
    {
        private static Lazy<Random> s_rand = new Lazy<Random>();

        /// <summary>
        /// Multiple threads are subscribing to a FromEventPattern sequence and disposing their subscriptions.
        /// While this is going on, one consumer does not want to be disturbed while receiving the sequence.
        /// 
        /// Runs a set of combinations of the RefCount_* tests.
        /// </summary>
        public static void RefCount_Mix()
        {
            Console.Title = MethodInfo.GetCurrentMethod().Name + " - 0% complete";

            for (int i = 1; i <= 100; i++)
            {
                var repeatCount = 10;

                foreach (var msgCount in new[] { 100, 1000, 10000, 100000 })
                {
                    // concurrency level {10, 20, ..., 100}
                    RefCount_ConcurrencyLevel_Linear(msgCount, repeatCount, 10, 100, 10);

                    // concurrency level {100, 200, ..., 1000}
                    RefCount_ConcurrencyLevel_Linear(msgCount, repeatCount, 100, 1000, 100);

                    // concurrency level {1, 2, 4, ..., 65536}
                    RefCount_ConcurrencyLevel_Exponential(msgCount, repeatCount, 1, 65536, 2);
                }

                foreach (var maxMsgCount in new[] { 10, 100, 1000, 10000, 100000 })
                {
                    foreach (var maxConcurrency in new[] { 10, 100, 1000, 10000, 100000 })
                    {
                        RefCount_Rand(repeatCount, maxMsgCount, maxConcurrency);
                    }
                }

                Console.Title = MethodInfo.GetCurrentMethod().Name + " - " + i + "% complete";
            }
        }

        /// <summary>
        /// Multiple threads are subscribing to a FromEventPattern sequence and disposing their subscriptions.
        /// While this is going on, one consumer does not want to be disturbed while receiving the sequence.
        /// Subscriptions are happening on the ThreadPool, possibly causing (expected) time gaps.
        /// 
        /// Runs a set of combinations of the RefCount_* tests.
        /// </summary>
        public static void RefCountWithPost_Mix()
        {
            Console.Title = MethodInfo.GetCurrentMethod().Name + " - 0% complete";

            for (int i = 1; i <= 100; i++)
            {
                var repeatCount = 10;

                foreach (var msgCount in new[] { 100, 1000, 10000, 100000 })
                {
                    // concurrency level {10, 20, ..., 100}
                    RefCountWithPost_ConcurrencyLevel_Linear(msgCount, repeatCount, 10, 100, 10);

                    // concurrency level {100, 200, ..., 1000}
                    RefCountWithPost_ConcurrencyLevel_Linear(msgCount, repeatCount, 100, 1000, 100);

                    // concurrency level {1, 2, 4, ..., 65536}
                    RefCountWithPost_ConcurrencyLevel_Exponential(msgCount, repeatCount, 1, 65536, 2);
                }

                foreach (var maxMsgCount in new[] { 10, 100, 1000, 10000, 100000 })
                {
                    foreach (var maxConcurrency in new[] { 10, 100, 1000, 10000, 100000 })
                    {
                        RefCountWithPost_Rand(repeatCount, maxMsgCount, maxConcurrency);
                    }
                }

                Console.Title = MethodInfo.GetCurrentMethod().Name + " - " + i + "% complete";
            }
        }

        /// <summary>
        /// Multiple threads are subscribing to a FromEventPattern sequence and disposing their subscriptions.
        /// While this is going on, one consumer does not want to be disturbed while receiving the sequence.
        /// 
        /// Uses random parameters for the number of messages and the level of concurrency.
        /// </summary>
        /// <param name="n">Number of iterations.</param>
        /// <param name="maxN">Maximum number of message.</param>
        /// <param name="maxM">Maximum level of concurrency.</param>
        public static void RefCount_Rand(int n, int maxN, int maxM)
        {
            RefCount_(RefCount_Rand_Params(n, maxN, maxM));
        }

        /// <summary>
        /// Multiple threads are subscribing to a FromEventPattern sequence and disposing their subscriptions.
        /// While this is going on, one consumer does not want to be disturbed while receiving the sequence.
        /// Subscriptions are happening on the ThreadPool, possibly causing (expected) time gaps.
        /// 
        /// Uses random parameters for the number of messages and the level of concurrency.
        /// </summary>
        /// <param name="n">Number of iterations.</param>
        /// <param name="maxN">Maximum number of message.</param>
        /// <param name="maxM">Maximum level of concurrency.</param>
        public static void RefCountWithPost_Rand(int n, int maxN, int maxM)
        {
            RefCountWithPost_(RefCount_Rand_Params(n, maxN, maxM));
        }

        private static IEnumerable<Tuple<int, int>> RefCount_Rand_Params(int n, int maxN, int maxM)
        {
            for (int i = 0; i < n; i++)
            {
                var N = s_rand.Value.Next(1, maxN);
                var M = s_rand.Value.Next(1, maxM);

                yield return new Tuple<int, int>(N, M);
            }
        }

        /// <summary>
        /// Multiple threads are subscribing to a FromEventPattern sequence and disposing their subscriptions.
        /// While this is going on, one consumer does not want to be disturbed while receiving the sequence.
        /// 
        /// Uses linear increments for the concurrency level.
        /// </summary>
        /// <param name="N">Number of messages.</param>
        /// <param name="n">Number of iterations.</param>
        /// <param name="min">Minimum level of concurrency.</param>
        /// <param name="max">Maximum level of concurrency.</param>
        /// <param name="step">Additive step size to increase level of concurrency.</param>
        public static void RefCount_ConcurrencyLevel_Linear(int N, int n, int min, int max, int step)
        {
            RefCount_(RefCount_ConcurrencyLevel_Linear_Params(N, n, min, max, step));
        }

        /// <summary>
        /// Multiple threads are subscribing to a FromEventPattern sequence and disposing their subscriptions.
        /// While this is going on, one consumer does not want to be disturbed while receiving the sequence.
        /// Subscriptions are happening on the ThreadPool, possibly causing (expected) time gaps.
        /// 
        /// Uses linear increments for the concurrency level.
        /// </summary>
        /// <param name="N">Number of messages.</param>
        /// <param name="n">Number of iterations.</param>
        /// <param name="min">Minimum level of concurrency.</param>
        /// <param name="max">Maximum level of concurrency.</param>
        /// <param name="step">Additive step size to increase level of concurrency.</param>
        public static void RefCountWithPost_ConcurrencyLevel_Linear(int N, int n, int min, int max, int step)
        {
            RefCountWithPost_(RefCount_ConcurrencyLevel_Linear_Params(N, n, min, max, step));
        }

        private static IEnumerable<Tuple<int, int>> RefCount_ConcurrencyLevel_Linear_Params(int N, int n, int min, int max, int step)
        {
            for (int i = 0; i < n; i++)
            {
                for (int M = min; M <= max; M += step)
                {
                    yield return new Tuple<int, int>(N, M);
                }
            }
        }

        /// <summary>
        /// Multiple threads are subscribing to a FromEventPattern sequence and disposing their subscriptions.
        /// While this is going on, one consumer does not want to be disturbed while receiving the sequence.
        /// 
        /// Uses exponential increments for the concurrency level.
        /// </summary>
        /// <param name="N">Number of messages.</param>
        /// <param name="n">Number of iterations.</param>
        /// <param name="min">Minimum level of concurrency.</param>
        /// <param name="max">Maximum level of concurrency.</param>
        /// <param name="step">Multiplicative step size to increase level of concurrency.</param>
        public static void RefCount_ConcurrencyLevel_Exponential(int N, int n, int min, int max, int step)
        {
            RefCount_(RefCount_ConcurrencyLevel_Exponential_Params(N, n, min, max, step));
        }

        /// <summary>
        /// Multiple threads are subscribing to a FromEventPattern sequence and disposing their subscriptions.
        /// While this is going on, one consumer does not want to be disturbed while receiving the sequence.
        /// Subscriptions are happening on the ThreadPool, possibly causing (expected) time gaps.
        /// 
        /// Uses exponential increments for the concurrency level.
        /// </summary>
        /// <param name="N">Number of messages.</param>
        /// <param name="n">Number of iterations.</param>
        /// <param name="min">Minimum level of concurrency.</param>
        /// <param name="max">Maximum level of concurrency.</param>
        /// <param name="step">Multiplicative step size to increase level of concurrency.</param>
        public static void RefCountWithPost_ConcurrencyLevel_Exponential(int N, int n, int min, int max, int step)
        {
            RefCountWithPost_(RefCount_ConcurrencyLevel_Exponential_Params(N, n, min, max, step));
        }

        private static IEnumerable<Tuple<int, int>> RefCount_ConcurrencyLevel_Exponential_Params(int N, int n, int min, int max, int step)
        {
            for (int i = 0; i < n; i++)
            {
                for (int M = min; M <= max; M *= step)
                {
                    yield return new Tuple<int, int>(N, M);
                }
            }
        }

        private static void RefCount_(IEnumerable<Tuple<int, int>> parameters)
        {
            foreach (var p in parameters)
            {
                var N = p.Item1;
                var M = p.Item2;

                Console.Write("N = {0}, M = {1} - ", N, M);

                var bar = new Bar();

                var foo = Observable.FromEventPattern<FooEventArgs>(h => { Console.Write("+"); bar.Foo += h; }, h => { bar.Foo -= h; Console.Write("-"); });

                var res = new List<int>();
                var n = 0;
                var e = new ManualResetEvent(false);

                var cd = new CountdownEvent(M * 2);
                for (int i = 0; i < M; i++)
                {
                    var f = new SingleAssignmentDisposable();

                    ThreadPool.QueueUserWorkItem(_ =>
                    {
                        f.Disposable = foo.Subscribe(__ => { Console.Write("!"); });
                        cd.Signal();
                    });

                    ThreadPool.QueueUserWorkItem(_ =>
                    {
                        f.Dispose();
                        cd.Signal();
                    });
                }

                Console.Write("{SB}");

                var d = foo.Subscribe(x =>
                {
                    //Console.Write("&");

                    if (++n == N)
                        e.Set();

                    res.Add(x.EventArgs.Qux);
                });

                Console.Write("{SE}");

                var t = new Thread(() =>
                {
                    Console.Write("{TB}");

                    for (int i = 0; i < N; i++)
                        bar.OnFoo(i);

                    Console.Write("{TE}");
                });

                t.Start();
                t.Join();

                cd.Wait();

                e.WaitOne();
                d.Dispose();

                if (!res.SequenceEqual(Enumerable.Range(0, N)))
                {
                    Console.WriteLine("Panic!");
                    break;
                }

                Console.WriteLine(".");
            }
        }

        private static void RefCountWithPost_(IEnumerable<Tuple<int, int>> parameters)
        {
            var worker = new Thread(() =>
            {
                SynchronizationContext.SetSynchronizationContext(new MySyncCtx());

                foreach (var p in parameters)
                {
                    var N = p.Item1;
                    var M = p.Item2;

                    Console.Write("N = {0}, M = {1} - ", N, M);

                    var bar = new Bar();

                    var foo = Observable.FromEventPattern<FooEventArgs>(h => { /*Console.Write("+");*/ bar.Foo += h; }, h => { bar.Foo -= h; /*Console.Write("-"); */});

                    var e = new ManualResetEvent(false);

                    var cd = new CountdownEvent(M * 2);
                    for (int i = 0; i < M; i++)
                    {
                        var f = new SingleAssignmentDisposable();

                        ThreadPool.QueueUserWorkItem(_ =>
                        {
                            f.Disposable = foo.Subscribe(__ => { /*Console.Write("!");*/ });
                            cd.Signal();
                        });

                        ThreadPool.QueueUserWorkItem(_ =>
                        {
                            f.Dispose();
                            cd.Signal();
                        });
                    }

                    var hasObserved = 0;

                    Console.Write("{SB}");

                    var d = foo.Subscribe(x =>
                    {
                        //
                        // [on BARTDE-M6500 with CPU and RAM pressure]
                        //
                        // Up to 8K concurrent observers, we typically don't see a time gap (expected worst-case behavior).
                        // The code below uses an event to check the desired behavior of eventually tuning in to the event stream.
                        //
                        Console.Write("&" + x.EventArgs.Qux);
                        e.Set();
                        Interlocked.Exchange(ref hasObserved, 1);
                    });

                    Console.Write("{SE}");

                    var t = new Thread(() =>
                    {
                        Console.Write("{TB}");

                        var i = 0;
                        while (Thread.VolatileRead(ref hasObserved) == 0)
                            bar.OnFoo(i++);

                        Console.Write("{TE}");
                    });

                    t.Start();
                    t.Join();

                    cd.Wait();

                    e.WaitOne();
                    d.Dispose();

                    Console.WriteLine(".");
                }
            });

            worker.Start();
            worker.Join();
        }

        class Bar
        {
            public event EventHandler<FooEventArgs> Foo;

            public void OnFoo(int x)
            {
                var foo = Foo;
                if (foo != null)
                    foo(this, new FooEventArgs { Qux = x });
            }
        }

        class FooEventArgs : EventArgs
        {
            public int Qux { get; set; }
        }

        class MySyncCtx : SynchronizationContext
        {
            public override void Post(SendOrPostCallback d, object state)
            {
                ThreadPool.QueueUserWorkItem(_ =>
                {
                    d(state);
                });
            }
        }
    }
}
#endif