Profile & Analyze Node.js Applications like a Pro

Profile & Analyze Node.js Applications like a Pro

NodeConf Colombia 2019

8cf49d39d44f100e1afed5c5a259fed4?s=128

Giovanny Gongora

June 21, 2019
Tweet

Transcript

  1. S E P T E M B E R 2

    3 , 2 0 1 9 Profiling & Analyzing Node.js applications Workshop
  2. Johnny johnny@nodesource.com @johnny4young Giovanny gio@nodesource.com @gioyik

  3. © 2019 NodeSourc e What you expect?

  4. © 2019 NodeSourc e What is this really?

  5. © 2019 NodeSourc e Who had a performance issue with

    a Node.js application before?
  6. © 2019 NodeSourc e let’s be honest now… two at

    least
  7. © 2019 NodeSourc e ok, this time for real, three

    times that you admit, we don’t judge
  8. © 2019 NodeSourc e Asking yourself real questions:

  9. © 2019 NodeSourc e Asking yourself real questions: • Why

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

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

    is it slow? • Why it did crash? • Was a bad architecture decision?
  12. © 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?
  13. © 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?
  14. © 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?
  15. © 2019 NodeSourc e What could happen?

  16. © 2019 NodeSourc e Bottlenecks

  17. © 2019 NodeSourc e Detect what’s the bottleneck

  18. © 2019 NodeSourc e Collect metrics and locate bottlenecks

  19. © 2019 NodeSourc e Sample profiling to narrow down the

    general problem area
  20. © 2019 NodeSourc e Isolate the exact area

  21. © 2019 NodeSourc e What you should know before we

    start?
  22. © 2019 NodeSourc e Event Loop

  23. © 2019 NodeSourc e No parallelism in the Event Loop

  24. © 2019 NodeSourc e So, what about workers?

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

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

  27. © 2019 NodeSourc e Profiling

  28. © 2019 NodeSourc e CPU

  29. © 2019 NodeSourc e perf

  30. © 2019 NodeSourc e Dtrace

  31. © 2019 NodeSourc e Flamegraphs

  32. © 2019 NodeSourc e The more strong/bold/intense the color in

    the flamegraph piece, the more unoptimized.
  33. © 2019 NodeSourc e 0x

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

  35. © 2019 NodeSourc e Memory

  36. © 2019 NodeSourc e Why?

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

  38. © 2019 NodeSourc e How GC works: • Root. •

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

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

    Object. • Primitives. • Non referenced object. R O O O O P P O P
  41. © 2019 NodeSourc e Memory scheme (v8): • Resident Set

    R E S I D E N T S E T
  42. © 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
  43. © 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
  44. © 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
  45. © 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
  46. © 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.
  47. © 2019 NodeSourc e Tooling

  48. © 2019 NodeSourc e Chrome DevTools

  49. © 2019 NodeSourc e VSCode

  50. © 2019 NodeSourc e ndb

  51. © 2019 NodeSourc e N|Solid

  52. © 2019 NodeSourc e Node clinic

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

  54. © 2019 NodeSourc e Why we don’t just break things

    while we are not in production… yet
  55. © 2019 NodeSourc e https://github.com/Gioyik/workshop- nodeconfco19

  56. © 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.
  57. © 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
  58. © 2019 NodeSourc e Exercise #3: CPU Profiling • Use

    Tooling to make a CPU Profile • Analyze the data in the CPU profile
  59. © 2019 NodeSourc e Exercise #5: Express + Mongodb •

    Use N|Solid to profile • Simulate a Load and wait for results to analysis
  60. © 2019 NodeSourc e Exercise #4: Memory Profiling • Let’s

    find the memory leak
  61. © 2019 NodeSourc e What should you learn next?

  62. © 2019 NodeSourc e Recommendations 62

  63. © 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.
  64. © 2019 NodeSourc e Async FTW! Modern applications spend most

    of their time waiting for I/O
  65. © 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
  66. © 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
  67. © 2019 NodeSourc e so… how good it was?

  68. © 2019 NodeSourc e One more thing

  69. © 2019 NodeSourc e twitch.tv/gioyik

  70. Thank you. Johnny johnny@nodesource.com @tbd Giovanny gio@nodesource.com @gioyik