// CPU-only allocation lab; exposes pprof only on loopback. package main import ( "bytes" "flag" "fmt" "hash/crc32" "log" "net/http" "net/http/pprof" "runtime" "sync" "time" ) func main() { addr := flag.Int("port", 19066, "loopback port") flag.Parse() // A lab-only sampling rate so that one request produces a readable profile. runtime.MemProfileRate = 4096 payload := bytes.Repeat([]byte("a"), 4096) pool := sync.Pool{New: func() any { return new(bytes.Buffer) }} mux := http.NewServeMux() mux.HandleFunc("GET /debug/pprof/heap", pprof.Handler("heap").ServeHTTP) mux.HandleFunc("GET /debug/pprof/profile", pprof.Profile) mux.HandleFunc("GET /frames/{mode}", func(w http.ResponseWriter, r *http.Request) { mode := r.PathValue("mode") if mode != "fresh" && mode != "pool" { http.Error(w, "fresh or pool", 400) return } var sum uint32 for i := 0; i < 4096; i++ { if r.Context().Err() != nil { return } var b *bytes.Buffer if mode == "pool" { b = pool.Get().(*bytes.Buffer) b.Reset() } else { b = new(bytes.Buffer) } b.WriteString("data: ") b.Write(payload) sum += crc32.ChecksumIEEE(b.Bytes()) if mode == "pool" { pool.Put(b) } } fmt.Fprintln(w, sum) }) log.Fatal((&http.Server{Addr: fmt.Sprintf("127.0.0.1:%d", *addr), Handler: mux, ReadHeaderTimeout: 3 * time.Second}).ListenAndServe()) }