Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

github.com/yinonavraham/go-profiling-demo

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/yinonavraham/go-profiling-demo

  • v0.0.0-20211021052337-9e008f224ab1
  • Source
  • Go
  • Socket score

Version published
Created
Source

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 {
   // ...
}

FAQs

Package last updated on 21 Oct 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc