-
Notifications
You must be signed in to change notification settings - Fork 258
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Embed version info; print benchmark config
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
Showing
14 changed files
with
304 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Example of who to set the version/git information in ./internal/version so | ||
# that the binaries will report the appropriate information. | ||
|
||
$ErrorActionPreference = 'Continue' | ||
$VerbosePreference = 'Continue' | ||
|
||
$root = Split-Path -Path $PSScriptRoot -Parent | ||
$dataDir = Join-Path $root 'internal/version/data' | ||
|
||
function run([string]$cmd, [string[]]$params) { | ||
Write-Verbose "$cmd $params" | ||
$s = & $cmd @params 2>&1 | ||
if ( $LASTEXITCODE -ne 0 ) { | ||
$err = "Command '$cmd $params' failed" | ||
if ( -not [string]::IsNullOrWhiteSpace("$s") ) { | ||
$err += ": $s" | ||
} | ||
Write-Warning $err | ||
} else { | ||
$s | ||
} | ||
} | ||
|
||
function writeto([string]$f, [string]$v) { | ||
if ( [string]::IsNullOrWhiteSpace($v) ) { | ||
return | ||
} | ||
$v = $v.Trim() | ||
|
||
Write-Output "${f}: $v" | ||
$v | Out-File -Encoding ascii -FilePath (Join-Path $dataDir $f) -NoNewline | ||
} | ||
|
||
$branch = run 'git' @('rev-parse', '--abbrev-ref', 'HEAD') | ||
if ( $branch -ceq 'HEAD') { | ||
Write-Warning 'Detected detached head' | ||
$branch = run 'git' @('name-rev', '--name-only', 'HEAD') | ||
if ( [string]::IsNullOrWhiteSpace($branch) -or $branch -eq 'undefined' ) { | ||
Write-Warning "'git name-rev' failed: $branch" | ||
$branch = run 'git' @('branch', '-r', '--contains=HEAD') | Select-Object -First 1 | ||
} | ||
} | ||
$branch = $branch -replace '^(origin|remotes/origin|refs/heads)/', '' | ||
writeto 'BRANCH' $branch | ||
|
||
$commit = run 'git' @('rev-parse', 'HEAD') | ||
try { | ||
run 'git' @('diff', '--no-ext-diff', '--quiet', '--exit-code', 'HEAD') | ||
if ( $LASTEXITCODE -ne 0 ) { | ||
Write-Warning 'Dirty repo' | ||
$commit += '-dirty' | ||
} | ||
} catch { } | ||
writeto 'COMMIT' $commit | ||
|
||
# don't use `--always`; we we're okay with an empty version since commit will likely be non-empty | ||
$version = run 'git' @('describe', '--match=v[0-9]*', '--tags', '--dirty') | ||
writeto 'VERSION' $version |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.