-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/bench: wait for load average to drop before starting
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
Showing
3 changed files
with
90 additions
and
0 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
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 | ||
} |
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,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 | ||
} |
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