Skip to content

Commit 518e692

Browse files
committed
GC Profiling
1 parent e4a6489 commit 518e692

34 files changed

+1742328
-1954622
lines changed

.DS_Store

6 KB
Binary file not shown.

.idea/libraries/NodeShowcase_node_modules.xml

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

+1-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/watcherTasks.xml

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

+757
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

NodeShowcase.iml

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<module type="WEB_MODULE" version="4">
3-
<component name="NewModuleRootManager" inherit-compiler-output="true">
4-
<exclude-output />
3+
<component name="NewModuleRootManager">
54
<content url="file://$MODULE_DIR$" />
65
<orderEntry type="sourceFolder" forTests="false" />
76
<orderEntry type="library" name="NodeShowcase node_modules" level="project" />

app.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// require('./monitoring/dummyagent').init();
2+
13

24

35
// CPU Profiling
@@ -8,7 +10,7 @@
810

911
// See below for callback timings
1012

11-
13+
// require('./monitoring/gc').init('./public/metrics');
1214

1315
var express = require('express');
1416
var path = require('path');
@@ -35,7 +37,7 @@ app.set('view engine', 'jade');
3537

3638
// uncomment after placing your favicon in /public
3739
//app.use(favicon(__dirname + '/public/favicon.ico'));
38-
app.use(logger('dev'));
40+
// app.use(logger('dev'));
3941
app.use(bodyParser.json());
4042
app.use(bodyParser.urlencoded({extended: false}));
4143
app.use(cookieParser());

monitoring/CpuProfiler.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function startProfiling() {
4646
*/
4747
function stopProfiling(id) {
4848
var profile = profiler.stopProfiling(id);
49-
fs.writeFile(_datadir + '/' + id + '.cpuprofile.json', JSON.stringify(profile), function () {
49+
fs.writeFile(_datadir + '/' + id + '.cpuprofile', JSON.stringify(profile), function () {
5050
console.log('Profiler data written');
5151
});
5252
}

monitoring/HeapDump.js

+5-9
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,6 @@ var fs = require('fs');
1010
var profiler = require('v8-profiler');
1111
var _datadir = null;
1212
var nextMBThreshold = 0;
13-
var gcprofiler = require('gc-profiler');
14-
15-
// Listen to GC events
16-
gcprofiler.on('gc', function (info) {
17-
console.log('GC happened');
18-
console.log(info);
19-
});
2013

2114

2215
/**
@@ -26,7 +19,7 @@ gcprofiler.on('gc', function (info) {
2619
*/
2720
module.exports.init = function (datadir) {
2821
_datadir = datadir;
29-
setInterval(tickHeapDump, 5 * 1000);
22+
setInterval(tickHeapDump, 500);
3023
};
3124

3225
/**
@@ -43,9 +36,12 @@ function tickHeapDump() {
4336
*/
4437
function heapDump() {
4538
var memMB = process.memoryUsage().rss / 1048576;
39+
40+
console.log(memMB + '>' + nextMBThreshold);
41+
4642
if (memMB > nextMBThreshold) {
4743
console.log('Current memory usage: %j', process.memoryUsage());
48-
nextMBThreshold += 100;
44+
nextMBThreshold += 50;
4945
var snap = profiler.takeSnapshot('profile');
5046
saveHeapSnapshot(snap, _datadir);
5147
}

monitoring/dummyagent.js

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
'use strict';
2+
3+
var fs = require('fs');
4+
5+
var RuxitAgent = function () {
6+
this.linkId = null;
7+
this.pathId = null;
8+
this.nodeId = null;
9+
};
10+
11+
var randomId = function () {
12+
return Math.floor(Math.random() * (99999 - 10000) + 10000);
13+
};
14+
15+
RuxitAgent.prototype.createAsyncLink = function () {
16+
this.linkId = randomId();
17+
fs.writeSync(1, "Async link created " + this.linkId + "\n\n");
18+
return this.linkId;
19+
};
20+
21+
RuxitAgent.prototype.startSubPath = function () {
22+
this.pathId = randomId();
23+
fs.writeSync(1, "Sub path started " + this.pathId + "\n");
24+
};
25+
26+
RuxitAgent.prototype.startNode = function (name) {
27+
this.nodeId = randomId();
28+
fs.writeSync(1, "Node started " + name + " " + this.nodeId + "\n");
29+
};
30+
31+
RuxitAgent.prototype.endNode = function () {
32+
fs.writeSync(1, "Sub path ended " + this.pathId + "\n");
33+
};
34+
35+
RuxitAgent.prototype.endSubPath = function () {
36+
fs.writeSync(1, "Node ended " + this.nodeId + "\n\n");
37+
};
38+
39+
40+
module.exports.init = function () {
41+
42+
var ruxitAgent = new RuxitAgent();
43+
var ruxitTag = ruxitAgent.createAsyncLink();
44+
45+
var nextTickOrig = process.nextTick;
46+
process.nextTick = function (cb) {
47+
48+
49+
var getStack = function (o) {
50+
var b, e, s;
51+
b = Error.prepareStackTrace;
52+
53+
Error.prepareStackTrace = function (_, stack) {
54+
return stack;
55+
};
56+
e = new Error;
57+
Error.captureStackTrace(e, o);
58+
s = e.stack;
59+
fs.writeSync(1, s + "\n\n");
60+
Error.prepareStackTrace = b;
61+
return s;
62+
};
63+
64+
getStack(this);
65+
66+
arguments[0] = function wrapped() {
67+
ruxitAgent.startSubPath(ruxitTag);
68+
ruxitAgent.startNode("Callback");
69+
try {
70+
cb();
71+
} finally {
72+
ruxitAgent.endNode();
73+
ruxitAgent.endSubPath();
74+
}
75+
};
76+
77+
nextTickOrig.apply(this, arguments);
78+
};
79+
};

monitoring/gc.js

+34-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,30 @@
11
var gcprofiler = require('gc-profiler');
22
var fs = require('fs');
33

4+
var _datadir = null;
45

6+
module.exports.init = function (datadir) {
57

6-
module.exports.init = function (time) {
78

9+
var stamp = Date.now();
10+
var time = process.hrtime();
11+
var runId = Math.round(time[0] * 1e3 + time[1] / 1e6);
12+
_datadir = datadir + '/' + runId + '_';
813

9-
fs.writeFile("/tmp/gc_Scavenge.csv", 'Start;Duration\n', function (err) {
14+
// Preparing CSV files
15+
fs.writeFile(_datadir + 'gc_Scavenge.csv', 'Start;Duration\n', function (err) {
1016
if (err) {
1117
return console.log(err);
1218
}
1319
});
1420

15-
fs.writeFile("/tmp/gc_MarkSweepCompact.csv", 'Start;Duration\n', function (err) {
21+
fs.writeFile(_datadir + 'gc_MarkSweepCompact.csv', 'Start;Duration\n', function (err) {
22+
if (err) {
23+
return console.log(err);
24+
}
25+
});
26+
27+
fs.writeFile(_datadir + 'memory.csv', 'Start;RSS;HeapTotal;HeapUsed\n', function (err) {
1628
if (err) {
1729
return console.log(err);
1830
}
@@ -23,13 +35,29 @@ module.exports.init = function (time) {
2335

2436
var diff = process.hrtime(time);
2537
var ms = (diff[0] * 1e3 + diff[1] / 1e6);
38+
fs.appendFile(_datadir + 'gc_' + info.type + ".csv", (ms - info.duration) + ';' + (info.duration) + "\n", function (err) {
39+
if (err) {
40+
return console.log(err);
41+
}
42+
});
43+
44+
});
2645

27-
console.log(ms - info.duration);
2846

29-
fs.appendFile("/tmp/gc_" + info.type + ".csv", (ms - info.duration) + ';' + (info.duration) + "\n", function (err) {
47+
var profileMemory = function () {
48+
var mem = process.memoryUsage();
49+
var diff = process.hrtime(time);
50+
var ms = (diff[0] * 1e3 + diff[1] / 1e6);
51+
// console.log('append');
52+
fs.appendFile(_datadir + "memory.csv", ms + ';' + mem.rss + ';' + mem.heapTotal + ';' + mem.heapUsed + "\n", function (err) {
3053
if (err) {
3154
return console.log(err);
3255
}
3356
});
34-
});
57+
};
58+
59+
// Profile memory every x ms
60+
setInterval(function () {
61+
profileMemory()
62+
}, 5000);
3563
};

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
"cookie-parser": "~1.3.5",
1212
"debug": "~2.2.0",
1313
"express": "~4.12.4",
14+
"gc-profiler": "^1.3.1",
1415
"jade": "~1.9.2",
1516
"less-middleware": "1.0.x",
1617
"morgan": "~1.5.3",
1718
"serve-favicon": "~2.2.1",
18-
"superagent": "^1.2.0"
19+
"superagent": "^1.2.0",
20+
"v8-profiler": "^5.3.2"
1921
}
2022
}

public/.DS_Store

6 KB
Binary file not shown.

public/burst1.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
}
8383

8484

85-
d3.json("/metrics/profile-1434177717747.cpuprofile.json", function (error, root) {
85+
d3.json("/metrics/profile-1445163184485.cpuprofile", function (error, root) {
8686
if (error) throw error;
8787
var path = svg.selectAll("path")
8888
.data(partition.nodes(root.head))

public/metrics/.DS_Store

6 KB
Binary file not shown.

0 commit comments

Comments
 (0)