├── .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 |