From 89754e52ce3f3adce8990f70f4b73e0b41f53c85 Mon Sep 17 00:00:00 2001 From: David Chaava Date: Tue, 27 Jun 2023 17:06:38 +0200 Subject: [PATCH] fix review comments --- .../Benchmark/Fx/DiagnosticDataListener.cs | 125 ++++++++++++------ .../Tools/Benchmark/Program.cs | 10 +- 2 files changed, 89 insertions(+), 46 deletions(-) diff --git a/Microsoft.Azure.Cosmos.Samples/Tools/Benchmark/Fx/DiagnosticDataListener.cs b/Microsoft.Azure.Cosmos.Samples/Tools/Benchmark/Fx/DiagnosticDataListener.cs index 26b111ca77..38a5c6e2f6 100644 --- a/Microsoft.Azure.Cosmos.Samples/Tools/Benchmark/Fx/DiagnosticDataListener.cs +++ b/Microsoft.Azure.Cosmos.Samples/Tools/Benchmark/Fx/DiagnosticDataListener.cs @@ -1,4 +1,8 @@ -namespace CosmosBenchmark.Fx +//------------------------------------------------------------ +// Copyright (c) Microsoft Corporation. All rights reserved. +//------------------------------------------------------------ + +namespace CosmosBenchmark.Fx { using System; using System.Diagnostics.Tracing; @@ -8,101 +12,140 @@ internal class DiagnosticDataListener : EventListener { + /// + /// A constant string representing the container name in Azure Blob Storage. + /// private const string BlobContainerName = "diagnostics"; + + /// + /// A constant string representing the diagnostics file path. + /// private const string DiagnosticsFilePath = "BenchmarkDiagnostics.out"; - private static readonly object fileLock = new object(); + private const int MaxDIagnosticFileSize = 100_000_000; + private const int FileSizeCheckIntervalMs = 5_000; + + /// + /// Lock object for synchronization. + /// + private readonly object fileLock = new object(); + private static readonly string BlobPrefix = $"{Environment.MachineName}/{Environment.MachineName}"; + + /// + /// Number of files + /// private int filesCount = 0; + /// + /// Represents a class that performs writing diagnostic data to a file and uploading it to Azure Blob Storage + /// public DiagnosticDataListener() { - if (!File.Exists(DiagnosticsFilePath)) - { - File.Create(DiagnosticsFilePath).Close(); - } + CreateFileIfNotExist(); + + /// + /// Checks the file size every n milliseconds for diagnostics and creates a new one if the maximum limit is exceeded. + /// ThreadPool.QueueUserWorkItem(state => { while (true) { - lock (fileLock) + lock (this.fileLock) { - // Check the file size - if (!File.Exists(DiagnosticsFilePath)) - { - File.Create(DiagnosticsFilePath).Close(); - } + CreateFileIfNotExist(); FileInfo fileInfo = new FileInfo(DiagnosticsFilePath); long fileSize = fileInfo.Length; - // If the file size is greater than 100MB (100,000,000 bytes) - if (fileSize > 100_000_000) + if (fileSize > MaxDIagnosticFileSize) { - // Create a new file with the same name string newFilePath = Path.Combine(fileInfo.DirectoryName, $"{fileInfo.Name}-{this.filesCount}"); File.Move(DiagnosticsFilePath, newFilePath, true); this.filesCount++; - // Optionally, you can perform additional actions on the new file - // For example, you can delete or process it - - Console.WriteLine("File size exceeded 100MB. Renamed the file and created a new one."); + Utility.TeeTraceInformation("File size exceeded 100MB. Renamed the file and created a new one."); } } - // Wait for 10 seconds before checking again - Thread.Sleep(5_000); + Thread.Sleep(FileSizeCheckIntervalMs); } }); } + private static void CreateFileIfNotExist() + { + if (!File.Exists(DiagnosticsFilePath)) + { + File.Create(DiagnosticsFilePath).Close(); + } + } + + /// + /// Listening for events generated by BenchmarkLatencyEventSource + /// + /// An instance of containing the request latency and diagnostics. protected override void OnEventWritten(EventWrittenEventArgs eventData) { - lock (fileLock) + lock (this.fileLock) { - using (StreamWriter writer = new StreamWriter(DiagnosticsFilePath, true)) + try + { + using (StreamWriter writer = new StreamWriter(DiagnosticsFilePath, true)) + { + writer.WriteLine($"{eventData.Payload[2]} ; {eventData.Payload[3]}"); + } + } + catch (Exception ex) { - writer.WriteLine($"{eventData.Payload[2]} ; {eventData.Payload[3]}"); + Utility.TeeTraceInformation("An exception ocured while writing diagnostic data to the file"); + Utility.TeeTraceInformation(ex.Message); } + } } - public static void UploadDiagnostcs(BenchmarkConfig config) + /// + /// Uploading all files with diagnostic data to blob storage + /// + /// An instance of containing the benchmark tool input parameters. + public void UploadDiagnostcs(BenchmarkConfig config) { - try - { - Console.WriteLine("Uploading diagnostics"); - string[] diagnosticFiles = Directory.GetFiles(".", $"{DiagnosticsFilePath}*"); - lock (fileLock) + Utility.TeeTraceInformation("Uploading diagnostics"); + string[] diagnosticFiles = Directory.GetFiles(".", $"{DiagnosticsFilePath}*"); + + lock (this.fileLock) + { + for (int i = 0; i < diagnosticFiles.Length; i++) { - for (int i = 0; i < diagnosticFiles.Length; i++) + try { string diagnosticFile = diagnosticFiles[i]; - Console.WriteLine($"Uploading {i+1} of {diagnosticFiles.Length} file: {diagnosticFile} "); + Utility.TeeTraceInformation($"Uploading {i + 1} of {diagnosticFiles.Length} file: {diagnosticFile} "); - string BlobName = $"{Environment.MachineName}/{Environment.MachineName}-{i}.out"; - BlobContainerClient blobContainerClient = GetBlobServiceClient(config); - BlobClient blobClient = blobContainerClient.GetBlobClient(BlobName); + string BlobName = $"{BlobPrefix}-{i}.out"; + BlobClient blobClient = GetBlobServiceClient(config, BlobName); blobClient.Upload(diagnosticFile, overwrite: true); } + catch (Exception ex) + { + Utility.TeeTraceInformation("An exception ocured while uploading file to the blob storage"); + Utility.TeeTraceInformation(ex.Message); + } } } - catch (Exception ex) - { - Console.WriteLine(ex.ToString()); - } + } - public static BlobContainerClient GetBlobServiceClient(BenchmarkConfig config) + private static BlobClient GetBlobServiceClient(BenchmarkConfig config, string BlobName) { BlobContainerClient blobContainerClient = new BlobContainerClient(config.ResultsStorageConnectionString, BlobContainerName); blobContainerClient.CreateIfNotExists(); - return blobContainerClient; + return blobContainerClient.GetBlobClient(BlobName); } } } diff --git a/Microsoft.Azure.Cosmos.Samples/Tools/Benchmark/Program.cs b/Microsoft.Azure.Cosmos.Samples/Tools/Benchmark/Program.cs index 135093656d..f8728b6de7 100644 --- a/Microsoft.Azure.Cosmos.Samples/Tools/Benchmark/Program.cs +++ b/Microsoft.Azure.Cosmos.Samples/Tools/Benchmark/Program.cs @@ -54,6 +54,11 @@ public static async Task Main(string[] args) Program program = new Program(); RunSummary runSummary = await program.ExecuteAsync(config); + + if (!string.IsNullOrEmpty(config.ResultsStorageConnectionString)) + { + diagnosticDataListener.UploadDiagnostcs(config); + } } finally { @@ -178,11 +183,6 @@ await this.PublishResults( cosmosClient); } - if (!string.IsNullOrEmpty(config.ResultsStorageConnectionString)) - { - DiagnosticDataListener.UploadDiagnostcs(config); - } - return runSummary; } }