]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: use RunParallel in benchmarks
authorDmitriy Vyukov <dvyukov@google.com>
Mon, 24 Feb 2014 16:28:14 +0000 (20:28 +0400)
committerDmitriy Vyukov <dvyukov@google.com>
Mon, 24 Feb 2014 16:28:14 +0000 (20:28 +0400)
LGTM=bradfitz
R=golang-codereviews, bradfitz
CC=golang-codereviews
https://golang.org/cl/68070043

src/pkg/net/http/serve_test.go

index fa481a111e83ae75d40e7d12b7ef215975c2a540..7e306bb02193dbcf4e9f57ae4b706e0e0ce24b10 100644 (file)
@@ -26,7 +26,6 @@ import (
        "runtime"
        "strconv"
        "strings"
-       "sync"
        "sync/atomic"
        "syscall"
        "testing"
@@ -2280,42 +2279,33 @@ func BenchmarkClientServerParallel64(b *testing.B) {
        benchmarkClientServerParallel(b, 64)
 }
 
-func benchmarkClientServerParallel(b *testing.B, conc int) {
+func benchmarkClientServerParallel(b *testing.B, parallelism int) {
        b.ReportAllocs()
-       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)
-                               res.Body.Close()
-                               if err != nil {
-                                       b.Logf("ReadAll: %v", err)
-                                       continue
-                               }
-                               body := string(all)
-                               if body != "Hello world.\n" {
-                                       panic("Got body: " + body)
-                               }
+       b.ResetTimer()
+       b.SetParallelism(parallelism)
+       b.RunParallel(func(pb *testing.PB) {
+               for pb.Next() {
+                       res, err := Get(ts.URL)
+                       if err != nil {
+                               b.Logf("Get: %v", err)
+                               continue
                        }
-                       wg.Done()
-               }()
-       }
-       wg.Wait()
+                       all, err := ioutil.ReadAll(res.Body)
+                       res.Body.Close()
+                       if err != nil {
+                               b.Logf("ReadAll: %v", err)
+                               continue
+                       }
+                       body := string(all)
+                       if body != "Hello world.\n" {
+                               panic("Got body: " + body)
+                       }
+               }
+       })
 }
 
 // A benchmark for profiling the server without the HTTP client code.