2015-03-18 00:27:34 +03:00
|
|
|
package benchmark
|
|
|
|
|
|
|
|
import (
|
2015-04-24 02:25:16 +03:00
|
|
|
"os"
|
2016-04-22 02:11:27 +03:00
|
|
|
"strconv"
|
2015-03-18 00:27:34 +03:00
|
|
|
"sync"
|
|
|
|
"testing"
|
2015-04-24 02:25:16 +03:00
|
|
|
"time"
|
2015-03-18 00:27:34 +03:00
|
|
|
|
2015-06-02 21:13:43 +03:00
|
|
|
"golang.org/x/net/context"
|
2015-06-12 02:48:04 +03:00
|
|
|
"google.golang.org/grpc"
|
2015-07-22 03:53:59 +03:00
|
|
|
testpb "google.golang.org/grpc/benchmark/grpc_testing"
|
2015-04-24 02:25:16 +03:00
|
|
|
"google.golang.org/grpc/benchmark/stats"
|
2015-03-18 00:27:34 +03:00
|
|
|
)
|
|
|
|
|
2015-06-02 21:13:43 +03:00
|
|
|
func runUnary(b *testing.B, maxConcurrentCalls int) {
|
2015-04-24 02:25:16 +03:00
|
|
|
s := stats.AddStats(b, 38)
|
2015-03-18 00:27:34 +03:00
|
|
|
b.StopTimer()
|
2016-04-22 02:11:27 +03:00
|
|
|
targetPort, stopper := StartServer("localhost:0")
|
2015-03-18 00:27:34 +03:00
|
|
|
defer stopper()
|
2016-04-22 02:11:27 +03:00
|
|
|
conn := NewClientConn(":" + strconv.Itoa(targetPort))
|
2016-04-08 00:13:42 +03:00
|
|
|
tc := testpb.NewBenchmarkServiceClient(conn)
|
2015-03-18 00:27:34 +03:00
|
|
|
|
|
|
|
// Warm up connection.
|
|
|
|
for i := 0; i < 10; i++ {
|
2015-06-02 21:13:43 +03:00
|
|
|
unaryCaller(tc)
|
2015-03-18 00:27:34 +03:00
|
|
|
}
|
|
|
|
ch := make(chan int, maxConcurrentCalls*4)
|
2015-04-29 21:02:40 +03:00
|
|
|
var (
|
|
|
|
mu sync.Mutex
|
|
|
|
wg sync.WaitGroup
|
|
|
|
)
|
2015-03-18 00:27:34 +03:00
|
|
|
wg.Add(maxConcurrentCalls)
|
|
|
|
|
|
|
|
// Distribute the b.N calls over maxConcurrentCalls workers.
|
|
|
|
for i := 0; i < maxConcurrentCalls; i++ {
|
|
|
|
go func() {
|
2015-09-07 00:35:37 +03:00
|
|
|
for range ch {
|
2015-04-29 21:02:40 +03:00
|
|
|
start := time.Now()
|
2015-06-02 21:13:43 +03:00
|
|
|
unaryCaller(tc)
|
2015-04-29 21:02:40 +03:00
|
|
|
elapse := time.Since(start)
|
|
|
|
mu.Lock()
|
|
|
|
s.Add(elapse)
|
|
|
|
mu.Unlock()
|
2015-03-18 00:27:34 +03:00
|
|
|
}
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
2015-04-29 21:20:15 +03:00
|
|
|
b.StartTimer()
|
2015-03-18 00:27:34 +03:00
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
ch <- i
|
|
|
|
}
|
2015-04-29 21:20:15 +03:00
|
|
|
b.StopTimer()
|
2015-03-18 00:27:34 +03:00
|
|
|
close(ch)
|
|
|
|
wg.Wait()
|
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
|
2015-06-02 21:13:43 +03:00
|
|
|
func runStream(b *testing.B, maxConcurrentCalls int) {
|
2015-06-02 00:58:25 +03:00
|
|
|
s := stats.AddStats(b, 38)
|
|
|
|
b.StopTimer()
|
2016-04-22 02:11:27 +03:00
|
|
|
targetPort, stopper := StartServer("localhost:0")
|
2015-06-02 00:58:25 +03:00
|
|
|
defer stopper()
|
2016-04-22 02:11:27 +03:00
|
|
|
conn := NewClientConn(":" + strconv.Itoa(targetPort))
|
2016-04-08 00:13:42 +03:00
|
|
|
tc := testpb.NewBenchmarkServiceClient(conn)
|
2015-06-06 20:08:59 +03:00
|
|
|
|
|
|
|
// Warm up connection.
|
2015-06-02 21:13:43 +03:00
|
|
|
stream, err := tc.StreamingCall(context.Background())
|
|
|
|
if err != nil {
|
2015-09-07 00:35:37 +03:00
|
|
|
b.Fatalf("%v.StreamingCall(_) = _, %v", tc, err)
|
2015-06-02 21:13:43 +03:00
|
|
|
}
|
2015-06-02 00:58:25 +03:00
|
|
|
for i := 0; i < 10; i++ {
|
2016-04-19 00:49:05 +03:00
|
|
|
streamCaller(stream)
|
2015-06-02 00:58:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
2015-06-06 20:08:59 +03:00
|
|
|
stream, err := tc.StreamingCall(context.Background())
|
|
|
|
if err != nil {
|
2015-09-07 00:35:37 +03:00
|
|
|
b.Fatalf("%v.StreamingCall(_) = _, %v", tc, err)
|
2015-06-06 20:08:59 +03:00
|
|
|
}
|
2015-09-07 00:35:37 +03:00
|
|
|
for range ch {
|
2015-06-02 00:58:25 +03:00
|
|
|
start := time.Now()
|
2016-04-19 00:49:05 +03:00
|
|
|
streamCaller(stream)
|
2015-06-02 00:58:25 +03:00
|
|
|
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()
|
|
|
|
}
|
2016-04-08 00:13:42 +03:00
|
|
|
func unaryCaller(client testpb.BenchmarkServiceClient) {
|
2015-03-18 00:27:34 +03:00
|
|
|
DoUnaryCall(client, 1, 1)
|
|
|
|
}
|
|
|
|
|
2016-04-19 00:49:05 +03:00
|
|
|
func streamCaller(stream testpb.BenchmarkService_StreamingCallClient) {
|
|
|
|
DoStreamingRoundTrip(stream, 1, 1)
|
2015-06-02 00:58:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkClientStreamc1(b *testing.B) {
|
2015-06-12 02:48:04 +03:00
|
|
|
grpc.EnableTracing = true
|
2015-06-02 21:13:43 +03:00
|
|
|
runStream(b, 1)
|
2015-06-02 00:58:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkClientStreamc8(b *testing.B) {
|
2015-06-12 02:48:04 +03:00
|
|
|
grpc.EnableTracing = true
|
2015-06-02 21:13:43 +03:00
|
|
|
runStream(b, 8)
|
2015-06-02 00:58:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkClientStreamc64(b *testing.B) {
|
2015-06-12 02:48:04 +03:00
|
|
|
grpc.EnableTracing = true
|
2015-06-02 21:13:43 +03:00
|
|
|
runStream(b, 64)
|
2015-06-02 00:58:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkClientStreamc512(b *testing.B) {
|
2015-06-12 02:48:04 +03:00
|
|
|
grpc.EnableTracing = true
|
2015-06-02 21:13:43 +03:00
|
|
|
runStream(b, 512)
|
2015-06-02 00:58:25 +03:00
|
|
|
}
|
2015-06-02 21:13:43 +03:00
|
|
|
func BenchmarkClientUnaryc1(b *testing.B) {
|
2015-06-12 02:48:04 +03:00
|
|
|
grpc.EnableTracing = true
|
2015-06-02 21:13:43 +03:00
|
|
|
runUnary(b, 1)
|
2015-03-18 00:27:34 +03:00
|
|
|
}
|
|
|
|
|
2015-06-02 21:13:43 +03:00
|
|
|
func BenchmarkClientUnaryc8(b *testing.B) {
|
2015-06-12 02:48:04 +03:00
|
|
|
grpc.EnableTracing = true
|
2015-06-02 21:13:43 +03:00
|
|
|
runUnary(b, 8)
|
2015-03-18 00:27:34 +03:00
|
|
|
}
|
|
|
|
|
2015-06-02 21:13:43 +03:00
|
|
|
func BenchmarkClientUnaryc64(b *testing.B) {
|
2015-06-12 02:48:04 +03:00
|
|
|
grpc.EnableTracing = true
|
2015-06-02 21:13:43 +03:00
|
|
|
runUnary(b, 64)
|
2015-03-18 00:27:34 +03:00
|
|
|
}
|
|
|
|
|
2015-06-02 21:13:43 +03:00
|
|
|
func BenchmarkClientUnaryc512(b *testing.B) {
|
2015-06-12 02:48:04 +03:00
|
|
|
grpc.EnableTracing = true
|
|
|
|
runUnary(b, 512)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkClientStreamNoTracec1(b *testing.B) {
|
|
|
|
grpc.EnableTracing = false
|
|
|
|
runStream(b, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkClientStreamNoTracec8(b *testing.B) {
|
|
|
|
grpc.EnableTracing = false
|
|
|
|
runStream(b, 8)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkClientStreamNoTracec64(b *testing.B) {
|
|
|
|
grpc.EnableTracing = false
|
|
|
|
runStream(b, 64)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkClientStreamNoTracec512(b *testing.B) {
|
|
|
|
grpc.EnableTracing = false
|
|
|
|
runStream(b, 512)
|
|
|
|
}
|
|
|
|
func BenchmarkClientUnaryNoTracec1(b *testing.B) {
|
|
|
|
grpc.EnableTracing = false
|
|
|
|
runUnary(b, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkClientUnaryNoTracec8(b *testing.B) {
|
|
|
|
grpc.EnableTracing = false
|
|
|
|
runUnary(b, 8)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkClientUnaryNoTracec64(b *testing.B) {
|
|
|
|
grpc.EnableTracing = false
|
|
|
|
runUnary(b, 64)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkClientUnaryNoTracec512(b *testing.B) {
|
|
|
|
grpc.EnableTracing = false
|
2015-06-02 21:13:43 +03:00
|
|
|
runUnary(b, 512)
|
2015-03-18 00:27:34 +03:00
|
|
|
}
|
2015-04-24 02:25:16 +03:00
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
os.Exit(stats.RunTestMain(m))
|
|
|
|
}
|