@@ -20,6 +20,7 @@ package grpc_test
2020
2121import (
2222 "context"
23+ "fmt"
2324 "io"
2425 "runtime"
2526 "sync"
@@ -29,6 +30,8 @@ import (
2930 "google.golang.org/grpc"
3031 "google.golang.org/grpc/codes"
3132 "google.golang.org/grpc/credentials/insecure"
33+ "google.golang.org/grpc/encoding"
34+ "google.golang.org/grpc/encoding/gzip"
3235 "google.golang.org/grpc/internal/grpcsync"
3336 "google.golang.org/grpc/internal/stubserver"
3437 "google.golang.org/grpc/status"
@@ -330,3 +333,70 @@ func (s) TestServer_GracefulStopWaits(t *testing.T) {
330333 t .Fatalf ("Timed out waiting for second RPC to start on server." )
331334 }
332335}
336+
337+ type nopCloserWriter struct { io.Writer }
338+
339+ func (n nopCloserWriter ) Close () error { return nil }
340+
341+ type noopCompressor struct {
342+ }
343+
344+ func (i * noopCompressor ) Compress (w io.Writer ) (io.WriteCloser , error ) {
345+ return & nopCloserWriter {
346+ Writer : w ,
347+ }, nil
348+ }
349+
350+ func (i * noopCompressor ) Decompress (r io.Reader ) (io.Reader , error ) {
351+ return r , nil
352+ }
353+
354+ func (i * noopCompressor ) Name () string {
355+ return "noop"
356+ }
357+
358+ func BenchmarkRPCCompressor (b * testing.B ) {
359+ encoding .RegisterCompressor (& noopCompressor {})
360+
361+ for _ , comp := range []string {gzip .Name , "noop" } {
362+ for _ , payloadSize := range []int {1024 , 10 * 1024 , 500 * 1024 } {
363+ b .Run (fmt .Sprintf ("comp=%v,payloadSize=%v" , comp , payloadSize ), func (b * testing.B ) {
364+ ss := stubserver.StubServer {
365+ UnaryCallF : func (ctx context.Context , in * testpb.SimpleRequest ) (* testpb.SimpleResponse , error ) {
366+ return & testpb.SimpleResponse {
367+ Username : "test" ,
368+ }, nil
369+ },
370+ }
371+
372+ // Start one RPC to the server.
373+ if err := ss .Start (nil , grpc .WithDefaultCallOptions (grpc .UseCompressor (comp ))); err != nil {
374+ b .Fatal ("Error starting server:" , err )
375+ }
376+ defer ss .Stop ()
377+ paylaod := make ([]byte , payloadSize )
378+ for i := 0 ; i < payloadSize ; i ++ {
379+ paylaod [i ] = byte (i )
380+ }
381+
382+ for i := 0 ; i < b .N ; i ++ {
383+ b .ReportAllocs ()
384+ ctx , cancel := context .WithTimeout (context .Background (), defaultTestTimeout )
385+ defer cancel ()
386+ _ , err := ss .Client .UnaryCall (ctx , & testpb.SimpleRequest {
387+ ResponseType : testpb .PayloadType_COMPRESSABLE ,
388+ ResponseSize : 1024 ,
389+ Payload : & testpb.Payload {
390+ Type : testpb .PayloadType_COMPRESSABLE ,
391+ Body : paylaod ,
392+ },
393+ })
394+
395+ if err != nil {
396+ b .Fatal ("Error staring call:" , err )
397+ }
398+ }
399+ })
400+ }
401+ }
402+ }
0 commit comments