Skip to content

Commit

Permalink
cmd/bench: wait for load average to drop before starting
Browse files Browse the repository at this point in the history
The coordinator runs tests on a freshly booted VM which may still be
running background boot tasks when bench starts.

For minimal noise, wait for the system load average to drop (indicating
background tasks have completed) before continuing with benchmarking.

For golang/go#49207.

Change-Id: I8df01592fea31d49eae54074213e202b21d5728a
Reviewed-on: https://go-review.googlesource.com/c/benchmarks/+/362375
Reviewed-by: Michael Knyszek <[email protected]>
Trust: Michael Pratt <[email protected]>
Run-TryBot: Michael Pratt <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
  • Loading branch information
prattmic committed Jan 10, 2022
1 parent e013d12 commit 4bd3f69
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
64 changes: 64 additions & 0 deletions cmd/bench/idle_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
"fmt"
"log"
"os"
"strconv"
"strings"
"time"
)

const idleMaxLoad = 0.2

// loadAvg returns the 1-minute load average.
func loadAvg() (float64, error) {
b, err := os.ReadFile("/proc/loadavg")
if err != nil {
return 0, fmt.Errorf("error reading /proc/loadavg: %w", err)
}

s := strings.TrimSpace(string(b))
log.Printf("Load average: %s", s)

parts := strings.Split(s, " ")

avg, err := strconv.ParseFloat(parts[0], 64)
if err != nil {
return 0, fmt.Errorf("malformed load average %q: %v", parts[0], err)
}
return avg, nil
}

func waitForIdle() error {
avg, err := loadAvg()
if err != nil {
return fmt.Errorf("error reading load average: %w", err)
}
if avg < idleMaxLoad {
return nil
}

log.Printf("Waiting for load average to drop below %.2f...", idleMaxLoad)

tick := time.NewTicker(30 * time.Second)
defer tick.Stop()

for _ = range tick.C {
avg, err := loadAvg()
if err != nil {
return fmt.Errorf("error reading load average: %w", err)
}
if avg < idleMaxLoad {
break
}

log.Printf("Waiting for load average to drop below %.2f...", idleMaxLoad)
}

return nil
}
13 changes: 13 additions & 0 deletions cmd/bench/idle_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build !linux
// +build !linux

package main

// waitForIdle is only implemented for Linux.
func waitForIdle() error {
return nil
}
13 changes: 13 additions & 0 deletions cmd/bench/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
package main

import (
"flag"
"fmt"
"log"
"os"
Expand All @@ -18,6 +19,8 @@ import (
"strings"
)

var wait = flag.Bool("wait", true, "wait for system idle before starting benchmarking")

func determineGOROOT() (string, error) {
g, ok := os.LookupEnv("GOROOT")
if ok {
Expand Down Expand Up @@ -60,6 +63,16 @@ func run(goroot string) error {
}

func main() {
flag.Parse()

if *wait {
// We may be on a freshly booted VM. Wait for boot tasks to
// complete before continuing.
if err := waitForIdle(); err != nil {
log.Fatalf("Failed to wait for idle: %v", err)
}
}

goroot, err := determineGOROOT()
if err != nil {
log.Fatalf("Unable to determine GOROOT: %v", err)
Expand Down

0 comments on commit 4bd3f69

Please sign in to comment.