grpc-go/benchmark/benchmark_test.go

151 строка
2.9 KiB
Go
Исходник Обычный вид История

package benchmark
import (
2015-04-24 02:25:16 +03:00
"os"
"sync"
"testing"
2015-04-24 02:25:16 +03:00
"time"
"golang.org/x/net/context"
testpb "google.golang.org/grpc/benchmark/grpc_testing"
2015-04-24 02:25:16 +03:00
"google.golang.org/grpc/benchmark/stats"
"google.golang.org/grpc/grpclog"
)
func runUnary(b *testing.B, maxConcurrentCalls int) {
2015-04-24 02:25:16 +03:00
s := stats.AddStats(b, 38)
b.StopTimer()
target, stopper := StartServer("localhost:0")
defer stopper()
conn := NewClientConn(target)
tc := testpb.NewTestServiceClient(conn)
// Warm up connection.
for i := 0; i < 10; i++ {
unaryCaller(tc)
}
ch := make(chan int, maxConcurrentCalls*4)
2015-04-29 21:02:40 +03:00
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 {
2015-04-29 21:02:40 +03:00
start := time.Now()
unaryCaller(tc)
2015-04-29 21:02:40 +03:00
elapse := time.Since(start)
mu.Lock()
s.Add(elapse)
mu.Unlock()
}
wg.Done()
}()
}
2015-04-29 21:20:15 +03:00
b.StartTimer()
for i := 0; i < b.N; i++ {
ch <- i
}
2015-04-29 21:20:15 +03:00
b.StopTimer()
close(ch)
wg.Wait()
conn.Close()
}
func runStream(b *testing.B, maxConcurrentCalls int) {
s := stats.AddStats(b, 38)
b.StopTimer()
target, stopper := StartServer("localhost:0")
defer stopper()
conn := NewClientConn(target)
tc := testpb.NewTestServiceClient(conn)
// Warm up connection.
stream, err := tc.StreamingCall(context.Background())
if err != nil {
grpclog.Fatalf("%v.StreamingCall(_) = _, %v", tc, err)
}
for i := 0; i < 10; i++ {
2015-06-03 03:39:51 +03:00
streamCaller(tc, stream)
}
ch := make(chan int, maxConcurrentCalls*4)
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() {
stream, err := tc.StreamingCall(context.Background())
if err != nil {
grpclog.Fatalf("%v.StreamingCall(_) = _, %v", tc, err)
}
for _ = range ch {
start := time.Now()
2015-06-03 03:39:51 +03:00
streamCaller(tc, stream)
elapse := time.Since(start)
mu.Lock()
s.Add(elapse)
mu.Unlock()
}
wg.Done()
}()
}
b.StartTimer()
for i := 0; i < b.N; i++ {
ch <- i
}
b.StopTimer()
close(ch)
wg.Wait()
conn.Close()
}
func unaryCaller(client testpb.TestServiceClient) {
DoUnaryCall(client, 1, 1)
}
2015-06-03 03:39:51 +03:00
func streamCaller(client testpb.TestServiceClient, stream testpb.TestService_StreamingCallClient) {
DoStreamingRoundTrip(client, stream, 1, 1)
}
func BenchmarkClientStreamc1(b *testing.B) {
runStream(b, 1)
}
func BenchmarkClientStreamc8(b *testing.B) {
runStream(b, 8)
}
func BenchmarkClientStreamc64(b *testing.B) {
runStream(b, 64)
}
func BenchmarkClientStreamc512(b *testing.B) {
runStream(b, 512)
}
func BenchmarkClientUnaryc1(b *testing.B) {
runUnary(b, 1)
}
func BenchmarkClientUnaryc8(b *testing.B) {
runUnary(b, 8)
}
func BenchmarkClientUnaryc64(b *testing.B) {
runUnary(b, 64)
}
func BenchmarkClientUnaryc512(b *testing.B) {
runUnary(b, 512)
}
2015-04-24 02:25:16 +03:00
func TestMain(m *testing.M) {
os.Exit(stats.RunTestMain(m))
}