mirror of
https://github.com/golang/go
synced 2024-11-22 09:44:40 -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"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
"syscall"
|
"syscall"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@ -1281,6 +1284,50 @@ func BenchmarkClientServer(b *testing.B) {
|
|||||||
b.StopTimer()
|
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.
|
// A benchmark for profiling the server without the HTTP client code.
|
||||||
// The client code runs in a subprocess.
|
// The client code runs in a subprocess.
|
||||||
//
|
//
|
||||||
|
Loading…
Reference in New Issue
Block a user