You can use built-in nodejs perf_hooks. For example:
// Dependencies
const { performance, PerformanceObserver } = require('perf_hooks');
performance.mark('start part1');
console.time('part1') //start timer for part 1.
var mysql = require('mysql');
console.timeEnd('part1') //end timer 1 and log how long it took.
performance.mark('end part1');
performance.mark('start part2');
console.time('part2') //start timer for part 2.
var connection = mysql.createConnection({
host: '...',
user: '...',
password: '...'
port: ...
database: '...'
});
console.timeEnd('part2') //end timer 2 and log how long it took.
performance.mark('end part2');
performance.mark('start part3');
console.time('part3') //start timer for part 3.
connection.connect(function (err) {
if (err) throw err;
connection.query("SELECT * FROM table", function (err, result, fields) {
if (err) throw err;
console.log(result);
console.timeEnd('part3') //end timer 3 in the callback and log how long it took.
performance.mark('end part3');
// Create observer to log out all the measurements
const obs = new PerformanceObserver((list) => {
// called once. list contains three items
const measurements = list.getEntriesByType('measure');
measurements.forEach((measurement) => {
console.log(`${measurement.name} ${measurement.duration}`);
});
});
obs.observe({ entryTypes: ['measure'] });
// Gather all the measurements
performance.measure('Beggining to end', 'start part1', 'end part3'); // Measure whole function
performance.measure('Part1', 'start part1', 'end part1'); // Measure the part1
performance.measure('Part2', 'start part2', 'end part2'); // Measure the part2
performance.measure('Part3', 'start part3', 'end part3'); // Measure the part3
});
connection.end();
});