Skip to content

Instantly share code, notes, and snippets.

@tankala
Created July 28, 2018 12:04
Show Gist options
  • Save tankala/f10182006a2a949ad9ca0f817713868c to your computer and use it in GitHub Desktop.
Save tankala/f10182006a2a949ad9ca0f817713868c to your computer and use it in GitHub Desktop.
Profiling Node.js application using v8-profiler
// Imports
var express = require('express');
const bodyParser = require('body-parser');
const morgan = require('morgan');
const app = express();
const controller = require('./controller');
const profiler = require('v8-profiler');
const fs = require('fs');
// Middlewares
app.use(morgan('dev'));
app.use(bodyParser.json());
app.use(express.static('public'));
// Paths
app.post('/getWordsCountInDesc', function (req, res) {
controller.getWordsCountInDesc(req, res);
});
app.get('/doCPUProfiling/profileId/:profileId/durationInSec/:durationInSec', function (req, res) {
let profileId = req.params['profileId'];
let durationInMilliSec = req.params['durationInSec'] * 1000;
// Start profiling
profiler.startProfiling(profileId);
setTimeout(function () {
stopProfiling(profileId);
}, durationInMilliSec);
res.json({});
});
var stopProfiling = function(profileId) {
let profile = profiler.stopProfiling(profileId);
fs.writeFile(__dirname + '/' + profileId + '.cpuprofile', JSON.stringify(profile), function () {
console.log('Profiler data written');
});
}
app.listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment