summary refs log tree commit diff
path: root/extra/admin-api/Models/Spacebar.Models.Generic/Trace.cs
diff options
context:
space:
mode:
Diffstat (limited to 'extra/admin-api/Models/Spacebar.Models.Generic/Trace.cs')
-rw-r--r--extra/admin-api/Models/Spacebar.Models.Generic/Trace.cs67
1 files changed, 67 insertions, 0 deletions
diff --git a/extra/admin-api/Models/Spacebar.Models.Generic/Trace.cs b/extra/admin-api/Models/Spacebar.Models.Generic/Trace.cs
new file mode 100644

index 000000000..e67227974 --- /dev/null +++ b/extra/admin-api/Models/Spacebar.Models.Generic/Trace.cs
@@ -0,0 +1,67 @@ +using System.Diagnostics; +using System.Text.Json.Nodes; +using System.Text.Json.Serialization; + +namespace Spacebar.Models.Generic; + +public class Trace { + [JsonPropertyName("micros")] + public long Micros { get; set; } + + [JsonIgnore] + public string? Name { get; set; } + + [JsonIgnore] + public List<Trace>? Calls { get; set; } + + [JsonPropertyName("calls")] + // zipped array of [string, Trace] + public JsonArray? ZippedCalls { + get { + if (Calls == null) return null; + var arr = new JsonArray(); + foreach (var t in Calls) { + arr.Add(t.Name); + arr.Add(t); + } + + return arr; + } + } + + public JsonArray AsRoot() { + return new() { + Name, + this + }; + } +} + +public static class TraceResult { + public static async Task<TraceResult<T>> TraceAsync<T>(string name, Func<Task<T>> func) { + var sw = Stopwatch.StartNew(); + var result = await func(); + sw.Stop(); + return new TraceResult<T> { + Name = name, + Micros = sw.Elapsed.Microseconds, + Result = result + }; + } + + public static async Task<TraceResult<T>> Trace<T>(string name, Func<T> func) { + var sw = Stopwatch.StartNew(); + var result = func(); + sw.Stop(); + return new TraceResult<T> { + Name = name, + Micros = sw.Elapsed.Microseconds, + Result = result + }; + } +} + +public class TraceResult<T> : Trace { + [JsonIgnore] + public T Result { get; set; } +} \ No newline at end of file