Skip to content

Commit

Permalink
Dramatically increase mock generation speed
Browse files Browse the repository at this point in the history
Mock generation speed is dramatically increased by specifying the
packages to load in config instead of dynamically finding them.

The issue with the previous logic is that a `package.Load` would be
called once per file, even if multiple files were part of the same
package. We can dramatically improve performance by giving a config file
that states which packages should be generated.

Add `with-expecter: True`

Logic fixes
  • Loading branch information
LandonTClipp committed Mar 4, 2023
1 parent 02edbfe commit 39ae11a
Show file tree
Hide file tree
Showing 57 changed files with 2,740 additions and 92 deletions.
11 changes: 11 additions & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,14 @@ quiet: False
all: True
keeptree: True
disable-version-string: True
with-expecter: True
packages:
- github.com/vektra/mockery/v2/pkg
- github.com/vektra/mockery/v2/pkg/fixtures
- github.com/vektra/mockery/v2/pkg/fixtures/buildtag
- github.com/vektra/mockery/v2/pkg/fixtures/constraints
- github.com/vektra/mockery/v2/pkg/fixtures/example_project
- github.com/vektra/mockery/v2/pkg/fixtures/http
- github.com/vektra/mockery/v2/pkg/fixtures/mocks
- github.com/vektra/mockery/v2/pkg/fixtures/test
- github.com/vektra/mockery/v2/pkg/logging
87 changes: 74 additions & 13 deletions cmd/mockery.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,6 @@ func (r *RootApp) Run() error {
}
baseDir = filepath.Dir(pkg.GoFiles[0])
}

var boilerplate string
if r.Config.BoilerplateFile != "" {
data, err := os.ReadFile(r.Config.BoilerplateFile)
Expand All @@ -276,6 +275,16 @@ func (r *RootApp) Run() error {
boilerplate = string(data)
}

buildTags := strings.Split(r.Config.BuildTags, " ")
walker := pkg.Walker{
Config: r.Config,
BaseDir: baseDir,
Recursive: recursive,
Filter: filter,
LimitOne: limitOne,
BuildTags: buildTags,
}

visitor := &pkg.GeneratorVisitor{
Config: r.Config,
InPackage: r.Config.InPackage,
Expand All @@ -287,20 +296,52 @@ func (r *RootApp) Run() error {
StructName: r.Config.StructName,
}

walker := pkg.Walker{
Config: r.Config,
BaseDir: baseDir,
Recursive: recursive,
Filter: filter,
LimitOne: limitOne,
BuildTags: strings.Split(r.Config.BuildTags, " "),
if r.Config.Packages != nil {
warnAlpha(
ctx,
"use of the 'packages' config variable is currently in an alpha state. Use at your own risk.",
map[string]any{
"discussion": "https://github.com/vektra/mockery/discussions/549",
})
parser := pkg.NewParser(buildTags)

if err := parser.ParsePackages(ctx, r.Config.Packages); err != nil {
log.Error().Err(err).Msg("unable to parse packages")
return err
}
log.Info().Msg("done parsing, loading")
if err := parser.Load(); err != nil {
log.Err(err).Msgf("failed to load parser")
return nil
}
log.Info().Msg("done loading, visiting interfeace nodes")
for _, iface := range parser.Interfaces() {
log.Info().
Str("interface", iface.QualifiedName).
Msg("generating interface")

if err := visitor.VisitWalk(ctx, iface); err != nil {
log.Err(err).Msg("error visiting interface node")
return err
}
}
} else {
warnDiscussion(
ctx,
"dynamic walking of project is being considered for removal "+
"in v3. Please provide your feedback at the linked discussion.",
map[string]any{
"pr": "https://github.com/vektra/mockery/pull/548",
"discussion": "https://github.com/vektra/mockery/discussions/549",
})

generated := walker.Walk(ctx, visitor)

if r.Config.Name != "" && !generated {
log.Fatal().Msgf("Unable to find '%s' in any go files under this path", r.Config.Name)
}
}

generated := walker.Walk(ctx, visitor)

if r.Config.Name != "" && !generated {
log.Fatal().Msgf("Unable to find '%s' in any go files under this path", r.Config.Name)
}
return nil
}

Expand Down Expand Up @@ -332,3 +373,23 @@ func getLogger(levelStr string) (zerolog.Logger, error) {

return log, nil
}

func warn(ctx context.Context, prefix string, message string, fields map[string]any) {
log := zerolog.Ctx(ctx)
event := log.Warn()
if fields != nil {
event = event.Fields(fields)
}
event.Msgf("%s: %s", prefix, message)
}
func warnDeprecation(ctx context.Context, message string, fields map[string]any) {
warn(ctx, "DEPRECATION", message, fields)
}

func warnDiscussion(ctx context.Context, message string, fields map[string]any) {
warn(ctx, "DISCUSSION", message, fields)
}

func warnAlpha(ctx context.Context, message string, fields map[string]any) {
warn(ctx, "ALPHA FEATURE", message, fields)
}
79 changes: 79 additions & 0 deletions mocks/cmd/stackTracer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

74 changes: 74 additions & 0 deletions mocks/pkg/Cleanup.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

104 changes: 104 additions & 0 deletions mocks/pkg/OutputStreamProvider.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 39ae11a

Please sign in to comment.