Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shell logger: Use fmt functions once #2805

Merged
merged 1 commit into from
Jun 5, 2024
Merged

Conversation

DrJosh9000
Copy link
Contributor

Description

fmt.Fprintf(w, "%s", fmt.Sprintf(format, v...)) seems silly - and following it with fmt.Fprintln(w) also seems silly.

fmt.Fprintf(w, format+"\n", v...) is faster and does less allocations:

goos: darwin
goarch: arm64
pkg: github.com/buildkite/agent/v3/internal/job/shell
          │ doublefmt.txt │            fmtconcat.txt            │
          │    sec/op     │   sec/op     vs base                │
Printf-10    140.80n ± 0%   87.05n ± 0%  -38.17% (p=0.000 n=30)

          │ doublefmt.txt │           fmtconcat.txt            │
          │     B/op      │   B/op     vs base                 │
Printf-10      40.00 ± 0%   0.00 ± 0%  -100.00% (p=0.000 n=30)

          │ doublefmt.txt │            fmtconcat.txt            │
          │   allocs/op   │ allocs/op   vs base                 │
Printf-10      2.000 ± 0%   0.000 ± 0%  -100.00% (p=0.000 n=30)

Context

I was wondering why two error messages (from different goroutines) collided this way in a job log:

Screenshot 2024-05-28 at 5 09 09 PM

and tracked it to the separate fmt.Fprintln call.

Changes

  • Change how format strings are handled in the shell loggers
  • Add benchmarks

Testing

  • Tests have run locally (with go test ./...). Buildkite employees may check this if the pipeline has run automatically.
  • Code is formatted (with go fmt ./...)

@DrJosh9000 DrJosh9000 requested a review from a team May 28, 2024 07:11
@DrJosh9000 DrJosh9000 force-pushed the newline-in-one-call branch 2 times, most recently from 2ba9f38 to f3b8b9e Compare May 28, 2024 07:20
@DrJosh9000 DrJosh9000 enabled auto-merge May 28, 2024 07:22
@DrJosh9000 DrJosh9000 merged commit 3880115 into main Jun 5, 2024
1 check passed
@DrJosh9000 DrJosh9000 deleted the newline-in-one-call branch June 5, 2024 06:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants