Complete the code to start CPU profiling using Node.js built-in profiler.
const profiler = require('inspector'); profiler.[1]();
The startProfiling method starts the CPU profiler session in Node.js.
Complete the code to stop CPU profiling and save the profile data.
const inspector = require('inspector'); const session = new inspector.Session(); session.connect(); session.post('Profiler.[1]', () => { session.disconnect(); });
The stop command stops the CPU profiler and returns the profile data.
Fix the error in the code to correctly enable CPU profiling in Node.js.
const inspector = require('inspector'); const session = new inspector.Session(); session.connect(); session.post('Profiler.[1]');
The enable command must be called before starting profiling to activate the profiler.
Fill both blanks to create a CPU profile and save it to a file named 'profile.cpuprofile'.
const fs = require('fs'); const inspector = require('inspector'); const session = new inspector.Session(); session.connect(); session.post('Profiler.enable'); session.post('Profiler.start', () => { setTimeout(() => { session.post('Profiler.[1]', (err, [2]) => { if (!err) { fs.writeFileSync('profile.cpuprofile', JSON.stringify([2])); } session.disconnect(); }); }, 1000); });
The stop command stops profiling and returns the profile data object.
Fill all three blanks to start profiling, enable the profiler, and then stop profiling correctly.
const inspector = require('inspector'); const session = new inspector.Session(); session.connect(); session.post('Profiler.[1]'); session.post('Profiler.[2]', () => { setTimeout(() => { session.post('Profiler.[3]', (err, profile) => { if (!err) { console.log('Profile collected'); } session.disconnect(); }); }, 500); });
You must first enable the profiler, then start it, and finally stop it to collect the profile.