Slide 1

Slide 1 text

D E C E M B E R 1 5 , 2 0 1 9 Workshop: Profile & Analyze Node.js applications Workshop

Slide 2

Slide 2 text

Giovanny [email protected] @gioyik

Slide 3

Slide 3 text

© 2019 NodeSourc e What you expect?

Slide 4

Slide 4 text

© 2019 NodeSourc e What is this really?

Slide 5

Slide 5 text

© 2019 NodeSourc e Who had a performance issue with a Node.js application before?

Slide 6

Slide 6 text

© 2019 NodeSourc e let’s be honest now… two at least

Slide 7

Slide 7 text

© 2019 NodeSourc e ok, this time for real, three times that you admit, we don’t judge

Slide 8

Slide 8 text

© 2019 NodeSourc e Asking yourself real questions:

Slide 9

Slide 9 text

© 2019 NodeSourc e Asking yourself real questions: • Why is it slow?

Slide 10

Slide 10 text

© 2019 NodeSourc e Asking yourself real questions: • Why is it slow? • Why it did crash?

Slide 11

Slide 11 text

© 2019 NodeSourc e Asking yourself real questions: • Why is it slow? • Why it did crash? • Was a bad architecture decision?

Slide 12

Slide 12 text

© 2019 NodeSourc e Asking yourself real questions: • Why is it slow? • Why it did crash? • Was a bad architecture decision? • What am I going to do with my life?

Slide 13

Slide 13 text

© 2019 NodeSourc e Asking yourself real questions: • Why is it slow? • Why it did crash? • Was a bad architecture decision? • What am I going to do with my life? • Should I start reviewing my resume again?

Slide 14

Slide 14 text

© 2019 NodeSourc e Asking yourself real questions: • Why is it slow? • Why it did crash? • Was a bad architecture decision? • What am I going to do with my life? • This didn’t happen in PHP, should I go back to Java?

Slide 15

Slide 15 text

© 2019 NodeSourc e What could happen?

Slide 16

Slide 16 text

© 2019 NodeSourc e Bottlenecks

Slide 17

Slide 17 text

© 2019 NodeSourc e Detect what’s the bottleneck

Slide 18

Slide 18 text

© 2019 NodeSourc e Collect metrics and locate bottlenecks

Slide 19

Slide 19 text

© 2019 NodeSourc e Sample profiling to narrow down the general problem area

Slide 20

Slide 20 text

© 2019 NodeSourc e Isolate the exact area

Slide 21

Slide 21 text

© 2019 NodeSourc e What you should know before we start?

Slide 22

Slide 22 text

© 2019 NodeSourc e Event Loop

Slide 23

Slide 23 text

© 2019 NodeSourc e No parallelism in the Event Loop

Slide 24

Slide 24 text

© 2019 NodeSourc e So, what about workers?

Slide 25

Slide 25 text

© 2019 NodeSourc e https://nodejs.org/en/docs/guides/ event-loop-timers-and-nexttick/

Slide 26

Slide 26 text

© 2019 NodeSourc e https://nodejs.org/en/docs/guides/ dont-block-the-event-loop/

Slide 27

Slide 27 text

© 2019 NodeSourc e Profiling

Slide 28

Slide 28 text

© 2019 NodeSourc e CPU

Slide 29

Slide 29 text

© 2019 NodeSourc e perf

Slide 30

Slide 30 text

© 2019 NodeSourc e Dtrace

Slide 31

Slide 31 text

© 2019 NodeSourc e Flamegraphs

Slide 32

Slide 32 text

© 2019 NodeSourc e The more strong/bold/intense the color in the flamegraph piece, the more unoptimized.

Slide 33

Slide 33 text

© 2019 NodeSourc e 0x

Slide 34

Slide 34 text

© 2019 NodeSourc e Don’t use v8-profiler package

Slide 35

Slide 35 text

© 2019 NodeSourc e Memory

Slide 36

Slide 36 text

© 2019 NodeSourc e Why?

Slide 37

Slide 37 text

© 2019 NodeSourc e How GC works: • Root R

Slide 38

Slide 38 text

© 2019 NodeSourc e How GC works: • Root. • Object. R O O O O

Slide 39

Slide 39 text

© 2019 NodeSourc e How GC works: • Root. • Object. • Primitives. R O O O O P P

Slide 40

Slide 40 text

© 2019 NodeSourc e How GC works: • Root • Object. • Primitives. • Non referenced object. R O O O O P P O P

Slide 41

Slide 41 text

© 2019 NodeSourc e Memory scheme (v8): • Resident Set R E S I D E N T S E T

Slide 42

Slide 42 text

© 2019 NodeSourc e Memory scheme (v8): • Resident Set • Code Segment R E S I D E N T S E T C O D E S E G M E N T

Slide 43

Slide 43 text

© 2019 NodeSourc e Memory scheme (v8): • Resident Set • Code Segment • Stack R E S I D E N T S E T C O D E S E G M E N T S TA C K

Slide 44

Slide 44 text

© 2019 NodeSourc e Memory scheme (v8): • Resident Set • Code Segment • Stack • Heap R E S I D E N T S E T C O D E S E G M E N T S TA C K H E A P

Slide 45

Slide 45 text

© 2019 NodeSourc e Memory scheme (v8): • Resident Set • Code Segment • Stack • Heap • Used heap R E S I D E N T S E T C O D E S E G M E N T S TA C K H E A P R S S 
 H E A P TO TA L 
 H E A P U S E D 
 E X TE RN A L

Slide 46

Slide 46 text

© 2019 NodeSourc e Relation between GC and a memory leak? • Impossible to remove non referenced data. • Portion of memory allocated not released when no longer needed.

Slide 47

Slide 47 text

© 2019 NodeSourc e Tooling

Slide 48

Slide 48 text

© 2019 NodeSourc e Chrome DevTools

Slide 49

Slide 49 text

© 2019 NodeSourc e VSCode

Slide 50

Slide 50 text

© 2019 NodeSourc e ndb

Slide 51

Slide 51 text

© 2019 NodeSourc e N|Solid

Slide 52

Slide 52 text

© 2019 NodeSourc e Node clinic

Slide 53

Slide 53 text

© 2019 NodeSourc e Don’t use node-inspector package

Slide 54

Slide 54 text

© 2019 NodeSourc e Why we don’t just break things while we are not in production… yet

Slide 55

Slide 55 text

© 2019 NodeSourc e https://github.com/Gioyik/perf-workshop

Slide 56

Slide 56 text

© 2019 NodeSourc e Exercise #1: Blocking the Event Loop • Create async code and block the event loop • Setup an express endpoint to the event loop operations • Use ab for testing the operation while non and blocked event loop.

Slide 57

Slide 57 text

© 2019 NodeSourc e Exercise #2: Flamegraphs • Run previous event loop exercises with 0x. • Run a simple rest API and observe more details that can appear in the graph

Slide 58

Slide 58 text

© 2019 NodeSourc e Exercise #3: CPU Profiling • Use Tooling to make a CPU Profile • Analyze the data in the CPU profile

Slide 59

Slide 59 text

© 2019 NodeSourc e Exercise #3: Express + Mongodb • Use N|Solid to profile • Simulate a Load and wait for results to analysis

Slide 60

Slide 60 text

© 2019 NodeSourc e Exercise #4: Memory Profiling • Let’s find the memory leak

Slide 61

Slide 61 text

© 2019 NodeSourc e What should you learn next?

Slide 62

Slide 62 text

© 2019 NodeSourc e Recommendations 62

Slide 63

Slide 63 text

© 2019 NodeSourc e I/O operations are… • Expensive. • A waste of resources if your program is unable to do other work. • Usually a hidden operation.

Slide 64

Slide 64 text

© 2019 NodeSourc e Async FTW! Modern applications spend most of their time waiting for I/O

Slide 65

Slide 65 text

© 2019 NodeSourc e Succeed doing I/O operations: • Choose asynchronous over synchronous • Opt for parallel I/O wherever possible • Don't hog the JavaScript thread with long-running calculations and iterations

Slide 66

Slide 66 text

© 2019 NodeSourc e Blocking the Event Loop • Totally possible • Happens more times we want it to happen (who wants to block it in first place?) • Iterations and complex calculations would prevent the process from handling other events

Slide 67

Slide 67 text

© 2019 NodeSourc e so… how good it was?

Slide 68

Slide 68 text

Thank you. Giovanny [email protected] @gioyik