Merge pull request #181 from iamqizhao/master

Add basic standalone benchmark client and server
This commit is contained in:
Qi Zhao 2015-04-29 13:27:01 -07:00
Родитель 97f42dd262 58e0450a3c
Коммит c69a26ced7
3 изменённых файлов: 109 добавлений и 6 удалений

Просмотреть файл

@ -24,26 +24,31 @@ func run(b *testing.B, maxConcurrentCalls int, caller func(testpb.TestServiceCli
}
ch := make(chan int, maxConcurrentCalls*4)
var wg sync.WaitGroup
var (
mu sync.Mutex
wg sync.WaitGroup
)
wg.Add(maxConcurrentCalls)
// Distribute the b.N calls over maxConcurrentCalls workers.
for i := 0; i < maxConcurrentCalls; i++ {
go func() {
for _ = range ch {
start := time.Now()
caller(tc)
elapse := time.Since(start)
mu.Lock()
s.Add(elapse)
mu.Unlock()
}
wg.Done()
}()
}
b.StartTimer()
for i := 0; i < b.N; i++ {
b.StartTimer()
start := time.Now()
ch <- i
elapsed := time.Since(start)
b.StopTimer()
s.Add(elapsed)
}
b.StopTimer()
close(ch)
wg.Wait()
conn.Close()

76
benchmark/client/main.go Normal file
Просмотреть файл

@ -0,0 +1,76 @@
package main
import (
"flag"
"fmt"
"math"
"sync"
"time"
"google.golang.org/grpc/benchmark"
"google.golang.org/grpc/benchmark/stats"
testpb "google.golang.org/grpc/interop/grpc_testing"
)
var (
server = flag.String("server", "", "The server address")
maxConcurrentRPCs = flag.Int("max_concurrent_rpcs", 1, "The max number of concurrent RPCs")
duration = flag.Int("duration", math.MaxInt32, "The duration in seconds to run the benchmark client")
)
func caller(client testpb.TestServiceClient) {
benchmark.DoUnaryCall(client, 1, 1)
}
func closeLoop() {
s := stats.NewStats(256)
conn := benchmark.NewClientConn(*server)
tc := testpb.NewTestServiceClient(conn)
// Warm up connection.
for i := 0; i < 100; i++ {
caller(tc)
}
ch := make(chan int, *maxConcurrentRPCs*4)
var (
mu sync.Mutex
wg sync.WaitGroup
)
wg.Add(*maxConcurrentRPCs)
// Distribute RPCs over maxConcurrentCalls workers.
for i := 0; i < *maxConcurrentRPCs; i++ {
go func() {
for _ = range ch {
start := time.Now()
caller(tc)
elapse := time.Since(start)
mu.Lock()
s.Add(elapse)
mu.Unlock()
}
wg.Done()
}()
}
// Stop the client when time is up.
done := make(chan struct{})
go func() {
<-time.After(time.Duration(*duration) * time.Second)
close(done)
}()
ok := true
for ok {
select {
case ch <- 0:
case <-done:
ok = false
}
}
close(ch)
wg.Wait()
conn.Close()
fmt.Println(s.String())
}
func main() {
flag.Parse()
closeLoop()
}

22
benchmark/server/main.go Normal file
Просмотреть файл

@ -0,0 +1,22 @@
package main
import (
"flag"
"fmt"
"math"
"time"
"google.golang.org/grpc/benchmark"
)
var (
duration = flag.Int("duration", math.MaxInt32, "The duration in seconds to run the benchmark server")
)
func main() {
flag.Parse()
addr, stopper := benchmark.StartServer()
fmt.Println("Server Address: ", addr)
<-time.After(time.Duration(*duration) * time.Second)
stopper()
}