Search code examples
goprofilinggo-ginpprofheap-profiling

How to use pprof to see memory usage by web server after HTTP requests


The documentation for net/http/pprof explains how to create a 30-second CPU profiling session and analyze the outcome.

This allows me to initiate one or more HTTP requests and see the resulting CPU utilization of my web application.

I see a route for generating a heap profile, but since the profiling doesn't occur over something like a 30-second window, I'm unsure conceptually how this interacts with my web application.

How can I "coordinate" the heap profiler so that it corresponds with one or more HTTP requests?


Solution

  • I don't think you can scope the profiler exactly to a request, but a common methodology is to take multiple, intentionally timed, heap profiles. For example:

    • Take a single profile of your application at an empty state to set a baseline
    • Take a profile (or multiple profiles) while the application is loaded with a request (or multiple requests)
    • Take a profile afterwards

    The powerful part of heap profiles is that pprof allows you to "diff" a profile by specifying 2 profiles! a base profile and a secondary profile to compare the base to!

    Using this it becomes easy to see the difference in allocated objects or total bytes.