Skip to content

Commit

Permalink
Run benchmark with function call instead of bundling messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimafisk committed May 14, 2020
1 parent 5c219f2 commit b793adc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
3 changes: 2 additions & 1 deletion cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ you need to deploy for your website.`,
// Build creates the compiled app that gets deployed.
func Build() {

defer build.Benchmark(time.Now(), "Total build", BenchmarkFlag)
build.CheckBenchmarkFlag(BenchmarkFlag)
defer build.Benchmark(time.Now(), "Total build")

// Get settings from config file.
siteConfig := readers.GetSiteConfig()
Expand Down
26 changes: 10 additions & 16 deletions cmd/build/benchmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,20 @@ import (
)

var benchmarkFlag bool
var messages []string
var elapsed []time.Duration

// CheckBenchmarkFlag sets global var if --benchmark flag is passed.
func CheckBenchmarkFlag(flag bool) {
// Can't check cmd.BenchmarkFlag directly since circular dependencies aren't allowed.
benchmarkFlag = flag
}

// Benchmark records time for individual build processes.
func Benchmark(start time.Time, message string, BenchmarkFlags ...bool) {
func Benchmark(start time.Time, message string) {

elapsed = append(elapsed, time.Since(start))
messages = append(messages, message)
elapsed := time.Since(start)

// Get length of variadic args (used to mimic optional parameters).
numFlags := len(BenchmarkFlags)
// If the --benchmark flag is true.
if numFlags == 1 && BenchmarkFlags[0] {
// Show time spent on each stage of build process.
for i, m := range messages {
fmt.Printf("\n%s took %s\n", m, elapsed[i])
}
} else if numFlags == 1 {
// If the --benchmark flag is false, just print overall build time.
fmt.Printf("\n%s took %s\n", message, elapsed[0])
if benchmarkFlag {
fmt.Printf("%s took %s\n", message, elapsed)
}

}

0 comments on commit b793adc

Please sign in to comment.