|
| 1 | +using System.Diagnostics; |
| 2 | +using PowerThreadPool; |
| 3 | +using PowerThreadPool.Options; |
| 4 | +using PowerThreadPool.Results; |
| 5 | + |
| 6 | +namespace UnitTest |
| 7 | +{ |
| 8 | + public class StressTest |
| 9 | + { |
| 10 | + PowerPool _powerPool; |
| 11 | + |
| 12 | + [Fact] |
| 13 | + public async Task StressTest1() |
| 14 | + { |
| 15 | + _powerPool = new PowerPool(new PowerPoolOption() { DestroyThreadOption = new DestroyThreadOption() }); |
| 16 | + |
| 17 | + int totalTasks = 1000000; |
| 18 | + |
| 19 | + for (int i = 0; i < 100; ++i) |
| 20 | + { |
| 21 | + int doneCount = 0; |
| 22 | + int failedCount = 0; |
| 23 | + |
| 24 | + _powerPool.EnablePoolIdleCheck = false; |
| 25 | + |
| 26 | + Task[] tasks = Enumerable.Range(0, totalTasks).Select(i => |
| 27 | + Task.Run(() => |
| 28 | + { |
| 29 | + string workId = _powerPool.QueueWorkItem(() => |
| 30 | + { |
| 31 | + }, (res) => |
| 32 | + { |
| 33 | + if (res.Status == Status.Failed) |
| 34 | + { |
| 35 | + Interlocked.Increment(ref failedCount); |
| 36 | + } |
| 37 | + Interlocked.Increment(ref doneCount); |
| 38 | + }); |
| 39 | + Assert.NotNull(workId); |
| 40 | + }) |
| 41 | + ).ToArray(); |
| 42 | + |
| 43 | + await Task.WhenAll(tasks); |
| 44 | + |
| 45 | + _powerPool.EnablePoolIdleCheck = true; |
| 46 | + |
| 47 | + await _powerPool.WaitAsync(); |
| 48 | + |
| 49 | + string errLog = ""; |
| 50 | + errLog = "doneCount: " + doneCount + "/" + totalTasks + " | failedCount: " + failedCount + " | powerPool.RunningWorkerCount: " + _powerPool.RunningWorkerCount + " | powerPool.WaitingWorkCount: " + _powerPool.WaitingWorkCount + " | powerPool.IdleWorkerCount: " + _powerPool.IdleWorkerCount + " | powerPool.MaxThreads: " + _powerPool.PowerPoolOption.MaxThreads; |
| 51 | + if (totalTasks != doneCount || 0 != failedCount || 0 != _powerPool.RunningWorkerCount || 0 != _powerPool.WaitingWorkCount || _powerPool.IdleWorkerCount == 0) |
| 52 | + { |
| 53 | + Assert.Fail(errLog); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + [Fact] |
| 59 | + public async Task StressTest2() |
| 60 | + { |
| 61 | + Random random = new Random(); |
| 62 | + bool run = false; |
| 63 | + int doneCount = 0; |
| 64 | + |
| 65 | + _powerPool = new PowerPool(); |
| 66 | + _powerPool.PowerPoolOption = new PowerPoolOption() |
| 67 | + { |
| 68 | + MaxThreads = 8, |
| 69 | + DestroyThreadOption = new DestroyThreadOption() { MinThreads = 4, KeepAliveTime = 1000 }, |
| 70 | + StartSuspended = true, |
| 71 | + DefaultCallback = (res) => |
| 72 | + { |
| 73 | + Interlocked.Increment(ref doneCount); |
| 74 | + }, |
| 75 | + }; |
| 76 | + |
| 77 | + long start = GetNowSs(); |
| 78 | + run = true; |
| 79 | + while (run) |
| 80 | + { |
| 81 | + if (GetNowSs() - start >= 300000) |
| 82 | + { |
| 83 | + break; |
| 84 | + } |
| 85 | + |
| 86 | + int runCount = random.Next(10, 200); |
| 87 | + doneCount = 0; |
| 88 | + for (int i = 0; i < runCount; ++i) |
| 89 | + { |
| 90 | + int r = random.Next(0, 101); |
| 91 | + if (r == 100) |
| 92 | + { |
| 93 | + string id = _powerPool.QueueWorkItem(() => { throw new Exception(); }); |
| 94 | + if (id == null) |
| 95 | + { |
| 96 | + Assert.Fail("PoolStopping"); |
| 97 | + } |
| 98 | + } |
| 99 | + else if (r >= 95 && r <= 99) |
| 100 | + { |
| 101 | + string id = _powerPool.QueueWorkItem(() => |
| 102 | + { |
| 103 | + Sleep(10000); |
| 104 | + int r1 = random.Next(0, 101); |
| 105 | + if (r1 >= 100 && r1 <= 100) |
| 106 | + { |
| 107 | + Thread.Sleep(1); |
| 108 | + } |
| 109 | + }); |
| 110 | + if (id == null) |
| 111 | + { |
| 112 | + Assert.Fail("PoolStopping"); |
| 113 | + } |
| 114 | + } |
| 115 | + else if (r >= 94 && r <= 94) |
| 116 | + { |
| 117 | + string id = _powerPool.QueueWorkItem(() => |
| 118 | + { |
| 119 | + Sleep(30000); |
| 120 | + int r1 = random.Next(0, 101); |
| 121 | + if (r1 >= 100 && r1 <= 100) |
| 122 | + { |
| 123 | + Thread.Sleep(1); |
| 124 | + } |
| 125 | + }); |
| 126 | + if (id == null) |
| 127 | + { |
| 128 | + Assert.Fail("PoolStopping"); |
| 129 | + } |
| 130 | + } |
| 131 | + else |
| 132 | + { |
| 133 | + string id = _powerPool.QueueWorkItem(() => |
| 134 | + { |
| 135 | + Sleep(random.Next(500, 1000)); |
| 136 | + int r1 = random.Next(0, 101); |
| 137 | + if (r1 >= 100 && r1 <= 100) |
| 138 | + { |
| 139 | + Thread.Sleep(1); |
| 140 | + } |
| 141 | + }); |
| 142 | + if (id == null) |
| 143 | + { |
| 144 | + Assert.Fail("PoolStopping"); |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + Thread.Yield(); |
| 150 | + |
| 151 | + if (runCount != _powerPool.WaitingWorkCount) |
| 152 | + { |
| 153 | + Assert.Fail(); |
| 154 | + } |
| 155 | + |
| 156 | + _powerPool.Start(); |
| 157 | + |
| 158 | + int r1 = random.Next(0, 101); |
| 159 | + if (r1 >= 81 && r1 <= 100) |
| 160 | + { |
| 161 | + _powerPool.Stop(); |
| 162 | + await _powerPool.WaitAsync(); |
| 163 | + if (_powerPool.RunningWorkerCount > 0 || _powerPool.WaitingWorkCount > 0) |
| 164 | + { |
| 165 | + Assert.Fail(); |
| 166 | + } |
| 167 | + } |
| 168 | + else if (r1 >= 61 && r1 <= 80) |
| 169 | + { |
| 170 | + _powerPool.Stop(true); |
| 171 | + await _powerPool.WaitAsync(); |
| 172 | + if (_powerPool.RunningWorkerCount > 0 || _powerPool.WaitingWorkCount > 0) |
| 173 | + { |
| 174 | + Assert.Fail(); |
| 175 | + } |
| 176 | + } |
| 177 | + else |
| 178 | + { |
| 179 | + await _powerPool.WaitAsync(); |
| 180 | + if (_powerPool.RunningWorkerCount > 0 || _powerPool.WaitingWorkCount > 0 || runCount != doneCount) |
| 181 | + { |
| 182 | + Assert.Fail(); |
| 183 | + } |
| 184 | + } |
| 185 | + if (r1 >= 81 && r1 <= 100) |
| 186 | + { |
| 187 | + } |
| 188 | + else |
| 189 | + { |
| 190 | + Sleep(random.Next(0, 1500)); |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + [Fact] |
| 196 | + public async Task StressTest3() |
| 197 | + { |
| 198 | + _powerPool = new PowerPool(new PowerPoolOption() { DestroyThreadOption = new DestroyThreadOption() }); |
| 199 | + |
| 200 | + int totalTasks = 100; |
| 201 | + int doneCount = 0; |
| 202 | + for (int i = 0; i < 1000000; ++i) |
| 203 | + { |
| 204 | + Task[] tasks = Enumerable.Range(0, totalTasks).Select(i => |
| 205 | + Task.Run(() => |
| 206 | + { |
| 207 | + _powerPool.QueueWorkItem(() => |
| 208 | + { |
| 209 | + Interlocked.Increment(ref doneCount); |
| 210 | + }); |
| 211 | + }) |
| 212 | + ).ToArray(); |
| 213 | + |
| 214 | + await Task.WhenAll(tasks); |
| 215 | + |
| 216 | + await _powerPool.WaitAsync(); |
| 217 | + } |
| 218 | + |
| 219 | + string errLog = ""; |
| 220 | + errLog = "doneCount: " + doneCount + "/" + 100 * 1000000 + " | powerPool.RunningWorkerCount: " + _powerPool.RunningWorkerCount + " | powerPool.WaitingWorkCount: " + _powerPool.WaitingWorkCount + " | powerPool.IdleWorkerCount: " + _powerPool.IdleWorkerCount + " | powerPool.MaxThreads: " + _powerPool.PowerPoolOption.MaxThreads; |
| 221 | + if (100 * 1000000 != doneCount || 0 != _powerPool.RunningWorkerCount || 0 != _powerPool.WaitingWorkCount || _powerPool.IdleWorkerCount == 0) |
| 222 | + { |
| 223 | + Assert.Fail(errLog); |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + [Fact] |
| 228 | + public async Task StressTest4() |
| 229 | + { |
| 230 | + Random random = new Random(); |
| 231 | + bool run = false; |
| 232 | + int doneCount = 0; |
| 233 | + |
| 234 | + _powerPool = new PowerPool(); |
| 235 | + _powerPool.PowerPoolOption = new PowerPoolOption() |
| 236 | + { |
| 237 | + MaxThreads = 8, |
| 238 | + DestroyThreadOption = new DestroyThreadOption() { MinThreads = 4, KeepAliveTime = 1000 }, |
| 239 | + StartSuspended = true, |
| 240 | + DefaultCallback = (res) => |
| 241 | + { |
| 242 | + Interlocked.Increment(ref doneCount); |
| 243 | + }, |
| 244 | + QueueType = QueueType.LIFO |
| 245 | + }; |
| 246 | + |
| 247 | + long start = GetNowSs(); |
| 248 | + run = true; |
| 249 | + while (run) |
| 250 | + { |
| 251 | + if (GetNowSs() - start >= 300000) |
| 252 | + { |
| 253 | + break; |
| 254 | + } |
| 255 | + |
| 256 | + int runCount = random.Next(10, 200); |
| 257 | + doneCount = 0; |
| 258 | + for (int i = 0; i < runCount; ++i) |
| 259 | + { |
| 260 | + int r = random.Next(0, 101); |
| 261 | + if (r == 100) |
| 262 | + { |
| 263 | + string id = _powerPool.QueueWorkItem(() => { throw new Exception(); }); |
| 264 | + if (id == null) |
| 265 | + { |
| 266 | + Assert.Fail("PoolStopping"); |
| 267 | + } |
| 268 | + } |
| 269 | + else if (r >= 95 && r <= 99) |
| 270 | + { |
| 271 | + string id = _powerPool.QueueWorkItem(() => |
| 272 | + { |
| 273 | + Sleep(10000); |
| 274 | + int r1 = random.Next(0, 101); |
| 275 | + if (r1 >= 100 && r1 <= 100) |
| 276 | + { |
| 277 | + Thread.Sleep(1); |
| 278 | + } |
| 279 | + }); |
| 280 | + if (id == null) |
| 281 | + { |
| 282 | + Assert.Fail("PoolStopping"); |
| 283 | + } |
| 284 | + } |
| 285 | + else if (r >= 94 && r <= 94) |
| 286 | + { |
| 287 | + string id = _powerPool.QueueWorkItem(() => |
| 288 | + { |
| 289 | + Sleep(30000); |
| 290 | + int r1 = random.Next(0, 101); |
| 291 | + if (r1 >= 100 && r1 <= 100) |
| 292 | + { |
| 293 | + Thread.Sleep(1); |
| 294 | + } |
| 295 | + }); |
| 296 | + if (id == null) |
| 297 | + { |
| 298 | + Assert.Fail("PoolStopping"); |
| 299 | + } |
| 300 | + } |
| 301 | + else |
| 302 | + { |
| 303 | + string id = _powerPool.QueueWorkItem(() => |
| 304 | + { |
| 305 | + Sleep(random.Next(500, 1000)); |
| 306 | + int r1 = random.Next(0, 101); |
| 307 | + if (r1 >= 100 && r1 <= 100) |
| 308 | + { |
| 309 | + Thread.Sleep(1); |
| 310 | + } |
| 311 | + }); |
| 312 | + if (id == null) |
| 313 | + { |
| 314 | + Assert.Fail("PoolStopping"); |
| 315 | + } |
| 316 | + } |
| 317 | + } |
| 318 | + |
| 319 | + Thread.Yield(); |
| 320 | + |
| 321 | + if (runCount != _powerPool.WaitingWorkCount) |
| 322 | + { |
| 323 | + Assert.Fail(); |
| 324 | + } |
| 325 | + |
| 326 | + _powerPool.Start(); |
| 327 | + |
| 328 | + int r1 = random.Next(0, 101); |
| 329 | + if (r1 >= 81 && r1 <= 100) |
| 330 | + { |
| 331 | + _powerPool.Stop(); |
| 332 | + await _powerPool.WaitAsync(); |
| 333 | + if (_powerPool.RunningWorkerCount > 0 || _powerPool.WaitingWorkCount > 0) |
| 334 | + { |
| 335 | + Assert.Fail(); |
| 336 | + } |
| 337 | + } |
| 338 | + else if (r1 >= 61 && r1 <= 80) |
| 339 | + { |
| 340 | + _powerPool.Stop(true); |
| 341 | + await _powerPool.WaitAsync(); |
| 342 | + if (_powerPool.RunningWorkerCount > 0 || _powerPool.WaitingWorkCount > 0) |
| 343 | + { |
| 344 | + Assert.Fail(); |
| 345 | + } |
| 346 | + } |
| 347 | + else |
| 348 | + { |
| 349 | + await _powerPool.WaitAsync(); |
| 350 | + if (_powerPool.RunningWorkerCount > 0 || _powerPool.WaitingWorkCount > 0 || runCount != doneCount) |
| 351 | + { |
| 352 | + Assert.Fail(); |
| 353 | + } |
| 354 | + } |
| 355 | + if (r1 >= 81 && r1 <= 100) |
| 356 | + { |
| 357 | + } |
| 358 | + else |
| 359 | + { |
| 360 | + Sleep(random.Next(0, 1500)); |
| 361 | + } |
| 362 | + } |
| 363 | + } |
| 364 | + |
| 365 | + private void Sleep(int ms) |
| 366 | + { |
| 367 | + Stopwatch stopwatch = Stopwatch.StartNew(); |
| 368 | + |
| 369 | + while (stopwatch.ElapsedMilliseconds < ms) |
| 370 | + { |
| 371 | + _powerPool.StopIfRequested(); |
| 372 | + } |
| 373 | + |
| 374 | + stopwatch.Stop(); |
| 375 | + } |
| 376 | + |
| 377 | + private long GetNowSs() |
| 378 | + { |
| 379 | + return DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); |
| 380 | + } |
| 381 | + } |
| 382 | +} |
0 commit comments