go-profiling-demo

command module
v0.0.0-...-9e008f2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 21, 2021 License: MIT Imports: 4 Imported by: 0

README

Go Profiling Demo

Go application for demoing profiling in Go using standard library tools and packages such as pprof, trace and expvar.

This demo was presented in a talk in a Go meetup hosted by AppsFlyer and JFrog on Oct. 19th 2021. The recording (in Hebrew) is available on YouTube. The slides used together with this demo are also available: Web Application Profiling 101 - Slides (pdf)

This is a simple HTTP server application, serving files from a data directory. It mainly implements the following endpoint:

GET /file/<file-path>

Setup

  1. Clone this repository
  2. Install wrk benchmarking tool
    (used to send concurrent requests to the demo server application)

Demo Flow

Following are the steps of this demo. Follow the links to each step to read what is done in the step and to see the code changes.

  1. Step 0 - Demo preparation and first run
  2. Step 1 - Add the pprof endpoints
  3. Step 2 - Collect CPU profile and visualize it
  4. Step 3 - Use execution tracer to visualize Go routines scheduling, GC events, and more
  5. Step 4 - Collect memory profile and visualize it
  6. Step 5 - Improvement attempt #1
  7. Step 6 - Improvement attempt #2
  8. Step 7 - Improvement attempt #3
  9. Step 8 - Add a custom profile
  10. Step 9 - Expose operational values

Notes

Don't use the default serve mux

Using the default serve mux complicates the ability to put access control and enables other packages to expose endpoints implicitly, similar to how the pprof endpoints are added just by adding an anonymous import. It is advised to use your own serve mux and add the pprof endpoints explicitly, preferably with an auth middleware. For example:

func main() {
   mux := http.NewServeMux()
   mux.HandleFunc("/file/", handleGetFile)
   addPprofHandlers(mux)
   // ...
	log.Fatal(http.ListenAndServe(address, authMiddleware(mux))) 
}

func addPprofHandlers(mux *http.ServeMux) {
	mux.HandleFunc("/debug/pprof/", pprof.Index)
	mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
	mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
	mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
	mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
}

func authMiddleware(next http.Handler) http.Handler {
   // ...
}

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
steps

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL