/protoc-gen-go-grpc-mock

Generate Go gRPC service mocks compatible with gomock

Primary LanguageGoMIT LicenseMIT

protoc-gen-go-grpc-mock

protoc-gen-go-grpc-mock generates gRPC service mocks compatible with go.uber.org/mock(aka gomock).

What different from gomock

gomock extracts the interfaces from Go source code and generates the mocks. The overhead of parsing Go source code is much higher than that of Protobuf. Using gomock to generate a mock for a bunch of *_grpc.pb.go files is very slow.

As mentioned above, that's why I wrote this plugin to speed up Protobuf Go generations.

Benchmark

Here is the petstore.proto and pre generated petstore_grpc.pb.go.

gomock

hyperfine "mockgen -source=petstore_grpc.pb.go -destination=mock/petstore_grpc_mock.pb.go"
Benchmark #1: mockgen -source=petstore_grpc.pb.go -destination=petstore_grpc_mock.pb.go
  Time (mean ± σ):     622.5 ms ± 117.0 ms    [User: 527.2 ms, System: 768.7 ms]
  Range (min … max):   503.9 ms … 918.9 ms    10 runs

protoc-gen-go-grpc-mock

hyperfine "protoc --go-grpc-mock_out=. petstore.proto"
Benchmark #1: protoc --go-grpc-mock_out=. petstore.proto
  Time (mean ± σ):      26.4 ms ±   7.5 ms    [User: 13.3 ms, System: 12.7 ms]
  Range (min … max):    17.8 ms …  62.8 ms    44 runs

Installation

go install github.com/sorcererxw/protoc-gen-go-grpc-mock@latest

Also required:

Usage

with protoc

protoc --go_out=. --go-grpc_out=. --go-grpc-mock_out=. petstore.proto

with buf

version: v1

plugins:
  - plugin: buf.build/protocolbuffers/go:v1.30.0
    out: .
    opt:
      - paths=source_relative

  - plugin: buf.build/grpc/go:v1.3.0
    out: .
    opt:
      - paths=source_relative

  - plugin: go-grpc-mock
    out: .
    opt: paths=source_relative