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
121
122
|
package flamegraph
import (
"ior/internal/types"
"syscall"
"testing"
)
func TestAddPath(t *testing.T) {
iod := newIorData()
path := pathType("testPath")
traceId := types.SYS_ENTER_OPENAT
comm := commType("testComm")
pid := pidType(1234)
tid := tidType(5678)
flags := flagsType(syscall.O_RDONLY)
cnt1 := Counter{Count: 1, Duration: 1000, DurationToPrev: 100}
iod.add(path, traceId, comm, pid, tid, flags, cnt1)
if iod.paths[path][traceId][comm][pid][tid][flags] != cnt1 {
t.Errorf("Expected counter %v, got %v", cnt1, iod.paths[path][traceId][comm][pid][tid][flags])
}
cnt2 := Counter{Count: 2, Duration: 2000, DurationToPrev: 200}
iod.add(path, traceId, comm, pid, tid, flags, cnt2)
resultCnt := cnt1.add(cnt2)
if iod.paths[path][traceId][comm][pid][tid][flags] != resultCnt {
t.Errorf("Expected counter %v, got %v", resultCnt, iod.paths[path][traceId][comm][pid][tid][flags])
}
}
func TestMerge(t *testing.T) {
rdwrFlag := flagsType(syscall.O_RDWR)
roFlag := flagsType(syscall.O_RDONLY)
traceId := types.SYS_ENTER_OPENAT
// Initialize iorData instances with sample data
iod1 := iorData{paths: pathMap{
"path1": {traceId: {"comm1": {100: {1000: {rdwrFlag: Counter{
Count: 10,
Duration: 1000,
DurationToPrev: 100,
}}}}}}}}
iod2 := iorData{paths: pathMap{
"path1": {traceId: {"comm1": {100: {1000: {roFlag: Counter{
Count: 20,
Duration: 2000,
DurationToPrev: 200,
}}}}}}}}
iod3 := iorData{paths: pathMap{
"path2": {traceId: {"comm2": {101: {1000: {roFlag: Counter{
Count: 20,
Duration: 2000,
DurationToPrev: 200,
}}}}}}}}
iod4 := iorData{paths: pathMap{
"path2": {traceId: {"comm2": {101: {1000: {roFlag: Counter{
Count: 40,
Duration: 4000,
DurationToPrev: 400,
}}}}}}}}
t.Log("iod1", iod1)
t.Log("iod2", iod2)
t.Log("iod3", iod3)
t.Log("iod4", iod4)
merged := iod1.merge(iod2).merge(iod3).merge(iod4)
t.Log("merged", merged)
t.Run("Merged correctly", func(t *testing.T) {
if len(merged.paths) != 2 {
t.Errorf("Expected 2 paths, got %d", len(merged.paths))
}
if merged.paths["path1"][traceId]["comm1"][100][1000][rdwrFlag].Count != 10 {
t.Errorf("Expected counter 10, got %d", merged.paths["path1"][1]["comm1"][100][1000][rdwrFlag].Count)
}
if merged.paths["path2"][traceId]["comm2"][101][1000][roFlag].Count != 60 {
t.Errorf("Expected counter 60, got %d", merged.paths["path2"][1]["comm2"][101][1000][roFlag].Count)
}
})
// t.Run("Iterate over lines", func(t *testing.T) {
// expectedLines := []string{
// "path1 ␞ enter_openat ␞ comm1 ␞ 100 ␞ 1000 ␞ O_RDWR ␞ 10 1000 100 0",
// "path1 ␞ enter_openat ␞ comm1 ␞ 100 ␞ 1000 ␞ O_RDONLY ␞ 20 2000 200 0",
// "path2 ␞ enter_openat ␞ comm2 ␞ 101 ␞ 1000 ␞ O_RDONLY ␞ 60 6000 600 0",
// }
// var lines []string
// for line := range merged.lines() {
// lines = append(lines, line)
// }
// if len(lines) != len(expectedLines) {
// t.Errorf("Expected %d lines, got %d", len(expectedLines), len(lines))
// }
// if !bothArraysHaveSameElements(lines, expectedLines) {
// t.Errorf("Expected lines %v, got %v", expectedLines, lines)
// }
// })
}
func bothArraysHaveSameElements(a, b []string) bool {
if len(a) != len(b) {
return false
}
for _, v1 := range a {
found := false
for _, v2 := range b {
if v1 == v2 {
found = true
break
}
}
if !found {
return false
}
}
return true
}
|