Skip to content

Commit

Permalink
Embed version info; print benchmark config (microsoft#1874)
Browse files Browse the repository at this point in the history
Update how we set/read the version and commit information, so that it
can be set via writing to files instead of needing to update all `go
build` commands to add (or update) `-ldflags` with
`-X main.version=... -X manin.gitCommit=...".

Augment benchmark configuration with additional information, such as the
go version, number of CPUs available, start time, version, and git
branch and commit.

This allows standardizing the configuration data across different
benchmarking suites.

Benchmark config follow the benchmark raw data format:
https://go.googlesource.com/proposal/+/master/design/14313-benchmark-format.md

Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>
  • Loading branch information
helsaawy authored Oct 23, 2023
1 parent d71ebae commit c77cd65
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
4 changes: 4 additions & 0 deletions version/data/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# DO NOT MOVE: this file prevents `//go:embed data/*` from failing at build time
BRANCH
COMMIT
VERSION
41 changes: 41 additions & 0 deletions version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package version

import (
"embed"
"strings"
)

// Using `//go:embed data/VERSION` (similarly for `data/COMMIT` and `data/BRANCH`) will
// fail at build time if the files don't exist, which will break existing build workflows.
//
// Alternatively, committing those files in git will cause problems as they will be constantly
// updated and overwitten if devs update them locally and commit the changes.
//
// Therefore, we embed a (non-empty) directory and look up the files at run-time so builds
// succeed regardless of whether the files are exist or not.
// `data/.gitignore` is our fallback file, which keeps `data/` non-empty and prevents [embed]
// from failing.

// Using a dedicated `data` directory allows us to separate out what files to embed.
// (Writing `//go:embed *` would include everything in this directory, including this file.)

// See scripts/Set-VersionInfo.ps1 for an example of setting these values via files in data/.

//go:embed data/*
var data embed.FS

var (
// Branch is the git branch the binary was built from.
Branch = readDataFile("BRANCH")

// Commit is the git commit the binary was built from.
Commit = readDataFile("COMMIT")

// Version is the complete semver.
Version = readDataFile("VERSION")
)

func readDataFile(f string) string {
b, _ := data.ReadFile("data/" + f)
return strings.TrimSpace(string(b))
}

0 comments on commit c77cd65

Please sign in to comment.