From f92382c20193a5366d15c7347dcc8ed2743f3b85 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Tue, 3 Mar 2026 13:00:38 +0200 Subject: Add WASM-ready flamegraph JSON export --- internal/flamegraph/nativejson_test.go | 75 ++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 internal/flamegraph/nativejson_test.go (limited to 'internal/flamegraph/nativejson_test.go') diff --git a/internal/flamegraph/nativejson_test.go b/internal/flamegraph/nativejson_test.go new file mode 100644 index 0000000..c76d327 --- /dev/null +++ b/internal/flamegraph/nativejson_test.go @@ -0,0 +1,75 @@ +package flamegraph + +import ( + "encoding/json" + "os" + "testing" +) + +type jsonNodeForTest struct { + Name string `json:"name"` + Value uint64 `json:"value"` + Total uint64 `json:"total"` + Children []jsonNodeForTest `json:"children"` +} + +type jsonFlamegraphForTest struct { + Fields []string `json:"fields"` + CountField string `json:"countField"` + Root jsonNodeForTest `json:"root"` +} + +func TestWriteJSONFromFileContainsFlamegraphTree(t *testing.T) { + dir := t.TempDir() + iorFile := writeTestIorZst(t, dir) + + n := NewNativeSVG([]string{"comm", "path", "tracepoint"}, "count") + outFile, err := n.WriteJSONFromFile(iorFile) + if err != nil { + t.Fatalf("WriteJSONFromFile returned error: %v", err) + } + + data, err := os.ReadFile(outFile) + if err != nil { + t.Fatalf("read output json: %v", err) + } + + var payload jsonFlamegraphForTest + if err := json.Unmarshal(data, &payload); err != nil { + t.Fatalf("unmarshal output json: %v", err) + } + + if payload.CountField != "count" { + t.Fatalf("count field = %q, want %q", payload.CountField, "count") + } + if len(payload.Fields) != 3 { + t.Fatalf("fields len = %d, want 3", len(payload.Fields)) + } + if payload.Root.Name != "root" { + t.Fatalf("root name = %q, want %q", payload.Root.Name, "root") + } + if payload.Root.Total != 1 { + t.Fatalf("root total = %d, want 1", payload.Root.Total) + } + if len(payload.Root.Children) != 1 { + t.Fatalf("root children len = %d, want 1", len(payload.Root.Children)) + } + if payload.Root.Children[0].Name != "tester" { + t.Fatalf("root child name = %q, want %q", payload.Root.Children[0].Name, "tester") + } +} + +func TestWriteJSONFromFileCleansUpPartialOutputOnError(t *testing.T) { + dir := t.TempDir() + iorFile := writeTestIorZst(t, dir) + + n := NewNativeSVG([]string{"invalidField"}, "count") + outFile, err := n.WriteJSONFromFile(iorFile) + if err == nil { + t.Fatal("expected error for invalid field, got nil") + } + + if _, statErr := os.Stat(outFile); !os.IsNotExist(statErr) { + t.Fatalf("expected partial output to be removed, stat err=%v", statErr) + } +} -- cgit v1.2.3