private static async Task RunTestAsync(IStressTest test, int durationSeconds, StressMetrics metrics) { var duration = TimeSpan.FromSeconds(durationSeconds); using var testCts = new CancellationTokenSource(duration); var cancellationToken = testCts.Token; metrics.StartAutoUpdate(); using var progressStatusCts = new CancellationTokenSource(); var progressStatusThread = PrintStatus( "=== Metrics ===", () => metrics.ToString(), newLine: true, progressStatusCts.Token); try { await test.RunAsync(cancellationToken); } catch (Exception e) when(ContainsOperationCanceledException(e)) { } // TODO: Consider more exception handling, including a special case for OutOfMemoryException, StackOverflowException, etc metrics.StopAutoUpdate(); progressStatusCts.Cancel(); progressStatusThread.Join(); }
private static void WriteMetrics(StressMetrics metrics, string header, StressOptions options) { var sb = new StringBuilder(); sb.AppendLine("=== Final Metrics ==="); sb.Append(metrics.ToString()); var metricsString = sb.ToString(); Console.WriteLine(metricsString); if (!string.IsNullOrEmpty(options.MetricsFile)) { File.WriteAllText(options.MetricsFile, header + metricsString); } }