Skip to content

Commit

Permalink
fix review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
David Chaava committed Jun 27, 2023
1 parent d44425e commit 89754e5
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
namespace CosmosBenchmark.Fx
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------

namespace CosmosBenchmark.Fx
{
using System;
using System.Diagnostics.Tracing;
Expand All @@ -8,101 +12,140 @@

internal class DiagnosticDataListener : EventListener
{
/// <summary>
/// A constant string representing the container name in Azure Blob Storage.
/// </summary>
private const string BlobContainerName = "diagnostics";

/// <summary>
/// A constant string representing the diagnostics file path.
/// </summary>
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;

/// <summary>
/// Lock object for synchronization.
/// </summary>
private readonly object fileLock = new object();
private static readonly string BlobPrefix = $"{Environment.MachineName}/{Environment.MachineName}";

/// <summary>
/// Number of files
/// </summary>
private int filesCount = 0;

/// <summary>
/// Represents a class that performs writing diagnostic data to a file and uploading it to Azure Blob Storage
/// </summary>
public DiagnosticDataListener()
{
if (!File.Exists(DiagnosticsFilePath))
{
File.Create(DiagnosticsFilePath).Close();
}
CreateFileIfNotExist();


/// <summary>
/// Checks the file size every n milliseconds for diagnostics and creates a new one if the maximum limit is exceeded.
/// </summary>
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();
}
}

/// <summary>
/// Listening for events generated by BenchmarkLatencyEventSource
/// </summary>
/// <param name="eventData">An instance of <see cref="EventWrittenEventArgs "/> containing the request latency and diagnostics.</param>
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)
/// <summary>
/// Uploading all files with diagnostic data to blob storage
/// </summary>
/// <param name="config">An instance of <see cref="BenchmarkConfig "/> containing the benchmark tool input parameters.</param>
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);
}
}
}
10 changes: 5 additions & 5 deletions Microsoft.Azure.Cosmos.Samples/Tools/Benchmark/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -178,11 +183,6 @@ await this.PublishResults(
cosmosClient);
}

if (!string.IsNullOrEmpty(config.ResultsStorageConnectionString))
{
DiagnosticDataListener.UploadDiagnostcs(config);
}

return runSummary;
}
}
Expand Down

0 comments on commit 89754e5

Please sign in to comment.