cs-codex-dist-tests/DistTestCore/Stopwatch.cs

37 lines
859 B
C#
Raw Normal View History

2023-04-14 12:53:39 +00:00
using Logging;
using Utils;
namespace DistTestCore
{
public class Stopwatch
{
2023-04-18 11:22:41 +00:00
private readonly DateTime start = DateTime.UtcNow;
private readonly BaseLog log;
private readonly string name;
public Stopwatch(BaseLog log, string name)
2023-04-14 12:53:39 +00:00
{
2023-04-18 11:22:41 +00:00
this.log = log;
this.name = name;
}
2023-04-14 12:53:39 +00:00
2023-04-18 11:22:41 +00:00
public static void Measure(BaseLog log, string name, Action action)
{
var sw = Begin(log, name);
2023-04-14 12:53:39 +00:00
action();
2023-04-18 11:22:41 +00:00
sw.End();
}
2023-04-14 12:53:39 +00:00
2023-04-18 11:22:41 +00:00
public static Stopwatch Begin(BaseLog log, string name)
{
return new Stopwatch(log, name);
}
2023-04-14 12:53:39 +00:00
2023-04-18 11:22:41 +00:00
public void End(string msg = "")
{
var duration = DateTime.UtcNow - start;
log.Log($"{name} {msg} ({Time.FormatDuration(duration)})");
2023-04-14 12:53:39 +00:00
}
}
}