forked from rogchap/v8go
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
cpuprofilenode_test.go
113 lines (93 loc) · 3.14 KB
/
cpuprofilenode_test.go
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
// Copyright 2021 the v8go contributors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package v8go_test
import (
"testing"
v8 "github.com/tommie/v8go"
)
func TestCPUProfileNode(t *testing.T) {
t.Parallel()
ctx := v8.NewContext(nil)
iso := ctx.Isolate()
defer iso.Dispose()
defer ctx.Close()
cpuProfiler := v8.NewCPUProfiler(iso)
defer cpuProfiler.Dispose()
title := "cpuprofilenodetest"
cpuProfiler.StartProfiling(title)
_, err := ctx.RunScript(profileScript, "script.js")
fatalIf(t, err)
val, err := ctx.Global().Get("start")
fatalIf(t, err)
fn, err := val.AsFunction()
fatalIf(t, err)
timeout, err := v8.NewValue(iso, int32(1000))
fatalIf(t, err)
_, err = fn.Call(ctx.Global(), timeout)
fatalIf(t, err)
cpuProfile := cpuProfiler.StopProfiling(title)
if cpuProfile == nil {
t.Fatal("expected profile not to be nil")
}
defer cpuProfile.Delete()
rootNode := cpuProfile.GetTopDownRoot()
if rootNode == nil {
t.Fatal("expected top down root not to be nil")
}
count := rootNode.GetChildrenCount()
var startNode *v8.CPUProfileNode
for i := 0; i < count; i++ {
if rootNode.GetChild(i).GetFunctionName() == "start" {
startNode = rootNode.GetChild(i)
}
}
if startNode == nil {
t.Fatal("expected node not to be nil")
}
checkNode(t, startNode, "script.js", "start", 23, 15)
parentName := startNode.GetParent().GetFunctionName()
if parentName != "(root)" {
t.Fatalf("expected (root), but got %v", parentName)
}
fooNode := findChild(t, startNode, "foo")
checkNode(t, fooNode, "script.js", "foo", 15, 13)
delayNode := findChild(t, fooNode, "delay")
checkNode(t, delayNode, "script.js", "delay", 12, 15)
// TODO: this is flaky? https://github.com/tommie/v8go/actions/runs/7363603520/job/20043211523
barNode := findChild(t, fooNode, "bar")
checkNode(t, barNode, "script.js", "bar", 13, 13)
loopNode := findChild(t, delayNode, "loop")
checkNode(t, loopNode, "script.js", "loop", 1, 14)
bazNode := findChild(t, fooNode, "baz")
checkNode(t, bazNode, "script.js", "baz", 14, 13)
}
func findChild(t *testing.T, node *v8.CPUProfileNode, functionName string) *v8.CPUProfileNode {
t.Helper()
var child *v8.CPUProfileNode
count := node.GetChildrenCount()
for i := 0; i < count; i++ {
if node.GetChild(i).GetFunctionName() == functionName {
child = node.GetChild(i)
}
}
if child == nil {
t.Fatal("failed to find child node")
}
return child
}
func checkNode(t *testing.T, node *v8.CPUProfileNode, scriptResourceName string, functionName string, line, column int) {
t.Helper()
if node.GetFunctionName() != functionName {
t.Fatalf("expected node to have function name %s, but got %s", functionName, node.GetFunctionName())
}
if node.GetScriptResourceName() != scriptResourceName {
t.Fatalf("expected node to have script resource name %s, but got %s", scriptResourceName, node.GetScriptResourceName())
}
if node.GetLineNumber() != line {
t.Fatalf("expected node at line %d, but got %d", line, node.GetLineNumber())
}
if node.GetColumnNumber() != column {
t.Fatalf("expected node at column %d, but got %d", column, node.GetColumnNumber())
}
}