How to use profilers with Meteor?

My Meteor application, in server side (node process), is using much more CPU than would be acceptable, and I want to investigate it.

Two simultaneous clients are leading node to use 100% of CPU. It is probably related to a massive use of observers, but I need to investigate it further before changing the whole application.

So, what tool can I use to profile it? How to use it?

You should also take a look at the Meteor-specific Observatory. It's a powerful server- and client-side logging package, with profiling support for arbitrary functions, and "automagical logging of template lifecycle methods, Collection methods (only find is supported so far), and Subscriptions profiling".

NodeTime is a pretty awesome profiling service. It's free to use, which is especially helpful in situations like yours, and is super easy to set-up!

The best solution I found is v8-profiler (plus node-inspector).

Install

  1. Go to [Meteor installation folder]/dev_bundle/lib/node_modules.

  2. exec $ npm install v8-profiler there.

  3. Add in your server code:

Meteor.startup(function() {
  profiler = __meteor_bootstrap__.require("v8-profiler")
  Meteor._debug("Server started!");
});

Use

  • Wherever in your app server code you can profile this way:

    profiler.startProfiling("name");                   //begin cpu profiling
    yourCode();
    var cpuProfile = profiler.stopProfiling("name");   //finish cpu profiling
    
  • Do not forget to run node-inspector

APM, Application Performance Monitoring, is a Meteor package + cloud service developed by Arunoda Susiripala of MeteorHacks fame. It's in beta right now, and it looks very promising:

calls

From the Costly Calls tab, you can drill into methods and identify those that take longest:

screenshot

This 1-minute tutorial video shows just this identification of costly methods, which is probably what you want.

More screenshots

more screenshots