Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Profile & Analyze Node.js Applications like a Pro

Profile & Analyze Node.js Applications like a Pro

NodeConf Colombia 2019

Giovanny Gongora

June 21, 2019
Tweet

More Decks by Giovanny Gongora

Other Decks in Programming

Transcript

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

    3 , 2 0 1 9 Profiling & Analyzing Node.js applications Workshop
  2. © 2019 NodeSourc e ok, this time for real, three

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

    is it slow? • Why it did crash? • Was a bad architecture decision?
  4. © 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?
  5. © 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?
  6. © 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?
  7. © 2019 NodeSourc e The more strong/bold/intense the color in

    the flamegraph piece, the more unoptimized.
  8. © 2019 NodeSourc e How GC works: • Root. •

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

    Object. • Primitives. • Non referenced object. R O O O O P P O P
  10. © 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
  11. © 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
  12. © 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
  13. © 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
  14. © 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.
  15. © 2019 NodeSourc e Why we don’t just break things

    while we are not in production… yet
  16. © 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.
  17. © 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
  18. © 2019 NodeSourc e Exercise #3: CPU Profiling • Use

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

    Use N|Solid to profile • Simulate a Load and wait for results to analysis
  20. © 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.
  21. © 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
  22. © 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