diff --git a/cmd/camget/camget.go b/cmd/camget/camget.go index c228a29b9..17c36f945 100644 --- a/cmd/camget/camget.go +++ b/cmd/camget/camget.go @@ -159,6 +159,8 @@ func main() { if *flagVerbose { log.Printf("HTTP requests: %d\n", httpStats.Requests()) + h1, h2 := httpStats.ProtoVersions() + log.Printf(" responses: %d (h1), %d (h2)\n", h1, h2) } } diff --git a/cmd/camput/camput.go b/cmd/camput/camput.go index 701c8ea7f..a6f70d950 100644 --- a/cmd/camput/camput.go +++ b/cmd/camput/camput.go @@ -69,6 +69,8 @@ func init() { log.Printf("Client stats: %s", stats.String()) if up.transport != nil { log.Printf(" #HTTP reqs: %d", up.transport.Requests()) + h1, h2 := up.transport.ProtoVersions() + log.Printf(" responses: %d (h1), %d (h2)\n", h1, h2) } } } diff --git a/pkg/httputil/transport.go b/pkg/httputil/transport.go index 6fac04f02..11365be9f 100644 --- a/pkg/httputil/transport.go +++ b/pkg/httputil/transport.go @@ -27,8 +27,9 @@ import ( // StatsTransport wraps another RoundTripper (or uses the default one) and // counts the number of HTTP requests performed. type StatsTransport struct { - mu sync.Mutex - reqs int + mu sync.Mutex + reqs int + h1, h2 int // responses by protocol type // Transport optionally specifies the transport to use. // If nil, http.DefaultTransport is used. @@ -44,6 +45,13 @@ func (t *StatsTransport) Requests() int { return t.reqs } +// ProtoVersions returns how many HTTP/1 and HTTP/2 responses were seen. +func (t *StatsTransport) ProtoVersions() (h1, h2 int) { + t.mu.Lock() + defer t.mu.Unlock() + return t.h1, t.h2 +} + func (t *StatsTransport) RoundTrip(req *http.Request) (resp *http.Response, err error) { t.mu.Lock() t.reqs++ @@ -70,6 +78,15 @@ func (t *StatsTransport) RoundTrip(req *http.Request) (resp *http.Response, err log.Printf("(%d) %s %s = error: %v (in %v)", n, req.Method, req.URL, err, td) } } + if resp != nil { + t.mu.Lock() + defer t.mu.Unlock() + if resp.ProtoMajor == 2 { + t.h2++ + } else { + t.h1++ + } + } return }