qualialabs / profile

A CPU profiler for Meteor. Profile the Meteor build process, server startup, and server runtime.
MIT License
57 stars 8 forks source link

Meteor CPU Profiler

This package makes it easy to record proper sampling CPU profiles of Meteor on the server. You can open the profile in Chrome for detailed analysis. A CPU profile looks like this in Chrome:

example flamechart

This package supports profiling both the Meteor build process and the Meteor server process.

Installation

$ meteor add qualia:profile

As of version 1.0.0, this package is only compatible with Meteor 1.8.2 or newer. If you are using an older version of Meteor you should stick with version 0.0.13.

Usage

There are two uses of qualia:profile. The first is for profiling Meteor build and reload performance. The second is for profiling Meteor runtime performance.

Build Profiling

To enable profiling Meteor builds, just run you application with the environment variable QUALIA_PROFILE_FOLDER set to the folder where you'd like the CPU profiles to be saved. For example, you might start your application like:

QUALIA_PROFILE_FOLDER=/path/to/profiles meteor

Once this environment variable is set, four different kinds of profiles will be saved to that folder.

  1. initial-build.cpuprofile profiles the build process the very first time it is run.
  2. full-rebuild.cpuprofile profiles the build process when at least one server-side file has changed.
  3. client-rebuild.cpuprofile profiles the build process when only client-side files have changed. Meteor has a fast track for this scenario.
  4. startup.cpuprofile profiles the server process as it boots up.

When trying to figure out why your app takes so long to build/rebuild, it is important to look at all 4 of these different profiles. You should also play around with which files you are using to trigger reloads. Meteor packages rebuild somewhat independently.

Runtime Profiling

To profile the Meteor server at runtime, open the Meteor shell and run the following commands:

import Profiler from "meteor/qualia:profile";

let profileName = 'myprofile';
let profilePath = '/path/to/profiles/myprofile.cpuprofile';
let profileMS = 10000;

Profiler.profileDuration(profileName, profilePath, profileMS);

This will profile your code for ten seconds and save the profile to /path/to/profiles/myprofile.cpuprofile.

You can call Profiler.profileDuration from anywhere in your code, but it is often convenient to call it from the Meteor shell. If you enable the Meteor shell in prod, you can profile live production code.

Reading Profiles

Chrome's javascript profiler is hidden by default. To enable it first open the DevTools main menu:

devtools main menu

After that, select More tools > JavaScript Profiler. The old profiler opens in a new panel called JavaScript Profiler. For more information reference this article.

Once the javascript profiler is enabled click Load to load your *.cpuprofile files. You can now navigate between your uploaded CPU profiles by using the sidebar on the left hand side.

For guidance on how to interpret these profiles, this tutorial is a good first step.