mirror of
https://github.com/golang/go
synced 2024-11-22 04:24:39 -07:00
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
This commit is contained in:
parent
8eb05b3843
commit
75af013229
@ -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.
|
||||
//
|
||||
|
Loading…
Reference in New Issue
Block a user