github.com/stackdocker/rkt@v0.10.1-0.20151109095037-1aa827478248/Godeps/_workspace/src/google.golang.org/grpc/benchmark/benchmark_test.go (about)

     1  package benchmark
     2  
     3  import (
     4  	"os"
     5  	"sync"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/coreos/rkt/Godeps/_workspace/src/golang.org/x/net/context"
    10  	"github.com/coreos/rkt/Godeps/_workspace/src/google.golang.org/grpc"
    11  	testpb "github.com/coreos/rkt/Godeps/_workspace/src/google.golang.org/grpc/benchmark/grpc_testing"
    12  	"github.com/coreos/rkt/Godeps/_workspace/src/google.golang.org/grpc/benchmark/stats"
    13  	"github.com/coreos/rkt/Godeps/_workspace/src/google.golang.org/grpc/grpclog"
    14  )
    15  
    16  func runUnary(b *testing.B, maxConcurrentCalls int) {
    17  	s := stats.AddStats(b, 38)
    18  	b.StopTimer()
    19  	target, stopper := StartServer("localhost:0")
    20  	defer stopper()
    21  	conn := NewClientConn(target)
    22  	tc := testpb.NewTestServiceClient(conn)
    23  
    24  	// Warm up connection.
    25  	for i := 0; i < 10; i++ {
    26  		unaryCaller(tc)
    27  	}
    28  	ch := make(chan int, maxConcurrentCalls*4)
    29  	var (
    30  		mu sync.Mutex
    31  		wg sync.WaitGroup
    32  	)
    33  	wg.Add(maxConcurrentCalls)
    34  
    35  	// Distribute the b.N calls over maxConcurrentCalls workers.
    36  	for i := 0; i < maxConcurrentCalls; i++ {
    37  		go func() {
    38  			for _ = range ch {
    39  				start := time.Now()
    40  				unaryCaller(tc)
    41  				elapse := time.Since(start)
    42  				mu.Lock()
    43  				s.Add(elapse)
    44  				mu.Unlock()
    45  			}
    46  			wg.Done()
    47  		}()
    48  	}
    49  	b.StartTimer()
    50  	for i := 0; i < b.N; i++ {
    51  		ch <- i
    52  	}
    53  	b.StopTimer()
    54  	close(ch)
    55  	wg.Wait()
    56  	conn.Close()
    57  }
    58  
    59  func runStream(b *testing.B, maxConcurrentCalls int) {
    60  	s := stats.AddStats(b, 38)
    61  	b.StopTimer()
    62  	target, stopper := StartServer("localhost:0")
    63  	defer stopper()
    64  	conn := NewClientConn(target)
    65  	tc := testpb.NewTestServiceClient(conn)
    66  
    67  	// Warm up connection.
    68  	stream, err := tc.StreamingCall(context.Background())
    69  	if err != nil {
    70  		grpclog.Fatalf("%v.StreamingCall(_) = _, %v", tc, err)
    71  	}
    72  	for i := 0; i < 10; i++ {
    73  		streamCaller(tc, stream)
    74  	}
    75  
    76  	ch := make(chan int, maxConcurrentCalls*4)
    77  	var (
    78  		mu sync.Mutex
    79  		wg sync.WaitGroup
    80  	)
    81  	wg.Add(maxConcurrentCalls)
    82  
    83  	// Distribute the b.N calls over maxConcurrentCalls workers.
    84  	for i := 0; i < maxConcurrentCalls; i++ {
    85  		go func() {
    86  			stream, err := tc.StreamingCall(context.Background())
    87  			if err != nil {
    88  				grpclog.Fatalf("%v.StreamingCall(_) = _, %v", tc, err)
    89  			}
    90  			for _ = range ch {
    91  				start := time.Now()
    92  				streamCaller(tc, stream)
    93  				elapse := time.Since(start)
    94  				mu.Lock()
    95  				s.Add(elapse)
    96  				mu.Unlock()
    97  			}
    98  			wg.Done()
    99  		}()
   100  	}
   101  	b.StartTimer()
   102  	for i := 0; i < b.N; i++ {
   103  		ch <- i
   104  	}
   105  	b.StopTimer()
   106  	close(ch)
   107  	wg.Wait()
   108  	conn.Close()
   109  }
   110  func unaryCaller(client testpb.TestServiceClient) {
   111  	DoUnaryCall(client, 1, 1)
   112  }
   113  
   114  func streamCaller(client testpb.TestServiceClient, stream testpb.TestService_StreamingCallClient) {
   115  	DoStreamingRoundTrip(client, stream, 1, 1)
   116  }
   117  
   118  func BenchmarkClientStreamc1(b *testing.B) {
   119  	grpc.EnableTracing = true
   120  	runStream(b, 1)
   121  }
   122  
   123  func BenchmarkClientStreamc8(b *testing.B) {
   124  	grpc.EnableTracing = true
   125  	runStream(b, 8)
   126  }
   127  
   128  func BenchmarkClientStreamc64(b *testing.B) {
   129  	grpc.EnableTracing = true
   130  	runStream(b, 64)
   131  }
   132  
   133  func BenchmarkClientStreamc512(b *testing.B) {
   134  	grpc.EnableTracing = true
   135  	runStream(b, 512)
   136  }
   137  func BenchmarkClientUnaryc1(b *testing.B) {
   138  	grpc.EnableTracing = true
   139  	runUnary(b, 1)
   140  }
   141  
   142  func BenchmarkClientUnaryc8(b *testing.B) {
   143  	grpc.EnableTracing = true
   144  	runUnary(b, 8)
   145  }
   146  
   147  func BenchmarkClientUnaryc64(b *testing.B) {
   148  	grpc.EnableTracing = true
   149  	runUnary(b, 64)
   150  }
   151  
   152  func BenchmarkClientUnaryc512(b *testing.B) {
   153  	grpc.EnableTracing = true
   154  	runUnary(b, 512)
   155  }
   156  
   157  func BenchmarkClientStreamNoTracec1(b *testing.B) {
   158  	grpc.EnableTracing = false
   159  	runStream(b, 1)
   160  }
   161  
   162  func BenchmarkClientStreamNoTracec8(b *testing.B) {
   163  	grpc.EnableTracing = false
   164  	runStream(b, 8)
   165  }
   166  
   167  func BenchmarkClientStreamNoTracec64(b *testing.B) {
   168  	grpc.EnableTracing = false
   169  	runStream(b, 64)
   170  }
   171  
   172  func BenchmarkClientStreamNoTracec512(b *testing.B) {
   173  	grpc.EnableTracing = false
   174  	runStream(b, 512)
   175  }
   176  func BenchmarkClientUnaryNoTracec1(b *testing.B) {
   177  	grpc.EnableTracing = false
   178  	runUnary(b, 1)
   179  }
   180  
   181  func BenchmarkClientUnaryNoTracec8(b *testing.B) {
   182  	grpc.EnableTracing = false
   183  	runUnary(b, 8)
   184  }
   185  
   186  func BenchmarkClientUnaryNoTracec64(b *testing.B) {
   187  	grpc.EnableTracing = false
   188  	runUnary(b, 64)
   189  }
   190  
   191  func BenchmarkClientUnaryNoTracec512(b *testing.B) {
   192  	grpc.EnableTracing = false
   193  	runUnary(b, 512)
   194  }
   195  
   196  func TestMain(m *testing.M) {
   197  	os.Exit(stats.RunTestMain(m))
   198  }