Helpers.cs 6.7 KB

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