private static void RunLoop(IPerfStressTest test, CancellationToken cancellationToken) { var sw = Stopwatch.StartNew(); while (!cancellationToken.IsCancellationRequested) { try { test.Run(cancellationToken); var count = Interlocked.Increment(ref _completedOperations); _lastCompletionTimes[count % _lastCompletionTimes.Length] = sw.Elapsed; } catch (OperationCanceledException) { } } }
private static void RunLoop(IPerfStressTest test, int index, CancellationToken cancellationToken) { var sw = Stopwatch.StartNew(); while (!cancellationToken.IsCancellationRequested) { try { test.Run(cancellationToken); _completedOperations[index]++; _lastCompletionTimes[index] = sw.Elapsed; } catch (OperationCanceledException) { } } }
private static async Task RunLoopAsync(IPerfStressTest test, CancellationToken cancellationToken) { var sw = Stopwatch.StartNew(); while (!cancellationToken.IsCancellationRequested) { try { await test.RunAsync(cancellationToken); var count = Interlocked.Increment(ref _completedOperations); _lastCompletionTimes[count % _lastCompletionTimes.Length] = sw.Elapsed; } catch (Exception e) { // Ignore if any part of the exception chain is type OperationCanceledException if (!ContainsOperationCanceledException(e)) { throw; } } } }
private static async Task RunLoopAsync(IPerfStressTest test, int index, CancellationToken cancellationToken) { var sw = Stopwatch.StartNew(); while (!cancellationToken.IsCancellationRequested) { try { await test.RunAsync(cancellationToken); _completedOperations[index]++; _lastCompletionTimes[index] = sw.Elapsed; } catch (Exception e) { // Ignore if any part of the exception chain is type OperationCanceledException if (!ContainsOperationCanceledException(e)) { throw; } } } }
private static async Task Run(Type testType, PerfStressOptions options) { if (!GCSettings.IsServerGC) { throw new InvalidOperationException("Requires server GC"); } Console.WriteLine("=== Options ==="); Console.WriteLine(JsonSerializer.Serialize(options, options.GetType(), new JsonSerializerOptions() { WriteIndented = true })); Console.WriteLine(); using var setupStatusCts = new CancellationTokenSource(); var setupStatusTask = PrintStatusAsync("=== Setup ===", () => ".", newLine: false, setupStatusCts.Token); using var cleanupStatusCts = new CancellationTokenSource(); Task cleanupStatusTask = null; var tests = new IPerfStressTest[options.Parallel]; for (var i = 0; i < options.Parallel; i++) { tests[i] = (IPerfStressTest)Activator.CreateInstance(testType, options); } try { await tests[0].GlobalSetupAsync(); try { await Task.WhenAll(tests.Select(t => t.SetupAsync())); setupStatusCts.Cancel(); await setupStatusTask; if (options.Warmup > 0) { await RunTestsAsync(tests, options.Sync, options.Parallel, options.Warmup, "Warmup"); } for (var i = 0; i < options.Iterations; i++) { var title = "Test"; if (options.Iterations > 1) { title += " " + (i + 1); } await RunTestsAsync(tests, options.Sync, options.Parallel, options.Duration, title); } } finally { if (!options.NoCleanup) { if (cleanupStatusTask == null) { cleanupStatusTask = PrintStatusAsync("=== Cleanup ===", () => ".", newLine: false, cleanupStatusCts.Token); } await Task.WhenAll(tests.Select(t => t.CleanupAsync())); } } } finally { if (!options.NoCleanup) { if (cleanupStatusTask == null) { cleanupStatusTask = PrintStatusAsync("=== Cleanup ===", () => ".", newLine: false, cleanupStatusCts.Token); } await tests[0].GlobalCleanupAsync(); } } cleanupStatusCts.Cancel(); if (cleanupStatusTask != null) { await cleanupStatusTask; } }