问题
My aim is to develop a java script profiler for nodejs . The requirements are as under :
- Should be able to fetch call stack .
- Get Time stamp information.
- Get number of iterations.
My chief concern is that i should not modify the source file ( .js file ) .
I have seen all the available profiling options for JavaScript code on node js . The problem i face is that most of them require manual injection of the profiling specific code into my source code. Here is an example
var profiler = new Profiler() // Need to create profiler in my .js file
profiler.startProfiling()
// My Code
profiler.endProfling()
Since most profilers require this kind of code injection. Can anyone suggest me any other profiling alternative (which will not need source code modification).
Currently i am using v8 functionality provided with node js to profile my JavaScript Code. For example
node --prof MyTestApp.js
This command gives me a v8.log . Here is a sample log
So here are my queries
- Can there be possible workaround for v8 , so that i can add timestamp information ,iteration count for functions
- Is there any other profiling tool (apart from v8) which could meet my requirement.
Help is appreciated
回答1:
You can use Intel VTune Amplifier XE to profile JS code. In short, you will be able to see the samples collected by your JS functions and how these samples distributed through JS source files. In addition, VTune displays complete call stack information which includes java (JIT) frames and native frames (native code, for example, system library or native library called from JS code). No need to inject anything in your code but you should rebuild Node.js (it takes less than 5 mins).
How to enable VTune support in Node.js
- Download node.js sources (nightly build). Note that last release is v0.10.25 but it contains v8 without VTune support. v0.11.11 is upgraded to v8 v.3.22.24.19 with VTune support.
Enable VTune support by adding just 3 lines
Add 2 lines in \src\node.cc
#include "v8-vtune.h" // located in \deps\v8\src\third_party\vtune
void Init(...) { … V8::SetFlagsFromCommandLine(&v8_argc, const_cast(v8_argv), true); vTune::InitializeVtuneForV8(); }
Add 1 line in \node.gyp file to add and build VTune support
'target_name': 'node', 'type': 'executable', 'dependencies': [ 'node_js2c#host', 'deps/v8/src/third_party/vtune/v8vtune.gyp:v8_vtune', ],
- Run "vcbuild.bat nosign" located in root node-v0.11.11 folder
Now you ready to profile JS code running in Node.js using VTune
How to profile Node.js using VTune
VTune can be downloaded here . Try evaluation version first.
My small app - test.js
<pre> <code> function say(word) {
console.log("Calculating ...");
var res = 0;
for (var i = 0; i < 20000; i++) {
for (var j = 0; j < 20000; j++) {
res = i * j / 2;
}
}
console.log("Done.");
console.log(word);
}
function execute(someFunction, value) {
someFunction(value);
}
execute(say, "Hello from Node.js!"); </code> </pre>
- Open VTune (bin32\amplxe-gui.exe)
- Create a new project
- Specify “node.exe” as application to run and “test.js” as app’s parameters
- Click OK, then New Analysis
- Choose “Advance Hotspots” as analysis type and check “Hotspots, stacks and context switched” to specify level of information collected during profiling session. Start profiling.
- When collection is stopped that VTune will display how the samples distributed through JS functions. You can dive into function to see how the samples distributed through source lines for a certain JS function.

来源:https://stackoverflow.com/questions/21571145/profiling-javascript-code-on-nodejs-possible-approaches