]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: add parallel client/server benchmark
authorDmitriy Vyukov <dvyukov@google.com>
Fri, 24 Aug 2012 10:19:49 +0000 (14:19 +0400)
committerDmitriy Vyukov <dvyukov@google.com>
Fri, 24 Aug 2012 10:19:49 +0000 (14:19 +0400)
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

index e79e0b604bf0050264e4397cd1ee3d661ebe9784..4624b5f5e4e118e88141dcae56532a10017689df 100644 (file)
@@ -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.
 //