Skip to content

Instantly share code, notes, and snippets.

@svsh227
Last active December 27, 2019 05:08
Show Gist options
  • Save svsh227/26b875aec2f20c45cc6f6fe5d6545172 to your computer and use it in GitHub Desktop.
Save svsh227/26b875aec2f20c45cc6f6fe5d6545172 to your computer and use it in GitHub Desktop.
Create a Flame Graph for your node app | Profiling nodeJS app
//app.js
const express = require('express');
const console = require('console');
const levenshtein = require('fast-levenshtein');
var arr=[];
const HOW_OBVIOUS_THE_FLAME_GRAPH_SHOULD_BE_ON_SCALE_1_TO_100 = 10;
const someFakeModule = (function someFakeModule () {
return {
calculateStringDistance (a, b) {
return levenshtein.get(a, b, {
useCollator: true
})
}
}
})()
const app = express();
app.get('/', (req, res) => {
res.send(`
<h2>Take a look at the network tab in devtools</h2>
<script>
function loops(func) {
return func().then(_ => setTimeout(loops, 20, func))
}
loops(_ => fetch('api/tick'))
</script>\
`)
});
app.get('/api/tick', (req, res) => {
arr.push({name:'Shubham'});
Promise.resolve('asynchronous flow will make our stacktrace more realistic'.repeat(HOW_OBVIOUS_THE_FLAME_GRAPH_SHOULD_BE_ON_SCALE_1_TO_100))
.then(text => {
const randomText =Math.random().toString(32).repeat(HOW_OBVIOUS_THE_FLAME_GRAPH_SHOULD_BE_ON_SCALE_1_TO_100)
return someFakeModule.calculateStringDistance(text, randomText)
})
.then(result => res.end(`result: ${result}, ${arr.length}`))
});
app.get('/api/end', () => process.exit());
app.listen(8080, () => {
console.log(`go to http://localhost:8080/ to generate traffic`)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment