Repository: grpc/grpc-go
File: benchmark/benchmain/main.go (Go)

Code before:
		ResponseType: pl.Type,
		ResponseSize: int32(bf.RespSizeBytes),
		Payload:      pl,
	}

	return streams, req, cleanup
}

func prepareMessages(streams [][]testgrpc.BenchmarkService_StreamingCallClient, req *testpb.SimpleRequest) [][]*grpc.PreparedMsg {
	preparedMsg := make([][]*grpc.PreparedMsg, len(streams))
	for cn, connStreams := range streams {
		preparedMsg[cn] = make([]*grpc.PreparedMsg, len(connStreams))
		for pos, stream := range connStreams {
			preparedMsg[cn][pos] = &grpc.PreparedMsg{}
			if err := preparedMsg[cn][pos].Encode(stream, req); err != nil {
				logger.Fatalf("%v.Encode(%v, %v) = %v", preparedMsg[cn][pos], req, stream, err)
			}
		}
	}
	return preparedMsg
}

// Makes a UnaryCall gRPC request using the given BenchmarkServiceClient and
// request and response sizes.
func unaryCaller(client testgrpc.BenchmarkServiceClient, reqSize, respSize int) {
	if err := benchmark.DoUnaryCall(context.TODO(), client, reqSize, respSize); err != nil {
		logger.Fatalf("DoUnaryCall failed: %v", err)
	}
}

func streamCaller(stream testgrpc.BenchmarkService_StreamingCallClient, req any) {
	if err := benchmark.DoStreamingRoundTripPreloaded(stream, req); err != nil {
		logger.Fatalf("DoStreamingRoundTrip failed: %v", err)
	}
}

func runBenchmark(caller rpcCallFunc, start startFunc, stop stopFunc, bf stats.Features, s *stats.Stats, mode string) {
	// if SleepBetweenRPCs > 0 we skip the warmup because otherwise
	// we are going to send a set of simultaneous requests on every connection,
	// which is something we are trying to avoid when using SleepBetweenRPCs.
	if bf.SleepBetweenRPCs == 0 {
		// Warm up connections.
		for i := 0; i < warmupCallCount; i++ {
			for cn := 0; cn < bf.Connections; cn++ {
				caller(cn, 0)
			}
		}
	}

	// Run benchmark.
	start(mode, bf)

Review comment (suggestion):
There is a problem with `DoUnaryCall` function having context as a parameter. It seems like we can just pass `context.Background()` here, but I'm not sure. Please, review

Code after:
		ResponseType: pl.Type,
		ResponseSize: int32(bf.RespSizeBytes),
		Payload:      pl,
	}

	return streams, req, cleanup
}

func prepareMessages(streams [][]testgrpc.BenchmarkService_StreamingCallClient, req *testpb.SimpleRequest) [][]*grpc.PreparedMsg {
	preparedMsg := make([][]*grpc.PreparedMsg, len(streams))
	for cn, connStreams := range streams {
		preparedMsg[cn] = make([]*grpc.PreparedMsg, len(connStreams))
		for pos, stream := range connStreams {
			preparedMsg[cn][pos] = &grpc.PreparedMsg{}
			if err := preparedMsg[cn][pos].Encode(stream, req); err != nil {
				logger.Fatalf("%v.Encode(%v, %v) = %v", preparedMsg[cn][pos], req, stream, err)
			}
		}
	}
	return preparedMsg
}

// Makes a UnaryCall gRPC request using the given BenchmarkServiceClient and
// request and response sizes.
func unaryCaller(client testgrpc.BenchmarkServiceClient, reqSize, respSize int) {
	if err := benchmark.DoUnaryCall(client, reqSize, respSize); err != nil {
		logger.Fatalf("DoUnaryCall failed: %v", err)
	}
}

func streamCaller(stream testgrpc.BenchmarkService_StreamingCallClient, req any) {
	if err := benchmark.DoStreamingRoundTripPreloaded(stream, req); err != nil {
		logger.Fatalf("DoStreamingRoundTrip failed: %v", err)
	}
}

func runBenchmark(caller rpcCallFunc, start startFunc, stop stopFunc, bf stats.Features, s *stats.Stats, mode string) {
	// if SleepBetweenRPCs > 0 we skip the warmup because otherwise
	// we are going to send a set of simultaneous requests on every connection,
	// which is something we are trying to avoid when using SleepBetweenRPCs.
	if bf.SleepBetweenRPCs == 0 {
		// Warm up connections.
		for i := 0; i < warmupCallCount; i++ {
			for cn := 0; cn < bf.Connections; cn++ {
				caller(cn, 0)
			}
		}
	}

	// Run benchmark.
	start(mode, bf)