blob: e67227974a4d47f26142abfe44949f27871b25f5 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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; }
}
|