go.uber.org/yarpc@v1.72.1/encoding/protobuf/testing/benchmark_test.go (about)

     1  // Copyright (c) 2022 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package testing
    22  
    23  import (
    24  	"io/ioutil"
    25  	"net"
    26  	"testing"
    27  
    28  	"go.uber.org/yarpc/api/transport"
    29  	"go.uber.org/yarpc/internal/grpcctx"
    30  	"go.uber.org/yarpc/internal/prototest/example"
    31  	"go.uber.org/yarpc/internal/prototest/examplepb"
    32  	"go.uber.org/yarpc/internal/prototest/exampleutil"
    33  	"go.uber.org/yarpc/internal/testutils"
    34  	"google.golang.org/grpc"
    35  	"google.golang.org/grpc/grpclog"
    36  )
    37  
    38  func init() {
    39  	grpclog.SetLoggerV2(grpclog.NewLoggerV2(ioutil.Discard, ioutil.Discard, ioutil.Discard))
    40  }
    41  
    42  func BenchmarkIntegrationYARPC(b *testing.B) {
    43  	for _, transportType := range testutils.AllTransportTypes {
    44  		b.Run(transportType.String(), func(b *testing.B) {
    45  			benchmarkForTransportType(b, transportType, func(clients *exampleutil.Clients) error {
    46  				benchmarkIntegrationYARPC(b, clients.KeyValueYARPCClient)
    47  				return nil
    48  			})
    49  		})
    50  	}
    51  }
    52  
    53  func BenchmarkIntegrationGRPCClient(b *testing.B) {
    54  	benchmarkForTransportType(b, testutils.TransportTypeGRPC, func(clients *exampleutil.Clients) error {
    55  		benchmarkIntegrationGRPC(b, clients.KeyValueGRPCClient, clients.ContextWrapper)
    56  		return nil
    57  	})
    58  }
    59  
    60  func BenchmarkIntegrationGRPCAll(b *testing.B) {
    61  	server := grpc.NewServer()
    62  	examplepb.RegisterKeyValueServer(server, example.NewKeyValueYARPCServer())
    63  	listener, err := net.Listen("tcp", "0.0.0.0:1234")
    64  	if err != nil {
    65  		b.Fatal(err.Error())
    66  	}
    67  	go func() { _ = server.Serve(listener) }()
    68  	defer server.Stop()
    69  	grpcClientConn, err := grpc.Dial("0.0.0.0:1234", grpc.WithInsecure())
    70  	if err != nil {
    71  		b.Fatal(err.Error())
    72  	}
    73  	benchmarkIntegrationGRPC(b, examplepb.NewKeyValueClient(grpcClientConn), grpcctx.NewContextWrapper())
    74  }
    75  
    76  func benchmarkForTransportType(b *testing.B, transportType testutils.TransportType, f func(*exampleutil.Clients) error) {
    77  	keyValueYARPCServer := example.NewKeyValueYARPCServer()
    78  	fooYARPCServer := example.NewFooYARPCServer(transport.NewHeaders())
    79  	exampleutil.WithClients(transportType, keyValueYARPCServer, fooYARPCServer, nil, f)
    80  }
    81  
    82  func benchmarkIntegrationYARPC(b *testing.B, keyValueYARPCClient examplepb.KeyValueYARPCClient) {
    83  	b.Run("Get", func(b *testing.B) {
    84  		setValue(keyValueYARPCClient, "foo", "bar")
    85  		b.ResetTimer()
    86  		for i := 0; i < b.N; i++ {
    87  			getValue(keyValueYARPCClient, "foo")
    88  		}
    89  	})
    90  }
    91  
    92  func benchmarkIntegrationGRPC(b *testing.B, keyValueClient examplepb.KeyValueClient, contextWrapper *grpcctx.ContextWrapper) {
    93  	b.Run("Get", func(b *testing.B) {
    94  		setValueGRPC(keyValueClient, contextWrapper, "foo", "bar")
    95  		b.ResetTimer()
    96  		for i := 0; i < b.N; i++ {
    97  			getValueGRPC(keyValueClient, contextWrapper, "foo")
    98  		}
    99  	})
   100  }