1
0
mirror of https://github.com/golang/go synced 2024-10-04 12:31:21 -06:00

net/http: use RunParallel in benchmarks

LGTM=bradfitz
R=golang-codereviews, bradfitz
CC=golang-codereviews
https://golang.org/cl/68070043
This commit is contained in:
Dmitriy Vyukov 2014-02-24 20:28:14 +04:00
parent b5705ed9ab
commit 96d5229818

View File

@ -26,7 +26,6 @@ import (
"runtime" "runtime"
"strconv" "strconv"
"strings" "strings"
"sync"
"sync/atomic" "sync/atomic"
"syscall" "syscall"
"testing" "testing"
@ -2280,42 +2279,33 @@ func BenchmarkClientServerParallel64(b *testing.B) {
benchmarkClientServerParallel(b, 64) benchmarkClientServerParallel(b, 64)
} }
func benchmarkClientServerParallel(b *testing.B, conc int) { func benchmarkClientServerParallel(b *testing.B, parallelism int) {
b.ReportAllocs() b.ReportAllocs()
b.StopTimer()
ts := httptest.NewServer(HandlerFunc(func(rw ResponseWriter, r *Request) { ts := httptest.NewServer(HandlerFunc(func(rw ResponseWriter, r *Request) {
fmt.Fprintf(rw, "Hello world.\n") fmt.Fprintf(rw, "Hello world.\n")
})) }))
defer ts.Close() defer ts.Close()
b.StartTimer() b.ResetTimer()
b.SetParallelism(parallelism)
numProcs := runtime.GOMAXPROCS(-1) * conc b.RunParallel(func(pb *testing.PB) {
var wg sync.WaitGroup for pb.Next() {
wg.Add(numProcs) res, err := Get(ts.URL)
n := int32(b.N) if err != nil {
for p := 0; p < numProcs; p++ { b.Logf("Get: %v", err)
go func() { continue
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)
}
} }
wg.Done() all, err := ioutil.ReadAll(res.Body)
}() res.Body.Close()
} if err != nil {
wg.Wait() 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. // A benchmark for profiling the server without the HTTP client code.