0
0
Node.jsframework~10 mins

CPU profiling basics in Node.js - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to start CPU profiling using Node.js built-in profiler.

Node.js
const profiler = require('inspector');
profiler.[1]();
Drag options to blanks, or click blank then click option'
AbeginProfiling
BstartProfiling
CinitProfiling
DlaunchProfiling
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name that does not exist like 'beginProfiling'.
Trying to call 'launchProfiling' which is not part of the inspector API.
2fill in blank
medium

Complete the code to stop CPU profiling and save the profile data.

Node.js
const inspector = require('inspector');
const session = new inspector.Session();
session.connect();
session.post('Profiler.[1]', () => {
  session.disconnect();
});
Drag options to blanks, or click blank then click option'
Astop
Bpause
Cend
Dstart
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'pause' which only temporarily halts profiling.
Using 'end' which is not a valid command in this context.
3fill in blank
hard

Fix the error in the code to correctly enable CPU profiling in Node.js.

Node.js
const inspector = require('inspector');
const session = new inspector.Session();
session.connect();
session.post('Profiler.[1]');
Drag options to blanks, or click blank then click option'
AstartProfiling
Bbegin
Cactivate
Denable
Attempts:
3 left
💡 Hint
Common Mistakes
Calling 'startProfiling' without enabling the profiler first.
Using non-existent commands like 'begin' or 'activate'.
4fill in blank
hard

Fill both blanks to create a CPU profile and save it to a file named 'profile.cpuprofile'.

Node.js
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);
});
Drag options to blanks, or click blank then click option'
Astop
Bprofile
Cdata
Dresult
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong command names like 'end' instead of 'stop'.
Using incorrect variable names for the profile data.
5fill in blank
hard

Fill all three blanks to start profiling, enable the profiler, and then stop profiling correctly.

Node.js
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);
});
Drag options to blanks, or click blank then click option'
Aenable
Bstart
Cstop
Dpause
Attempts:
3 left
💡 Hint
Common Mistakes
Skipping the enable step.
Using 'pause' instead of 'stop' to end profiling.