using System.Diagnostics;
namespace Chronos.Data.Tests.Perf;
/// Timing helpers of the opt-in performance gates (CHRONOS_PERF=1, category perf).
internal static class PerfGate
{
public const string Variable = "CHRONOS_PERF";
public static async Task TimeAsync(Func action)
{
var watch = Stopwatch.StartNew();
await action();
return watch.Elapsed.TotalMilliseconds;
}
/// Median of timed runs after one untimed warm-up.
public static async Task MedianAsync(Func action, int runs = 5)
{
await action();
var times = new double[runs];
for (var i = 0; i < runs; i++) times[i] = await TimeAsync(action);
Array.Sort(times);
return times[runs / 2];
}
}