From 75af013229e9d22da8ed3272f9f936016b085365 Mon Sep 17 00:00:00 2001 From: Dmitriy Vyukov Date: Fri, 24 Aug 2012 14:19:49 +0400 Subject: [PATCH] net/http: add parallel client/server benchmark R=bradfitz@golang.org R=bradfitz CC=bradfitz, dave, dsymonds, gobot, golang-dev https://golang.org/cl/6441134 --- src/pkg/net/http/serve_test.go | 47 ++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/pkg/net/http/serve_test.go b/src/pkg/net/http/serve_test.go index e79e0b604bf..4624b5f5e4e 100644 --- a/src/pkg/net/http/serve_test.go +++ b/src/pkg/net/http/serve_test.go @@ -22,8 +22,11 @@ import ( "os" "os/exec" "reflect" + "runtime" "strconv" "strings" + "sync" + "sync/atomic" "syscall" "testing" "time" @@ -1281,6 +1284,50 @@ func BenchmarkClientServer(b *testing.B) { b.StopTimer() } +func BenchmarkClientServerParallel4(b *testing.B) { + benchmarkClientServerParallel(b, 4) +} + +func BenchmarkClientServerParallel64(b *testing.B) { + benchmarkClientServerParallel(b, 64) +} + +func benchmarkClientServerParallel(b *testing.B, conc int) { + b.StopTimer() + ts := httptest.NewServer(HandlerFunc(func(rw ResponseWriter, r *Request) { + fmt.Fprintf(rw, "Hello world.\n") + })) + defer ts.Close() + b.StartTimer() + + numProcs := runtime.GOMAXPROCS(-1) * conc + var wg sync.WaitGroup + wg.Add(numProcs) + n := int32(b.N) + for p := 0; p < numProcs; p++ { + go func() { + for atomic.AddInt32(&n, -1) >= 0 { + res, err := Get(ts.URL) + if err != nil { + b.Logf("Get: %v", err) + continue + } + all, err := ioutil.ReadAll(res.Body) + if err != nil { + b.Logf("ReadAll: %v", err) + continue + } + body := string(all) + if body != "Hello world.\n" { + panic("Got body: " + body) + } + } + wg.Done() + }() + } + wg.Wait() +} + // A benchmark for profiling the server without the HTTP client code. // The client code runs in a subprocess. //