]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: add new Server benchmark
authorBrad Fitzpatrick <bradfitz@golang.org>
Mon, 4 Jun 2012 15:04:40 +0000 (08:04 -0700)
committerBrad Fitzpatrick <bradfitz@golang.org>
Mon, 4 Jun 2012 15:04:40 +0000 (08:04 -0700)
The new BenchmarkServer avoids profiling the client code
by running it in a child process.

R=rsc
CC=golang-dev
https://golang.org/cl/6260053

src/pkg/net/http/serve_test.go

index cea3387a14543713ec2bfa2768fad0565a620a19..661fb43b97a8ff2d5aecfb83fc8990d3e4eaf6ae 100644 (file)
@@ -20,7 +20,9 @@ import (
        "net/http/httputil"
        "net/url"
        "os"
+       "os/exec"
        "reflect"
+       "strconv"
        "strings"
        "syscall"
        "testing"
@@ -1262,3 +1264,56 @@ func BenchmarkClientServer(b *testing.B) {
 
        b.StopTimer()
 }
+
+// A benchmark for profiling the server without the HTTP client code.
+// The client code runs in a subprocess.
+//
+// For use like:
+//   $ go test -c
+//   $ ./http.test -test.run=XX -test.bench=Benchmarktime=15 -test.cpuprofile=http.prof
+//   $ go tool pprof http.test http.prof
+//   (pprof) web
+func BenchmarkServer(b *testing.B) {
+       // Child process mode;
+       if url := os.Getenv("TEST_BENCH_SERVER_URL"); url != "" {
+               n, err := strconv.Atoi(os.Getenv("TEST_BENCH_CLIENT_N"))
+               if err != nil {
+                       panic(err)
+               }
+               for i := 0; i < n; i++ {
+                       res, err := Get(url)
+                       if err != nil {
+                               log.Panicf("Get:", err)
+                       }
+                       all, err := ioutil.ReadAll(res.Body)
+                       if err != nil {
+                               log.Panicf("ReadAll:", err)
+                       }
+                       body := string(all)
+                       if body != "Hello world.\n" {
+                               log.Panicf("Got body:", body)
+                       }
+               }
+               os.Exit(0)
+               return
+       }
+
+       var res = []byte("Hello world.\n")
+       b.StopTimer()
+       ts := httptest.NewServer(HandlerFunc(func(rw ResponseWriter, r *Request) {
+               rw.Header().Set("Content-Type", "text/html; charset=utf-8")
+               rw.Write(res)
+       }))
+       defer ts.Close()
+       b.StartTimer()
+
+       cmd := exec.Command(os.Args[0], "-test.run=XXXX", "-test.bench=BenchmarkServer")
+       cmd.Env = append([]string{
+               fmt.Sprintf("TEST_BENCH_CLIENT_N=%d", b.N),
+               fmt.Sprintf("TEST_BENCH_SERVER_URL=%s", ts.URL),
+       }, os.Environ()...)
+       out, err := cmd.CombinedOutput()
+       if err != nil {
+               b.Errorf("Test failure: %v, with output: %s", err, out)
+       }
+}