forked from node-inspector/v8-profiler
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathv8-profiler.js
149 lines (123 loc) · 3.43 KB
/
v8-profiler.js
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
var binding = require("./build/Release/profiler"),
extend = require('util')._extend;
function Snapshot() {}
Snapshot.prototype.getHeader = function() {
return {
typeId: this.typeId,
uid: this.uid,
title: this.title
}
}
Snapshot.prototype.compare = function (other) {
var my_objects = this.nodeCounts(),
their_objects = other.nodeCounts(),
diff = {}, i, k, my_val, their_val;
all_keys = Object.keys(my_objects).concat(Object.keys(their_objects)); //has dupes, oh well
for (i = 0; i < all_keys.length; i++) {
k = all_keys[i];
my_val = my_objects[k] || 0;
their_val = their_objects[k] || 0;
diff[k] = their_val - my_val;
}
return diff;
};
Snapshot.prototype.allNodes = function() {
var n = this.nodesCount, i, nodes = [];
for (i = 0; i < n; i++) {
nodes[i] = this.getNode(i);
}
return nodes;
};
Snapshot.prototype.nodeCounts = function() {
var objects = {};
this.allNodes().forEach(function(n){
if(n.type === "Object") {
if (objects[n.name]) {
objects[n.name] += 1;
}
else {
objects[n.name] = 1;
}
}
else {
if (objects[n.type]) {
objects[n.type] += 1;
}
else {
objects[n.type] = 1;
}
}
});
return objects;
};
function CpuProfile() {}
CpuProfile.prototype.getHeader = function() {
return {
typeId: this.typeId,
uid: this.uid,
title: this.title
}
}
var starTime, endTime;
var profiler = {
/*HEAP PROFILER API*/
get snapshots() { return binding.heap.snapshots; },
takeSnapshot: function(name, control) {
var snapshot = binding.heap.takeSnapshot.apply(null, arguments);
snapshot.__proto__ = Snapshot.prototype;
return snapshot;
},
getSnapshot: function(index) {
var snapshot = binding.heap.snapshots[index];
if (!snapshot) return;
snapshot.__proto__ = Snapshot.prototype;
return snapshot;
},
findSnapshot: function(uid) {
var snapshot = binding.heap.snapshots.filter(function(snapshot) {
return snapshot.uid == uid;
})[0];
if (!snapshot) return;
snapshot.__proto__ = Snapshot.prototype;
return snapshot;
},
deleteAllSnapshots: function () {
binding.heap.snapshots.forEach(function(snapshot) {
snapshot.delete();
});
},
startTrackingHeapObjects: binding.heap.startTrackingHeapObjects,
stopTrackingHeapObjects: binding.heap.stopTrackingHeapObjects,
getHeapStats: binding.heap.getHeapStats,
getObjectByHeapObjectId: binding.heap.getObjectByHeapObjectId,
/*CPU PROFILER API*/
get profiles() { return binding.cpu.profiles; },
startProfiling: function(name, recsamples) {
startTime = Date.now();
binding.cpu.startProfiling(name, recsamples);
},
stopProfiling: function(name) {
var profile = binding.cpu.stopProfiling(name);
endTime = Date.now();
profile.__proto__ = CpuProfile.prototype;
if (!profile.startTime) profile.startTime = startTime;
if (!profile.endTime) profile.endTime = endTime;
return profile;
},
getProfile: function(index) {
return binding.cpu.profiles[index];
},
findProfile: function(uid) {
var profile = binding.cpu.profiles.filter(function(profile) {
return profile.uid == uid;
})[0];
return profile;
},
deleteAllProfiles: function() {
binding.cpu.profiles.forEach(function(profile) {
profile.delete();
});
}
};
module.exports = profiler;
process.profiler = profiler;