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
|
// 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.Diagnostics;
using System.Threading;
using System.Linq;
namespace ReactiveTests.Stress
{
public static class Helpers
{
public static void RunWithMemoryPressure(int minBytes, int maxBytes, double activePercent, Action a)
{
var started = new ManualResetEvent(false);
var stopped = 0;
var avg = (minBytes + maxBytes) / 2;
var MIN = avg / 1000;
var MAX = avg / 10;
var allocator = new Thread(() =>
{
var rand = new Random();
var roots = new List<byte[]>();
var bytes = 0;
while (bytes < avg)
{
var n = rand.Next(MIN, MAX);
bytes += n;
roots.Add(new byte[n]);
}
started.Set();
long avgSum = 0;
long count = 0;
const int DEC = 0;
const int INC = 1;
var trendPhase = 0;
var trendLength = 0;
while (Thread.VolatileRead(ref stopped) == 0)
{
if (trendLength-- == 0)
{
trendPhase = rand.Next(0, 1000) % 2;
trendLength = rand.Next(1, 10);
}
var mem = TimeSpan.Zero;
var sw = Stopwatch.StartNew();
var busy = new Stopwatch();
while (Thread.VolatileRead(ref stopped) == 0 && (double)mem.Ticks / (double)sw.ElapsedTicks < activePercent)
{
busy.Restart();
var runFor = rand.Next(10, 100);
while (busy.ElapsedMilliseconds < runFor)
{
if (trendPhase == INC)
{
if (bytes < maxBytes)
{
var n = rand.Next(MIN, MAX);
bytes += n;
roots.Add(new byte[n]);
continue;
}
else
{
trendPhase = DEC;
}
}
if (trendPhase == DEC)
{
if (bytes > minBytes)
{
if (roots.Count > 0)
{
var i = rand.Next(0, roots.Count);
bytes -= roots[i].Length;
roots.RemoveAt(i);
}
continue;
}
else
{
trendPhase = INC;
}
}
}
mem += busy.Elapsed;
}
var sleepFor = rand.Next(100, 1000);
Thread.Sleep(sleepFor);
avgSum += bytes;
count++;
//Console.WriteLine(bytes + " - Avg = " + avgSum / count);
}
});
allocator.Start();
started.WaitOne();
try
{
a();
}
finally
{
Interlocked.Exchange(ref stopped, 1);
allocator.Join();
}
}
public static void RunWithProcessorPressure(int threadCount, int lockCount, double activePercent, double lockChancePercent, Action a)
{
var stopped = 0;
var started = new CountdownEvent(threadCount);
var ts = new Thread[threadCount];
var locks = Enumerable.Range(0, lockCount).Select(_ => new object()).ToArray();
for (int i = 0; i < threadCount; i++)
{
var id = i;
var t = ts[i] = new Thread(() =>
{
var rand = new Random();
started.Signal();
var sw = Stopwatch.StartNew();
var run = TimeSpan.Zero;
while (Thread.VolatileRead(ref stopped) == 0)
{
var busy = new Stopwatch();
while (Thread.VolatileRead(ref stopped) == 0 && (double)run.Ticks / (double)sw.ElapsedTicks < activePercent)
{
busy.Restart();
const int RUN = 0;
const int BLOCK = 1;
var action = lockCount > 0 && rand.Next() % 100 <= lockChancePercent * 100 ? BLOCK : RUN;
switch (action)
{
case RUN:
//Console.WriteLine("~" + id);
while (busy.ElapsedMilliseconds < 10)
;
break;
case BLOCK:
//Console.WriteLine("!" + id);
lock (locks[rand.Next(0, lockCount)])
Thread.Sleep(rand.Next(100, 1000));
break;
}
run += busy.Elapsed;
}
Thread.Sleep(rand.Next(100, 1000));
}
});
t.Start();
}
started.Wait();
try
{
a();
}
finally
{
Interlocked.Exchange(ref stopped, 1);
foreach (var t in ts)
t.Join();
}
}
public static void SleepOrSpin(int ms)
{
if (ms == 0)
return;
if (ms % 2 == 0)
{
var sw = Stopwatch.StartNew();
while (sw.Elapsed.TotalMilliseconds < ms)
;
}
else
Thread.Sleep(ms);
}
}
}
#endif
|