|
| 1 | +package v8go_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + v8 "rogchap.com/v8go" |
| 7 | +) |
| 8 | + |
| 9 | +func TestCPUProfileNode(t *testing.T) { |
| 10 | + t.Parallel() |
| 11 | + |
| 12 | + ctx := v8.NewContext(nil) |
| 13 | + iso := ctx.Isolate() |
| 14 | + defer iso.Dispose() |
| 15 | + defer ctx.Close() |
| 16 | + |
| 17 | + cpuProfiler := v8.NewCPUProfiler(iso) |
| 18 | + defer cpuProfiler.Dispose() |
| 19 | + |
| 20 | + title := "cpuprofilenodetest" |
| 21 | + cpuProfiler.StartProfiling(title) |
| 22 | + |
| 23 | + _, err := ctx.RunScript(profileScript, "script.js") |
| 24 | + fatalIf(t, err) |
| 25 | + val, err := ctx.Global().Get("start") |
| 26 | + fatalIf(t, err) |
| 27 | + fn, err := val.AsFunction() |
| 28 | + fatalIf(t, err) |
| 29 | + timeout, err := v8.NewValue(iso, int32(1000)) |
| 30 | + fatalIf(t, err) |
| 31 | + _, err = fn.Call(ctx.Global(), timeout) |
| 32 | + fatalIf(t, err) |
| 33 | + |
| 34 | + cpuProfile := cpuProfiler.StopProfiling(title) |
| 35 | + if cpuProfile == nil { |
| 36 | + t.Fatal("expected profile not to be nil") |
| 37 | + } |
| 38 | + defer cpuProfile.Delete() |
| 39 | + |
| 40 | + rootNode := cpuProfile.GetTopDownRoot() |
| 41 | + if rootNode == nil { |
| 42 | + t.Fatal("expected top down root not to be nil") |
| 43 | + } |
| 44 | + count := rootNode.GetChildrenCount() |
| 45 | + var startNode *v8.CPUProfileNode |
| 46 | + for i := 0; i < count; i++ { |
| 47 | + if rootNode.GetChild(i).GetFunctionName() == "start" { |
| 48 | + startNode = rootNode.GetChild(i) |
| 49 | + } |
| 50 | + } |
| 51 | + if startNode == nil { |
| 52 | + t.Fatal("expected node not to be nil") |
| 53 | + } |
| 54 | + checkNode(t, startNode, "script.js", "start", 23, 15) |
| 55 | + |
| 56 | + parentName := startNode.GetParent().GetFunctionName() |
| 57 | + if parentName != "(root)" { |
| 58 | + t.Fatalf("expected (root), but got %v", parentName) |
| 59 | + } |
| 60 | + |
| 61 | + fooNode := findChild(t, startNode, "foo") |
| 62 | + checkNode(t, fooNode, "script.js", "foo", 15, 13) |
| 63 | + |
| 64 | + delayNode := findChild(t, fooNode, "delay") |
| 65 | + checkNode(t, delayNode, "script.js", "delay", 12, 15) |
| 66 | + |
| 67 | + barNode := findChild(t, fooNode, "bar") |
| 68 | + checkNode(t, barNode, "script.js", "bar", 13, 13) |
| 69 | + |
| 70 | + loopNode := findChild(t, delayNode, "loop") |
| 71 | + checkNode(t, loopNode, "script.js", "loop", 1, 14) |
| 72 | + |
| 73 | + bazNode := findChild(t, fooNode, "baz") |
| 74 | + checkNode(t, bazNode, "script.js", "baz", 14, 13) |
| 75 | +} |
| 76 | + |
| 77 | +func findChild(t *testing.T, node *v8.CPUProfileNode, functionName string) *v8.CPUProfileNode { |
| 78 | + t.Helper() |
| 79 | + |
| 80 | + var child *v8.CPUProfileNode |
| 81 | + count := node.GetChildrenCount() |
| 82 | + for i := 0; i < count; i++ { |
| 83 | + if node.GetChild(i).GetFunctionName() == functionName { |
| 84 | + child = node.GetChild(i) |
| 85 | + } |
| 86 | + } |
| 87 | + if child == nil { |
| 88 | + t.Fatal("failed to find child node") |
| 89 | + } |
| 90 | + return child |
| 91 | +} |
| 92 | + |
| 93 | +func checkNode(t *testing.T, node *v8.CPUProfileNode, scriptResourceName string, functionName string, line, column int) { |
| 94 | + t.Helper() |
| 95 | + |
| 96 | + if node.GetFunctionName() != functionName { |
| 97 | + t.Fatalf("expected node to have function name %s, but got %s", functionName, node.GetFunctionName()) |
| 98 | + } |
| 99 | + if node.GetScriptResourceName() != scriptResourceName { |
| 100 | + t.Fatalf("expected node to have script resource name %s, but got %s", scriptResourceName, node.GetScriptResourceName()) |
| 101 | + } |
| 102 | + if node.GetLineNumber() != line { |
| 103 | + t.Fatalf("expected node at line %d, but got %d", line, node.GetLineNumber()) |
| 104 | + } |
| 105 | + if node.GetColumnNumber() != column { |
| 106 | + t.Fatalf("expected node at column %d, but got %d", column, node.GetColumnNumber()) |
| 107 | + } |
| 108 | +} |
0 commit comments