1

I'm doing Go training in my company and I want to present in best way pprof package. I think that My example isn't good enough to simply present power of pprof. I want to use only go std library. Code should give me simplier call graph with some bottleneck.

My current workflow is as below: 1. include pprof in webserver 2. run webserver (it'll gather data for 30 seconds (default)) 3. run some curl shots to webserver 4. present pprof graphs

My code looks like below:

package main

import (
    "fmt"
    "net/http"
    _ "net/http/pprof"
)

func handler(w http.ResponseWriter, r *http.Request) {
    for i := 0; i < 100000000; i++ {
        w.Write([]byte(fmt.Sprintf("%d - %d, ", i, i)))
    }
}

func main() {
    http.HandleFunc("/", handler)
    http.ListenAndServe(":8080", nil)
}

Pprof without filtering

ppfof all

Pprof for handler() function only

enter image description here

Jacek Wysocki
  • 1,070
  • 11
  • 24
  • 1
    Can I suggest another tack? The smaller a program is, the nicer its profile looks, and the less useful it is. As you can see from those call graphs, they're just spaghetti, and the program can contain real speedups, but [*there's no way to see them in the call graph*](http://stackoverflow.com/a/25870103/23771). Certainly the speedups can be found, but some cannot be found with the profiler, and when those are the only ones left, they kill. I have a constructive suggestion, which is to at least know how to use [*random pausing*](http://stackoverflow.com/a/378024/23771). – Mike Dunlavey Nov 05 '15 at 20:57

1 Answers1

1

How about string concatenation? It produces a fairly linear graph on my machine. Besides, this is a good way to teach people why they should build strings by appenting to a []byte rather than concatenating strings, creating tonnes of garbage.

func handler(w http.ResponseWriter, r *http.Request) {
    var s string
    for i := 0; i < 100000; i++ {
        s += strconv.Itoa(i)
    }
    w.Write([]byte(s))
}
Ainar-G
  • 34,563
  • 13
  • 93
  • 119