Debian Codegraph @

external/rx/Rx/NET/Source/Tests.System.Reactive/Stress/Helpers.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.Diagnostics;
7 using System.Threading;
8 using System.Linq;
9
10 namespace ReactiveTests.Stress
11 {
12 public static class Helpers
13 {
14 public static void RunWithMemoryPressure(int minBytes, int maxBytes, double activePercent, Action a)
15 {
16 var started = new ManualResetEvent(false);
17 var stopped = 0;
18
19 var avg = (minBytes + maxBytes) / 2;
20 var MIN = avg / 1000;
21 var MAX = avg / 10;
22
23 var allocator = new Thread(() =>
24 {
25 var rand = new Random();
26 var roots = new List<byte[]>();
27 var bytes = 0;
28
29 while (bytes < avg)
30 {
31 var n = rand.Next(MIN, MAX);
32 bytes += n;
33 roots.Add(new byte[n]);
34 }
35
36 started.Set();
37
38 long avgSum = 0;
39 long count = 0;
40
41 const int DEC = 0;
42 const int INC = 1;
43
44 var trendPhase = 0;
45 var trendLength = 0;
46
47 while (Thread.VolatileRead(ref stopped) == 0)
48 {
49 if (trendLength-- == 0)
50 {
51 trendPhase = rand.Next(0, 1000) % 2;
52 trendLength = rand.Next(1, 10);
53 }
54
55 var mem = TimeSpan.Zero;
56
57 var sw = Stopwatch.StartNew();
58
59 var busy = new Stopwatch();
60
61 while (Thread.VolatileRead(ref stopped) == 0 && (double)mem.Ticks / (double)sw.ElapsedTicks < activePercent)
62 {
63 busy.Restart();
64
65 var runFor = rand.Next(10, 100);
66 while (busy.ElapsedMilliseconds < runFor)
67 {
68 if (trendPhase == INC)
69 {
70 if (bytes < maxBytes)
71 {
72 var n = rand.Next(MIN, MAX);
73 bytes += n;
74 roots.Add(new byte[n]);
75 continue;
76 }
77 else
78 {
79 trendPhase = DEC;
80 }
81 }
82
83 if (trendPhase == DEC)
84 {
85 if (bytes > minBytes)
86 {
87 if (roots.Count > 0)
88 {
89 var i = rand.Next(0, roots.Count);
90 bytes -= roots[i].Length;
91 roots.RemoveAt(i);
92 }
93 continue;
94 }
95 else
96 {
97 trendPhase = INC;
98 }
99 }
100 }
101
102 mem += busy.Elapsed;
103 }
104
105 var sleepFor = rand.Next(100, 1000);
106 Thread.Sleep(sleepFor);
107
108 avgSum += bytes;
109 count++;
110 //Console.WriteLine(bytes + " - Avg = " + avgSum / count);
111 }
112 });
113
114 allocator.Start();
115 started.WaitOne();
116
117 try
118 {
119 a();
120 }
121 finally
122 {
123 Interlocked.Exchange(ref stopped, 1);
124 allocator.Join();
125 }
126 }
127
128 public static void RunWithProcessorPressure(int threadCount, int lockCount, double activePercent, double lockChancePercent, Action a)
129 {
130 var stopped = 0;
131 var started = new CountdownEvent(threadCount);
132
133 var ts = new Thread[threadCount];
134 var locks = Enumerable.Range(0, lockCount).Select(_ => new object()).ToArray();
135
136 for (int i = 0; i < threadCount; i++)
137 {
138 var id = i;
139
140 var t = ts[i] = new Thread(() =>
141 {
142 var rand = new Random();
143
144 started.Signal();
145
146 var sw = Stopwatch.StartNew();
147 var run = TimeSpan.Zero;
148
149 while (Thread.VolatileRead(ref stopped) == 0)
150 {
151 var busy = new Stopwatch();
152
153 while (Thread.VolatileRead(ref stopped) == 0 && (double)run.Ticks / (double)sw.ElapsedTicks < activePercent)
154 {
155 busy.Restart();
156
157 const int RUN = 0;
158 const int BLOCK = 1;
159
160 var action = lockCount > 0 && rand.Next() % 100 <= lockChancePercent * 100 ? BLOCK : RUN;
161
162 switch (action)
163 {
164 case RUN:
165 //Console.WriteLine("~" + id);
166 while (busy.ElapsedMilliseconds < 10)
167 ;
168 break;
169 case BLOCK:
170 //Console.WriteLine("!" + id);
171 lock (locks[rand.Next(0, lockCount)])
172 Thread.Sleep(rand.Next(100, 1000));
173 break;
174 }
175
176 run += busy.Elapsed;
177 }
178
179 Thread.Sleep(rand.Next(100, 1000));
180 }
181 });
182
183 t.Start();
184 }
185
186 started.Wait();
187
188 try
189 {
190 a();
191 }
192 finally
193 {
194 Interlocked.Exchange(ref stopped, 1);
195 foreach (var t in ts)
196 t.Join();
197 }
198 }
199
200 public static void SleepOrSpin(int ms)
201 {
202 if (ms == 0)
203 return;
204
205 if (ms % 2 == 0)
206 {
207 var sw = Stopwatch.StartNew();
208 while (sw.Elapsed.TotalMilliseconds < ms)
209 ;
210 }
211 else
212 Thread.Sleep(ms);
213 }
214 }
215 }
216 #endif