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); } }
private static void WriteExceptions(StressMetrics metrics, string header, StressOptions options) { var sb = new StringBuilder(); sb.AppendLine("=== Exceptions ==="); foreach (var exception in metrics.Exceptions) { sb.AppendLine(exception.ToString()); sb.AppendLine(); } var exceptionsString = sb.ToString(); Console.WriteLine(exceptionsString); if (!string.IsNullOrEmpty(options.ExceptionsFile)) { File.WriteAllText(options.ExceptionsFile, header + exceptionsString); } }
private static void WriteEvents(StressMetrics metrics, string header, StressOptions options) { var sb = new StringBuilder(); sb.AppendLine("=== Events ==="); foreach (var e in metrics.Events) { sb.AppendLine($"[{e.EventArgs.EventSource.Name} :: {e.EventArgs.EventName}]"); sb.AppendLine(e.Message); sb.AppendLine(); } var eventsString = sb.ToString(); Console.WriteLine(eventsString); if (!string.IsNullOrEmpty(options.EventsFile)) { File.WriteAllText(options.ExceptionsFile, header + eventsString); } }
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(); }