using System;
using System.Collections.Generic;
using System.Diagnostics;
using OmegaLeo.HelperLib.Shared.Attributes;
namespace OmegaLeo.HelperLib.Helpers
{
///
/// Utility class for benchmarking code execution time.
///
[Documentation(nameof(BenchmarkUtility), "Utility class for benchmarking code execution time.", null, @"```csharp
BenchmarkUtility.Start(""MyBenchmark"");
// Code to benchmark
BenchmarkUtility.Stop(""MyBenchmark"");
var results = BenchmarkUtility.GetResults(""MyBenchmark"");
```")]
[Changelog("1.2.0", "Fixed root namespace to OmegaLeo.HelperLib.Helpers.", "January 28, 2026")]
///
/// Utility class for benchmarking code execution time.
///
public class BenchmarkUtility
{
private static Dictionary> _benchmarks = new Dictionary>();
private static Dictionary _stopwatches = new Dictionary();
///
/// Retrieves or creates a Stopwatch instance for the given key.
///
/// The key parameter
/// The return value
[Documentation("GetStopwatch", "Retrieves or creates a Stopwatch instance for the given key.", null, null)]
private static Stopwatch GetStopwatch(string key)
{
Stopwatch stopwatch;
if (!_stopwatches.TryGetValue(key, out stopwatch))
{
stopwatch = new Stopwatch();
_stopwatches.Add(key, stopwatch);
}
return stopwatch;
}
///
/// Records the execution time of the provided action and returns the elapsed time in milliseconds.
///
/// The actionToRecord parameter
/// The return value
[Documentation("Record", "Records the execution time of the provided action and returns the elapsed time in milliseconds.", null, @"```csharp
var time = BenchmarkUtility.Record(() =>
{
// Code to benchmark
});
Console.WriteLine($""Elapsed time: {time} ms"");
```")]
public static long Record(Action actionToRecord)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
actionToRecord.Invoke();
stopwatch.Stop();
return stopwatch.ElapsedMilliseconds;
}
///
/// Records the execution time of the provided action, saves it under the given key, and returns the elapsed time in milliseconds.
///
/// The key parameter
/// The actionToRecord parameter
/// The return value
[Documentation("RecordAndSaveToResults", "Records the execution time of the provided action, saves it under the given key, and returns the elapsed time in milliseconds.", null, @"```csharp
var time = BenchmarkUtility.RecordAndSaveToResults(""MyBenchmark"", () =>
{
// Code to benchmark
});
Console.WriteLine($""Elapsed time: {time} ms"");
var results = BenchmarkUtility.GetResults(""MyBenchmark"");
var averageTime = results.Average();
Console.WriteLine($""Average time: {averageTime} ms"");
```")]
public static long RecordAndSaveToResults(string key, Action actionToRecord)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
actionToRecord.Invoke();
stopwatch.Stop();
if (!_benchmarks.ContainsKey(key))
{
_benchmarks[key] = new List();
}
_benchmarks[key].Add(stopwatch.ElapsedMilliseconds);
return stopwatch.ElapsedMilliseconds;
}
///
/// Starts or restarts the stopwatch for the given key.
///
/// The key parameter
[Documentation("Start", "Starts or restarts the stopwatch for the given key.", null, null)]
///
/// Starts or restarts the stopwatch for the given key.
///
/// The benchmark identifier
public static void Start(string key)
{
GetStopwatch(key).Restart();
}
///
/// Stops the stopwatch for the given key and records the elapsed time.
///
/// The key parameter
[Documentation("Stop", "Stops the stopwatch for the given key and records the elapsed time.", null, null)]
///
/// Stops the stopwatch for the given key and records the elapsed time.
///
/// The benchmark identifier
public static void Stop(string key)
{
if (!_benchmarks.ContainsKey(key))
{
_benchmarks[key] = new List();
}
GetStopwatch(key).Stop();
_benchmarks[key].Add(GetStopwatch(key).ElapsedMilliseconds);
}
///
/// Retrieves the list of recorded times for the given key.
///
/// The key parameter
/// The return value
[Documentation("GetResults", "Retrieves the list of recorded times for the given key.", null, null)]
///
/// Retrieves the list of recorded times for the given key.
///
/// The benchmark identifier
/// List of recorded times in milliseconds
public static List GetResults(string key)
{
return _benchmarks.ContainsKey(key) ? _benchmarks[key] : new List();
}
///
/// Clears all recorded benchmark results.
///
[Documentation("ClearResults", "Clears all recorded benchmark results.", null, null)]
public static void ClearResults()
{
_benchmarks.Clear();
}
///
/// Retrieves all recorded benchmark results.
///
/// The return value
[Documentation("GetAllResults", "Retrieves all recorded benchmark results.", null, null)]
public static Dictionary> GetAllResults()
{
return new Dictionary>(_benchmarks);
}
}
}