summaryrefslogtreecommitdiff
path: root/internal/flamegraph/testfixture.go
blob: 277492515da0655cadc02539ae4bbca6f05024c1 (plain)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package flamegraph

import (
	"ior/internal/types"
	"strings"
)

// SeedTestFlameData populates a deterministic static flamegraph fixture.
// Intended for keyboard-navigation validation in TUI test-flame mode.
func SeedTestFlameData(liveTrie *LiveTrie) {
	if liveTrie == nil {
		return
	}
	for _, record := range testFlameRecords() {
		liveTrie.AddRecord(record)
	}
}

// SeedTestLiveFlameData populates deterministic synthetic data for a given live tick.
// The data shape stays navigable while branch weights shift by phase so the
// terminal flamegraph visibly changes over time.
func SeedTestLiveFlameData(liveTrie *LiveTrie, tick uint64) {
	if liveTrie == nil {
		return
	}
	phase := tick % 4
	for _, base := range testFlameRecords() {
		weight := liveTestWeight(base, phase)
		liveTrie.AddRecord(withTestFlameWeight(base, weight))
	}
}

func testFlameRecords() []IterRecord {
	return []IterRecord{
		newTestFlameRecord("api", "/srv/api/lib/http/client/read", 2001, 2201, types.SYS_ENTER_READ, 180),
		newTestFlameRecord("api", "/srv/api/lib/json/encode/write", 2001, 2201, types.SYS_ENTER_WRITE, 120),
		newTestFlameRecord("api", "/srv/api/storage/postgres/query/read", 2001, 2201, types.SYS_ENTER_READ, 240),
		newTestFlameRecord("api", "/srv/api/storage/postgres/commit/fsync", 2001, 2201, types.SYS_ENTER_FSYNC, 70),
		newTestFlameRecord("worker", "/srv/worker/queue/pop/read", 2002, 2202, types.SYS_ENTER_READ, 160),
		newTestFlameRecord("worker", "/srv/worker/queue/push/write", 2002, 2202, types.SYS_ENTER_WRITE, 145),
		newTestFlameRecord("worker", "/srv/worker/cache/redis/get/read", 2002, 2202, types.SYS_ENTER_READ, 95),
		newTestFlameRecord("worker", "/srv/worker/cache/redis/set/write", 2002, 2202, types.SYS_ENTER_WRITE, 90),
		newTestFlameRecord("ingest", "/srv/ingest/parser/csv/read", 2003, 2203, types.SYS_ENTER_READ, 110),
		newTestFlameRecord("ingest", "/srv/ingest/parser/csv/normalize/write", 2003, 2203, types.SYS_ENTER_WRITE, 80),
		newTestFlameRecord("ingest", "/srv/ingest/uploader/s3/put/writev", 2003, 2203, types.SYS_ENTER_WRITEV, 75),
		newTestFlameRecord("batch", "/srv/batch/jobs/report/open", 2004, 2204, types.SYS_ENTER_OPENAT, 55),
		newTestFlameRecord("batch", "/srv/batch/jobs/report/close", 2004, 2204, types.SYS_ENTER_CLOSE, 35),
		newTestFlameRecord("batch", "/srv/batch/jobs/report/rename", 2004, 2204, types.SYS_ENTER_RENAMEAT, 20),
	}
}

func newTestFlameRecord(comm, path string, pid, tid uint32, traceID types.TraceId, weight uint64) IterRecord {
	return IterRecord{
		Path:    path,
		TraceID: traceID,
		Comm:    comm,
		Pid:     pid,
		Tid:     tid,
		Cnt: Counter{
			Count:          weight,
			Duration:       weight * 1000,
			DurationToPrev: weight * 350,
			Bytes:          weight * 4096,
		},
	}
}

func withTestFlameWeight(record IterRecord, weight uint64) IterRecord {
	record.Cnt = Counter{
		Count:          weight,
		Duration:       weight * 1000,
		DurationToPrev: weight * 350,
		Bytes:          weight * 4096,
	}
	return record
}

func liveTestWeight(record IterRecord, phase uint64) uint64 {
	base := record.Cnt.Count
	multiplier := uint64(1)

	switch phase {
	case 0:
		if record.Comm == "api" {
			multiplier += 4
		}
		if strings.Contains(record.Path, "/lib/") {
			multiplier += 2
		}
	case 1:
		if record.Comm == "worker" {
			multiplier += 4
		}
		if strings.Contains(record.Path, "/queue/") {
			multiplier += 2
		}
	case 2:
		if record.Comm == "ingest" {
			multiplier += 4
		}
		if strings.Contains(record.Path, "/uploader/") || strings.Contains(record.Path, "/parser/") {
			multiplier += 2
		}
	case 3:
		if record.Comm == "batch" {
			multiplier += 4
		}
		if strings.Contains(record.Path, "/report/") {
			multiplier += 2
		}
	}

	if strings.Contains(record.Path, "/storage/") && phase%2 == 0 {
		multiplier++
	}
	if strings.Contains(record.Path, "/cache/") && phase%2 == 1 {
		multiplier++
	}
	return base * multiplier
}