LoginSignup
1
0

More than 3 years have passed since last update.

Node.jsでプロファイリングをする方法

Posted at

node --prof index.js

--profはプロファイリングと呼ばれる、処理に時間がかかっている様子やどれ位メモリを使っているのかを調べる方法を提供するオプションです。

console
$ node --prof index.js

とすると、index.jsが入っているディレクトリに、

index.jsが入っているディレクトリ
isolate-0x103002a00-v8.log

以上のようなログファイルが生成されます。
しかし、このログファイルは人間が見てもよくわかりません。
そこで、このログファイルを以下のように人間が見やすいファイルにします。

console
$ node --prof-process isolate-0x103002a00-v8.log

すると、コンソールに以下の表示が出ます。

console
>
Statistical profiling result from isolate-0x103002a00-v8.log, (382 ticks, 37 unaccounted, 0 excluded).

 [Shared libraries]:
   ticks  total  nonlib   name
     15    3.9%          /usr/lib/system/libsystem_platform.dylib
      3    0.8%          /usr/lib/system/libdyld.dylib
      2    0.5%          /usr/lib/system/libsystem_pthread.dylib
      1    0.3%          /usr/lib/system/libsystem_malloc.dylib
      1    0.3%          /usr/lib/system/libsystem_kernel.dylib

 [JavaScript]:
   ticks  total  nonlib   name
      1    0.3%    0.3%  Script: ~bootstrapInternalLoaders internal/bootstrap/loaders.js:42:35
      1    0.3%    0.3%  LazyCompile: ~realpathSync fs.js:1380:22
      1    0.3%    0.3%  LazyCompile: ~getOptionValue internal/options.js:6:24
      1    0.3%    0.3%  LazyCompile: ~binding internal/bootstrap/loaders.js:77:39
      1    0.3%    0.3%  LazyCompile: ~QuickSort native array.js:530:19
      1    0.3%    0.3%  Builtin: LoadIC_Uninitialized
      1    0.3%    0.3%  Builtin: Construct {1}

 [C++]:
   ticks  total  nonlib   name
(中略)
[Summary]:
   ticks  total  nonlib   name
      7    1.8%    1.9%  JavaScript
    316   82.7%   87.8%  C++
      7    1.8%    1.9%  GC
     22    5.8%          Shared libraries
     37    9.7%          Unaccounted

この[Summary]という部分に注目すると、どの処理に一番時間がかかっているのか調べることができます。
今回の処理では、C++の処理が全体の87.8%なので、C++の処理を改善すれば良いことがわかります。
その他にも[Bottom up (heavy) profile]という部分には時間のかかった処理が書かれています。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0