You can integrate CPUpro directly into your Node.js application to profile specific code blocks and save the results. The API is inspired by console.profile() and console.profileEnd().
Basic Usage (Named Profiles)
Use profiler.profile('name') to start and profiler.profileEnd('name') to stop. The returned object allows you to save the data or generate a report.
const profiler = require('cpupro');
profiler.profile('profileName');
// ... do something
const profile = profiler.profileEnd('profileName');
// Write data to .cpuprofile file
profile.writeToFile('./path/to/demo.cpuprofile');
// Or write a report (the viewer with embedded data) to file
profile.report.writeToFile('report.html');
// Or open the report in a browser
profile.report.open();
Using Profile References
You can use the object returned by profile() directly to avoid managing names.
const profiler = require('cpupro');
const profile = profiler.profile();
// ... do something
// End profiling and open a report in a browser
profile.profileEnd().openReport();
Declarative Actions
You can chain actions at the start of profiling. These actions will be executed automatically when profileEnd() is called or when the process exits if profileEnd() is not explicitly invoked.
const profiler = require('cpupro');
profiler.profile()
.writeToFile('./path/to/demo.cpuprofile');
// No need to call profileEnd() if you want the profile dumped on process exit
const profiler = require('cpupro');
profiler.profile('profileName');
// ... do something
const profile = profiler.profileEnd('profileName');
profile.writeToFile('./path/to/demo.cpuprofile');
profile.report.writeToFile('report.html');
profile.report.open();