Pretty print build durations

This commit is contained in:
Chris Gilmer 2020-08-11 11:22:40 -07:00
parent 13d1b50d8a
commit 04f952a2f4
No known key found for this signature in database
GPG Key ID: 9B9B5634A88AB754
4 changed files with 26 additions and 2 deletions

View File

@ -9,6 +9,7 @@ import (
"strconv"
"strings"
"sync"
"time"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclparse"
@ -17,6 +18,7 @@ import (
"github.com/hashicorp/packer/template"
"golang.org/x/sync/semaphore"
"github.com/hako/durafmt"
"github.com/posener/complete"
)
@ -202,6 +204,9 @@ func (c *BuildCommand) RunContext(buildCtx context.Context, cla *BuildArgs) int
log.Printf("Force build: %v", cla.Force)
log.Printf("On error: %v", cla.OnError)
// Get the start of the build command
buildCommandStart := time.Now()
// Run all the builds in parallel and wait for them to complete
var wg sync.WaitGroup
var artifacts = struct {
@ -235,6 +240,9 @@ func (c *BuildCommand) RunContext(buildCtx context.Context, cla *BuildArgs) int
// Run the build in a goroutine
go func() {
// Get the start of the build
buildStart := time.Now()
defer wg.Done()
defer limitParallel.Release(1)
@ -242,13 +250,18 @@ func (c *BuildCommand) RunContext(buildCtx context.Context, cla *BuildArgs) int
log.Printf("Starting build run: %s", name)
runArtifacts, err := b.Run(buildCtx, ui)
// Get the duration of the build and parse it
buildEnd := time.Now()
buildDuration := buildEnd.Sub(buildStart)
fmtBuildDuration := durafmt.Parse(buildDuration).LimitFirstN(2)
if err != nil {
ui.Error(fmt.Sprintf("Build '%s' errored: %s", name, err))
ui.Error(fmt.Sprintf("Build '%s' errored after %s: %s", name, fmtBuildDuration, err))
errors.Lock()
errors.m[name] = err
errors.Unlock()
} else {
ui.Say(fmt.Sprintf("Build '%s' finished.", name))
ui.Say(fmt.Sprintf("Build '%s' finished after %s.", name, fmtBuildDuration))
if nil != runArtifacts {
artifacts.Lock()
artifacts.m[name] = runArtifacts
@ -274,6 +287,12 @@ func (c *BuildCommand) RunContext(buildCtx context.Context, cla *BuildArgs) int
log.Printf("Waiting on builds to complete...")
wg.Wait()
// Get the duration of the buildCommand command and parse it
buildCommandEnd := time.Now()
buildCommandDuration := buildCommandEnd.Sub(buildCommandStart)
fmtBuildCommandDuration := durafmt.Parse(buildCommandDuration).LimitFirstN(2)
c.Ui.Say(fmt.Sprintf("\n==> Wait completed after %s", fmtBuildCommandDuration))
if err := buildCtx.Err(); err != nil {
c.Ui.Say("Cleanly cancelled builds after being interrupted.")
return 1

1
go.mod
View File

@ -54,6 +54,7 @@ require (
github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e // indirect
github.com/gorilla/websocket v0.0.0-20170319172727-a91eba7f9777 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.1.0
github.com/hako/durafmt v0.0.0-20200710122514-c0fb7b4da026
github.com/hashicorp/consul/api v1.4.0
github.com/hashicorp/errwrap v1.0.0
github.com/hashicorp/go-checkpoint v0.0.0-20171009173528-1545e56e46de

2
go.sum
View File

@ -286,6 +286,8 @@ github.com/gorilla/websocket v0.0.0-20170319172727-a91eba7f9777 h1:JIM+OacoOJRU3
github.com/gorilla/websocket v0.0.0-20170319172727-a91eba7f9777/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/grpc-ecosystem/go-grpc-middleware v1.1.0 h1:THDBEeQ9xZ8JEaCLyLQqXMMdRqNr0QAUJTIkQAUtFjg=
github.com/grpc-ecosystem/go-grpc-middleware v1.1.0/go.mod h1:f5nM7jw/oeRSadq3xCzHAvxcr8HZnzsqU6ILg/0NiiE=
github.com/hako/durafmt v0.0.0-20200710122514-c0fb7b4da026 h1:BpJ2o0OR5FV7vrkDYfXYVJQeMNWa8RhklZOpW2ITAIQ=
github.com/hako/durafmt v0.0.0-20200710122514-c0fb7b4da026/go.mod h1:5Scbynm8dF1XAPwIwkGPqzkM/shndPm79Jd1003hTjE=
github.com/hashicorp/consul/api v1.4.0 h1:jfESivXnO5uLdH650JU/6AnjRoHrLhULq0FnC3Kp9EY=
github.com/hashicorp/consul/api v1.4.0/go.mod h1:xc8u05kyMa3Wjr9eEAsIAo3dg8+LywT5E/Cl7cNS5nU=
github.com/hashicorp/consul/sdk v0.4.0 h1:zBtCfKJZcJDBvSCkQJch4ulp59m1rATFLKwNo/LYY30=

2
vendor/modules.txt vendored
View File

@ -297,6 +297,8 @@ github.com/gophercloud/utils/openstack/compute/v2/flavors
github.com/gorilla/websocket
# github.com/grpc-ecosystem/go-grpc-middleware v1.1.0
github.com/grpc-ecosystem/go-grpc-middleware
# github.com/hako/durafmt v0.0.0-20200710122514-c0fb7b4da026
github.com/hako/durafmt
# github.com/hashicorp/consul/api v1.4.0
github.com/hashicorp/consul/api
# github.com/hashicorp/errwrap v1.0.0