Skip to content

Commit

Permalink
add benchmark, support profiling, disable gc
Browse files Browse the repository at this point in the history
  • Loading branch information
joefitzgerald committed Oct 25, 2018
1 parent 1aadac1 commit 809cf46
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,5 @@ fixtures/vendored/bar/barfakes
fixtures/vendored/baz/bazfakes
fixtures/vendored/vendoredfakes
integration/testdata/output
*.profile
*.bench
35 changes: 35 additions & 0 deletions benchmark_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"io/ioutil"
"log"
"path/filepath"
"testing"

"github.com/maxbrunsfeld/counterfeiter/arguments"
)

func BenchmarkSingleRun(b *testing.B) {
b.StopTimer()
workingDir, err := filepath.Abs(filepath.Join(".", "fixtures"))
if err != nil {
b.Fatal(err)
}
log.SetOutput(ioutil.Discard)

args := arguments.ParsedArguments{
GenerateInterfaceAndShimFromPackageDirectory: false,
SourcePackageDir: workingDir,
PackagePath: workingDir,
OutputPath: filepath.Join(workingDir, "fixturesfakes", "fake_something.go"),
DestinationPackageName: "fixturesfakes",
InterfaceName: "Something",
FakeImplName: "FakeSomething",
PrintToStdOut: false,
}

b.StartTimer()
for i := 0; i < b.N; i++ {
doGenerate(workingDir, args)
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f // indirect
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e // indirect
golang.org/x/text v0.3.0 // indirect
golang.org/x/tools v0.0.0-20181011152555-a398e557df60
golang.org/x/tools v0.0.0-20181019201213-3e7aa9e59977
gopkg.in/fsnotify.v1 v1.4.7 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/yaml.v2 v2.2.1 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e h1:o3PsSEY8E4eXWkXrIP9YJALUk
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20181011152555-a398e557df60 h1:PDHN4Zqpfzyio+3C8FTXO2gSMFZa0f8tFifs+K4tdoQ=
golang.org/x/tools v0.0.0-20181011152555-a398e557df60/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20181019201213-3e7aa9e59977 h1:gyGiDZZwunzILdKyr2iu0bvDC9KvK4wItjj+aV+sBro=
golang.org/x/tools v0.0.0-20181019201213-3e7aa9e59977/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
Expand Down
44 changes: 36 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,35 @@ import (
"log"
"os"
"path/filepath"
"runtime/debug"
"runtime/pprof"

"github.com/maxbrunsfeld/counterfeiter/arguments"
"github.com/maxbrunsfeld/counterfeiter/generator"
)

func main() {
debug.SetGCPercent(-1)
profile := false
if os.Getenv("COUNTERFEITER_PROFILE") != "" {
profile = true
}
if profile {
p, err := filepath.Abs(filepath.Join(".", "counterfeiter.profile"))
if err != nil {
fail("%v", err)
}
f, err := os.Create(p)
if err != nil {
fail("%v", err)
}
if err := pprof.StartCPUProfile(f); err != nil {
fail("%v", err)
}
fmt.Printf("Profile: %s\n", p)
defer pprof.StopCPUProfile()
}

log.SetFlags(log.Lshortfile)
if !isDebug() {
log.SetOutput(ioutil.Discard)
Expand Down Expand Up @@ -44,21 +67,26 @@ func isDebug() bool {

func generate(workingDir string, args arguments.ParsedArguments) {
reportStarting(args.PrintToStdOut, args.OutputPath, args.FakeImplName)

b, err := doGenerate(workingDir, args)
if err != nil {
fail("%v", err)
}

printCode(string(b), args.OutputPath, args.PrintToStdOut)
reportDoneSimple(args.PrintToStdOut)
}

func doGenerate(workingDir string, args arguments.ParsedArguments) ([]byte, error) {
mode := generator.InterfaceOrFunction
if args.GenerateInterfaceAndShimFromPackageDirectory {
mode = generator.Package
}
f, err := generator.NewFake(mode, args.InterfaceName, args.PackagePath, args.FakeImplName, args.DestinationPackageName, workingDir)
if err != nil {
fail("%v", err)
}
b, err := f.Generate(true)
if err != nil {
fail("%v", err)
return nil, err
}

printCode(string(b), args.OutputPath, args.PrintToStdOut)
reportDoneSimple(args.PrintToStdOut)
return f.Generate(true)
}

func printCode(code, outputPath string, printToStdOut bool) {
Expand Down

0 comments on commit 809cf46

Please sign in to comment.