在nodejs上分析JavaScript代码 - 可能的方法 [英] Profiling JavaScript Code on nodejs - Possible Approaches

查看:119
本文介绍了在nodejs上分析JavaScript代码 - 可能的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是为nodejs开发一个java脚本分析器。
要求如下:


  1. 应该可以获取调用堆栈。

  2. 获取时间戳信息。

  3. 获取迭代次数。

我主要担心的是我不应该修改源文件(.js文件)。



我已经在节点js上看到了JavaScript代码的所有可用性能分析选项。我面临的问题是,大多数需要手动将特定于配置文件的代码注入我的源代码中。以下是一个示例



var profiler = new Profiler() //需要在我的.js文件中创建探查器



profiler.startProfiling()



//我的代码



profiler.endProfling()



由于大多数个人资料需要这种代码注入。
任何人都可以建议我任何其他的分析备选方案(不需要修改源代码)。



目前我正在使用节点js提供的v8功能来分析我的JavaScript代码。例如



node --prof MyTestApp.js



这个命令给了我一个v8.log。这是一个示例日志



所以这是我的疑问


  1. v8是否有可能的解决方法,以便我可以添加时间戳信息,函数的迭代次数

  2. 是否有其他可以满足我要求的分析工具(除了v8之外)。

帮助表示赞赏

解决方案

您可以使用英特尔VTune Amplifier XE来分析JS代码。简而言之,您将能够看到JS函数收集的样本以及这些样本如何通过JS源文件分发。此外,VTune显示完整的调用堆栈信息,其中包括java(JIT)帧和本机帧(本机代码,例如,从JS代码调用的系统库或本机库)。
无需在代码中注入任何内容,但是你应该重建Node.js(它需要不到5分钟)。



如何启用VTune Node.js中的支持


  1. 下载node.js 来源(每晚构建)。
    请注意,上一版本是v0.10.25,但它包含没有VTune支持的v8。在VTune支持下,v0.11.11升级到v8 v.3.22.24.19。

  2. 通过仅添加3行启用VTune支持



    在\src\\\
    ode.cc中添加 2行



       #includev8-vtune.h //位于\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ Init(...){
    ...
    V8 :: SetFlagsFromCommandLine(& v8_argc,const_cast(v8_argv),true);
    vTune :: InitializeVtuneForV8();
    }



    在\ node.gyp中添加 1行用于添加和构建VTune支持的文件



     'target_name':'node',
    'type': 'executable',
    'dependent':[
    'node_js2c#host',
    'deps / v8 / src / third_party / vtune / v8vtune.gyp:v8_vtune',
    ],


  3. 运行位于根节点-v0的vcbuild.bat nosign .11.11文件夹

现在您已准备好使用VTune配置在Node.js中运行的JS代码



如何使用VTune分析Node.js



可以下载VTune 这里。首先尝试评估版本。



我的小应用程序 - test.js

 <预> <代码> 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(完成。);
console.log(word);
}

函数执行(someFunction,value){
someFunction(value);
}

执行(比如,来自Node.js的Hello!); < /代码> < /预>




  1. 打开VTune(bin32 \ amplxe-gui.exe)

  2. 创建新项目

  3. 将node.exe指定为要运行的应用程序,将test.js指定为应用程序的参数

  4. 单击确定,然后单击新建分析

  5. 选择高级热点作为分析类型,并选中热点,堆栈和上下文切换以指定在分析会话期间收集的信息级别。开始分析。

  6. 当集合停止时,VTune将显示通过JS函数分发样本的方式。您可以深入了解函数以查看样本如何通过源行分配给某个JS函数。


My aim is to develop a java script profiler for nodejs . The requirements are as under :

  1. Should be able to fetch call stack .
  2. Get Time stamp information.
  3. 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

  1. Can there be possible workaround for v8 , so that i can add timestamp information ,iteration count for functions
  2. Is there any other profiling tool (apart from v8) which could meet my requirement.

Help is appreciated

解决方案

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

  1. 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.
  2. 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',
      ], 

  3. 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>

  1. Open VTune (bin32\amplxe-gui.exe)
  2. Create a new project
  3. Specify "node.exe" as application to run and "test.js" as app’s parameters
  4. Click OK, then New Analysis
  5. Choose "Advance Hotspots" as analysis type and check "Hotspots, stacks and context switched" to specify level of information collected during profiling session. Start profiling.
  6. 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.

这篇关于在nodejs上分析JavaScript代码 - 可能的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆