Profiling JavaScript* Code in Node.js*
This recipe provides configuration steps to rebuild
Node.js
* and enable Intel® VTune™ Amplifier performance analysis for your JavaScript code including mixed-mode call stacks containing JS frames and native frames (native code, for example, system libraries or native libraries called from the JavaScript code).
Content expert
:
Denis PravdinIngredients
This section lists the hardware and software tools used for the performance analysis scenario.
- Application:sample.js. The application is used as a demo and not available for download.
- JavaScript environment:Node.jsversion 8.0.0 with Chrome* V8 version 5.8.283.41
- Performance analysis tools: Intel VTune Amplifier 2018 - Advanced Hotspots analysis
- Starting with the 2020 release, Intel® VTune™ Amplifier has been renamed toIntel® VTune™.Profiler
- Most recipes in theIntel® VTune™Performance Analysis Cookbook are flexible. You can apply them to different versions ofProfilerIntel® VTune™. In some cases, minor adjustments may be required.Profiler
- Get the latest version ofIntel® VTune™:Profiler
- From theIntel® VTune™product page.Profiler
- Download the latest standalone package from the Intel® oneAPI standalone components page.
- Operating system: Windows* 10
Enable VTune Amplifier Support in Node.js
- Downloadnode.jssources (nightly build).
- Run thevcbuild.batscript from the rootnode-v8.0.0folder:echo vcbuild.bat enable-vtuneThis script buildsNode.jswith theVTunesupport to profile JavaScript code.Profiler
- On Linux* systems, avoid using theenable-vtuneflag with thefully-staticconfigure flag. This combination is not compatible and causes theNode.jsenvironment to crash.
- If you use Microsoft Visual Studio* 2015 IDE or higher, make sure to add#define _SILENCE_STDEXT_HASH_DEPRECATION_WARNINGSto thenode-v8.0.0-win\deps\v8\src\third_party\vtune\vtune-jit.ccfile:#include <string.h> #ifdef WIN32 #define _SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS #include <hash_map> using namespace std; #else ...
Profile JavaScript Code Running in Node.js
This recipe uses a sample JavaScript application:
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!");
To profile this application with the VTune Amplifier:
- Launch the VTune Amplifier:amplxe-gui.exe
- Click the New Project
icon on the toolbar to create a new project.
- In theAnalysis Targettab, specifynode.exein theApplicationfield andsample.jsin theApplication parametersfield:
- Switch to theAnalysis Typetab and select theAdvance Hotspotsanalysis type from the left pane and clickStartto run the analysis.Advanced Hotspots analysis was integrated into the generic Hotspots analysis starting with Intel VTune Amplifier 2019, and is available via the Hardware Event-Based Sampling collection mode.
When the analysis is complete, the VTune Amplifier opens the result in the default Hotspots viewpoint. Use the
Bottom-up
window to explore how the samples are distributed through JavaScript functions. Double-click the function that took the most CPU time to execute to view the source code and identify the hottest code line:
