record per-rpc rtt in benchmark_test

This commit is contained in:
iamqizhao 2015-04-29 11:02:40 -07:00
parent 93ec6daf9c
commit f29a6f2d85
1 changed files with 9 additions and 4 deletions

View File

@ -24,25 +24,30 @@ 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()
}()
}
for i := 0; i < b.N; i++ {
b.StartTimer()
start := time.Now()
ch <- i
elapsed := time.Since(start)
b.StopTimer()
s.Add(elapsed)
}
close(ch)
wg.Wait()