Skip to content

Commit

Permalink
go/tools/bazel_benchmark: extract some logic into bash script (#1601)
Browse files Browse the repository at this point in the history
bazel_benchmark.sh is now responsible for cloning rules_go at master
into a temp directory. This script can be copied to a bin directory
and run with a timer. The rest of the bazel_benchmark.go logic will
run at the tip of master.

Also: record Bazel version in the output file.
  • Loading branch information
jayconrod authored Jul 24, 2018
1 parent a966c42 commit 0bf340e
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 40 deletions.
6 changes: 6 additions & 0 deletions go/tools/bazel_benchmark/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
load("@io_bazel_rules_go//go:def.bzl", "go_binary")

go_binary(
name = "bazel_benchmark",
srcs = ["bazel_benchmark.go"],
)
78 changes: 38 additions & 40 deletions go/tools/bazel_benchmark/bazel_benchmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,13 @@ func run(args []string) error {
if err := fs.Parse(args); err != nil {
return err
}
if rulesGoDir != "" {
var err error
rulesGoDir, err = filepath.Abs(rulesGoDir)
if err != nil {
return err
}
if rulesGoDir == "" {
return errors.New("-rules_go_dir not set")
}
if abs, err := filepath.Abs(rulesGoDir); err != nil {
return err
} else {
rulesGoDir = abs
}
if outPath == "" {
return errors.New("-out not set")
Expand All @@ -125,14 +126,6 @@ func run(args []string) error {
outPath = abs
}

if rulesGoDir == "" {
var err error
rulesGoDir, err = cloneRulesGo()
if err != nil {
return err
}
defer os.RemoveAll(rulesGoDir)
}
commit, err := getCommit(rulesGoDir)
if err != nil {
return err
Expand All @@ -142,11 +135,16 @@ func run(args []string) error {
if err != nil {
return err
}
log.Printf("running benchmarks in %s", dir)
if !keep {
defer cleanupWorkspace(dir)
}

bazelVersion, err := getBazelVersion()
if err != nil {
return err
}

log.Printf("running benchmarks in %s", dir)
targetSet := make(map[string]bool)
for _, b := range benchmarks {
for _, t := range b.targets {
Expand All @@ -168,25 +166,7 @@ func run(args []string) error {
}

log.Printf("writing results to %s", outPath)
return recordResults(outPath, time.Now().UTC(), commit, benchmarks)
}

func cloneRulesGo() (dir string, err error) {
dir, err = ioutil.TempDir("", "rules_go")
if err != nil {
return "", err
}
defer func() {
if err != nil {
os.RemoveAll(dir)
}
}()
cmd := exec.Command("git", "clone", "--depth=1", "--single-branch", "--no-tags", "--", "https://github.com/bazelbuild/rules_go", dir)
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return "", err
}
return dir, nil
return recordResults(outPath, time.Now().UTC(), bazelVersion, commit, benchmarks)
}

func getCommit(rulesGoDir string) (commit string, err error) {
Expand Down Expand Up @@ -266,8 +246,25 @@ func cleanupWorkspace(dir string) error {
if err := logBazelCommand("clean", "--expunge"); err != nil {
return err
}
fmt.Fprintf(os.Stderr, "skipping removal of dir %s\n", dir)
return nil
return os.RemoveAll(dir)
}

func getBazelVersion() (string, error) {
out, err := exec.Command("bazel", "version").Output()
if err != nil {
return "", err
}
prefix := []byte("Build label: ")
i := bytes.Index(out, prefix)
if i < 0 {
return "", errors.New("could not find bazel version in output")
}
out = out[i+len(prefix):]
i = bytes.IndexByte(out, '\n')
if i >= 0 {
out = out[:i]
}
return string(out), nil
}

func fetch(targets []string) error {
Expand Down Expand Up @@ -309,11 +306,11 @@ func runBenchmark(b *benchmark) error {
return nil
}

func recordResults(outPath string, t time.Time, commit string, benchmarks []benchmark) (err error) {
func recordResults(outPath string, t time.Time, bazelVersion, commit string, benchmarks []benchmark) (err error) {
// TODO(jayconrod): update the header if new columns are added.
columnMap, outExists, err := buildColumnMap(outPath, benchmarks)
header := buildHeader(columnMap)
record := buildRecord(t, commit, benchmarks, columnMap)
record := buildRecord(t, bazelVersion, commit, benchmarks, columnMap)
defer func() {
if err != nil {
log.Printf("error writing results: %s: %v", outPath, err)
Expand Down Expand Up @@ -370,7 +367,7 @@ func buildColumnMap(outPath string, benchmarks []benchmark) (columnMap map[strin
}

doneReading:
for _, s := range []string{"time", "commit"} {
for _, s := range []string{"time", "bazel_version", "commit"} {
if _, ok := columnMap[s]; !ok {
columnMap[s] = len(columnMap)
}
Expand All @@ -391,9 +388,10 @@ func buildHeader(columnMap map[string]int) []string {
return header
}

func buildRecord(t time.Time, commit string, benchmarks []benchmark, columnMap map[string]int) []string {
func buildRecord(t time.Time, bazelVersion, commit string, benchmarks []benchmark, columnMap map[string]int) []string {
record := make([]string, len(columnMap))
record[columnMap["time"]] = t.Format("2006-01-02 15:04:05")
record[columnMap["bazel_version"]] = bazelVersion
record[columnMap["commit"]] = commit
for _, b := range benchmarks {
record[columnMap[b.desc]] = fmt.Sprintf("%.3f", b.result.Seconds())
Expand Down
29 changes: 29 additions & 0 deletions go/tools/bazel_benchmark/bazel_benchmark.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash

# Copyright 2018 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -euo pipefail

rules_go_dir=$(mktemp --directory --tmpdir rules_go.XXXXXX)
function cleanup {
rm -rf "$rules_go_dir"
}
trap cleanup EXIT

git clone --depth=1 --single-branch --no-tags \
https://github.com/bazelbuild/rules_go "$rules_go_dir"
cd "$rules_go_dir"
bazel run //go/tools/bazel_benchmark -- -rules_go_dir "$rules_go_dir" "$@"

0 comments on commit 0bf340e

Please sign in to comment.