Speed Up GoMock With Conditional Generation

Yes, reflection mode is slow, & yes, source mode is not. 22 December 2019
Posted in: Go mocking

Recently I’ve been frustrated by the slow reflection performance of Go’s mockgen when running go generate ./... on a large project. I’ve found it useful to use the Bourne shell built-in test command to conditionally invoke mockgen iif:

go generate does not implement any kind of parallelism, so the slow performance of mockgen, while in source mode, has become a bit of a drag; thus –

package ordering

import (
	"context"
)

//go:generate sh -c "test client_mock_test.go -nt $GOFILE && exit 0; mockgen -package $GOPACKAGE -destination client_mock_test.go github.com/whatever/project/ordering OrderClient"

type OrderClient interface {
	Create(ctx context.Context, o *OrderRequest) (*OrderResponse, error)
	Status(ctx context.Context, orderRefID string) (*OrderResponse, error)
	Cancel(ctx context.Context, orderRefID string) (*OrderResponse, error)
}

On my fairly large project, this reduces many generate runs from the order of 45 seconds to 2 or 3 seconds.

(The above code sample probably works in source mode, but has been contrived for simplicity.)