├── .gitignore ├── LICENSE ├── README.md ├── app.js ├── docs └── index.html ├── package.json └── spoilers.sh /.gitignore: -------------------------------------------------------------------------------- 1 | # flamegraphing leftovers 2 | perfs.out 3 | perf.data* 4 | isolate-0x* 5 | flamegraph*.htm 6 | 7 | 8 | # Logs 9 | logs 10 | *.log 11 | npm-debug.log* 12 | yarn-debug.log* 13 | yarn-error.log* 14 | 15 | # Runtime data 16 | pids 17 | *.pid 18 | *.seed 19 | *.pid.lock 20 | 21 | # Directory for instrumented libs generated by jscoverage/JSCover 22 | lib-cov 23 | 24 | # Coverage directory used by tools like istanbul 25 | coverage 26 | 27 | # nyc test coverage 28 | .nyc_output 29 | 30 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 31 | .grunt 32 | 33 | # Bower dependency directory (https://bower.io/) 34 | bower_components 35 | 36 | # node-waf configuration 37 | .lock-wscript 38 | 39 | # Compiled binary addons (http://nodejs.org/api/addons.html) 40 | build/Release 41 | 42 | # Dependency directories 43 | node_modules/ 44 | jspm_packages/ 45 | 46 | # Typescript v1 declaration files 47 | typings/ 48 | 49 | # Optional npm cache directory 50 | .npm 51 | 52 | # Optional eslint cache 53 | .eslintcache 54 | 55 | # Optional REPL history 56 | .node_repl_history 57 | 58 | # Output of 'npm pack' 59 | *.tgz 60 | 61 | # Yarn Integrity file 62 | .yarn-integrity 63 | 64 | # dotenv environment variables file 65 | .env 66 | 67 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Zbyszek Tenerowicz 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Flame graph exercise 2 | 3 | ## Preparation 4 | 5 | run `npm install` in here 6 | Don't analyze `app.js` before trying the task 7 | 8 | ## Difficulty 9 | Open app.js and modify `HOW_OBVIOUS_THE_FLAME_GRAPH_SHOULD_BE_ON_SCALE_1_TO_100` 10 | It will affect how hard it is to find the function you're looking for. 11 | 12 | difficulty level | value 13 | --- | --- 14 | a month old baby would spot it | 100 15 | still easy | 30 16 | normal | 10 (default) 17 | hard | 5 18 | please try normal first ;) | 1 19 | 20 | ## Task 1 21 | Run the app through perf. Make the following request: 22 | ``` 23 | curl http://localhost:8080/api/tick 24 | ``` 25 | Or just open it in the browser. 26 | 27 | Generate a flame graph, identify the function in your code that performs the long synchronous computation. 28 | 29 | ## Task 2 30 | Now for something more realistic. 31 | 32 | Start the app, open http://localhost:8080/ to simulate traffic (it repeatedly makes requests to /api/tick), run a short perf on the running process and generate a flame graph. See if you would identify the function under real traffic. Try with shorter sleep and lower profiling frequency too. 33 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | const express = require('express') 2 | const console = require('console') 3 | const levenshtein = require('fast-levenshtein') 4 | 5 | //Tweak this to change how easily spotted the long operation is going to be. 6 | //When set to 100 it should be the only visible operation 7 | const HOW_OBVIOUS_THE_FLAME_GRAPH_SHOULD_BE_ON_SCALE_1_TO_100 = 10 8 | 9 | const someFakeModule = (function someFakeModule () { 10 | return { 11 | calculateStringDistance (a, b) { 12 | //Here's where heavy sunchronous computation happens 13 | return levenshtein.get(a, b, { 14 | useCollator: true 15 | }) 16 | } 17 | } 18 | })() 19 | 20 | const app = express() 21 | 22 | app.get('/', (req, res) => { 23 | res.send(` 24 |

Take a look at the network tab in devtools

25 | 32 | `) 33 | }) 34 | 35 | app.get('/api/tick', (req, res) => { 36 | Promise.resolve('asynchronous flow will make our stacktrace more realistic'.repeat(HOW_OBVIOUS_THE_FLAME_GRAPH_SHOULD_BE_ON_SCALE_1_TO_100)) 37 | .then(text => { 38 | const randomText = Math.random().toString(32).repeat(HOW_OBVIOUS_THE_FLAME_GRAPH_SHOULD_BE_ON_SCALE_1_TO_100) 39 | return someFakeModule.calculateStringDistance(text, randomText) 40 | }) 41 | .then(result => res.end(`result: ${result}`)) 42 | }) 43 | 44 | app.get('/api/end', () => process.exit()) 45 | 46 | app.listen(8080, () => { 47 | console.log(`go to http://localhost:8080/ to generate traffic`) 48 | }) 49 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "flame-graph-exercise", 3 | "version": "1.0.0", 4 | "private": true, 5 | "scripts": { 6 | "start": "node --perf-basic-prof-only-functions app.js" 7 | }, 8 | "repository": { 9 | "type": "git", 10 | "url": "git+https://github.com/naugtur/node-example-flamegraph.git" 11 | }, 12 | "author": "naugtur", 13 | "license": "ISC", 14 | "dependencies": { 15 | "express": "^4.14.1", 16 | "fast-levenshtein": "^2.0.6" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /spoilers.sh: -------------------------------------------------------------------------------- 1 | 2 | ## 3 | # Try it for yourself first :) 4 | # 5 | # 6 | # 7 | # #### ##### #### # # ###### ##### #### 8 | # # # # # # # # # # # # 9 | # #### # # # # # # ##### # # #### 10 | # # ##### # # # # # ##### # 11 | # # # # # # # # # # # # # 12 | # #### # #### # ###### ###### # # #### 13 | # 14 | # ### 15 | # ## # # ###### ## ##### ### 16 | # # # # # # # # # # ### 17 | # # # ###### ##### # # # # # 18 | # ###### # # # ###### # # 19 | # # # # # # # # # # ### 20 | # # # # # ###### # # ##### ### 21 | # 22 | 23 | 24 | 25 | # Flame graph of a server boot + single request run 26 | # This part can be executed step by step by running: 27 | # bash spoilers.sh 28 | perf record -e cycles:u -g -- node --perf-basic-prof app.js & 29 | sleep 1 30 | curl http://localhost:8080/api/tick 31 | curl http://localhost:8080/api/end 32 | sleep 1 33 | perf script | egrep -v "( __libc_start| LazyCompile | v8::internal::| Builtin:| Stub:| LoadIC:|\[unknown\]| LoadPolymorphicIC:)" | sed 's/ LazyCompile:[*~]\?/ /' > perfs.out 34 | stackvis perf < perfs.out > flamegraph.htm 35 | 36 | rm isolate* 37 | rm perf* 38 | 39 | echo "open flamegraph.htm" 40 | 41 | # End of the executable part 42 | exit 0 43 | ## But here's the more interesting part 44 | # Try this manually: 45 | 46 | npm start #in one terminal 47 | # open it in the browser 48 | # then in the other terminal 49 | perf record -F99 -p `pgrep -n node` -g -- sleep 3 50 | # perf will collect samples for 3 seconds 51 | perf script | egrep -v "( __libc_start| LazyCompile | v8::internal::| Builtin:| Stub:| LoadIC:|\[unknown\]| LoadPolymorphicIC:)" | sed 's/ LazyCompile:[*~]\?/ /' > perfs.out 52 | stackvis perf < perfs.out > flamegraph.htm 53 | --------------------------------------------------------------------------------