Comparing multiple profiles

One of the really nice features of profiling is that you can compare profiles with one another. If we have two separate measurements from the same program, we can determine whether or not the change we made is having a positive impact on the system. Let's augment our HTTP sleep timing function a little bit: 

  1. Let's add a few extra imports:
package main

import (
"fmt"
"net/http"
_ "net/http/pprof"
"strconv"
"time"
)
  1. Next, we'll augment our handler to take a query string parameter for time:
func main() { 
Handler := func(w http.ResponseWriter, r *http.Request) {
sleepDuration := r.URL.Query().Get("time")
sleepDurationInt, err := strconv.Atoi(sleepDuration)
if err != nil {
fmt.Println("Incorrect value passed as a query string for time")
return
}
sleep(sleepDurationInt)
fmt.Fprintf(w, "Slept for %v Milliseconds", sleepDuration)
}
http.HandleFunc("/", Handler)
http.ListenAndServe(":1234", nil)
}
  1. We'll leave our sleep function exactly the same:
func sleep(sleepTime int) {
time.Sleep(time.Duration(sleepTime) * time.Millisecond)
fmt.Println("Slept for ", sleepTime, " Milliseconds")
}
  1. Now that we have this extra functionality, we can take multiple profiles with different timings just by passing a query parameter to our HTTP handler:
    • We can run our new timed profiling tool:
go run timedHttpProfiling.go
    • In another Terminal, we can start our profiling tool:
curl -s "localhost:1234/debug/pprof/profile?seconds=20" > 5-millisecond-profile.dump
    • We can then make many requests for our new resource:
ab -n 10000 -c 10 http://localhost:1234/?time=5
    • We can then gather a second profile:
curl -s "localhost:1234/debug/pprof/profile?seconds=20" > 10-millisecond-profile.dump
    • Then we make a second request for our new resource, generating a second profile:
ab -n 10000 -c 10 http://localhost:1234/?time=10
  1. We now have two separate profiles available, which are stored in 5-millisecond-profile.dump and 10-millisecond-profile.dump. We can compare these using the same tools as before, setting a base profile and a secondary profile. The following screenshot illustrates this:

Comparing profiles allows us to understand how changes impact our systems.

Let's move on to flame graphs in the next section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.15.99