loadtest.go: add IP into seed (#2230)

When running loadtest on a number of machines all at once, machines that
start at the same time tend to wind up with the same random seed, leading
to the same sequence of operations.

Mixing in the IP address perturbs the seed enough to change things up.
This commit is contained in:
cmasone-attic
2016-08-01 15:19:31 -07:00
committed by GitHub
parent af6f702e78
commit 0b102403b3
+22 -3
View File
@@ -6,10 +6,12 @@ package main
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"io/ioutil"
"math/rand"
"net"
"os"
"os/exec"
"strings"
@@ -31,7 +33,7 @@ type runner struct {
}
func main() {
rand.Seed(time.Now().Unix())
rand.Seed(time.Now().UnixNano() + bestEffortGetIP())
if len(os.Args) != 2 {
fmt.Println("Usage: loadtest <database>")
@@ -57,6 +59,23 @@ func main() {
}
}
func bestEffortGetIP() (asNum int64) {
addrs, err := net.InterfaceAddrs()
if err != nil {
return
}
for _, a := range addrs {
if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
asNum = int64(binary.BigEndian.Uint32([]byte(ipnet.IP.To4())))
break
}
}
}
return
}
func runDiff(db, ds string) {
if parent := getParent(db, ds); parent != "" {
call(nil, "noms", "diff", ds, parent)
@@ -70,7 +89,7 @@ func runLogDiff(db, ds string) {
}
func runLogShow(db, ds string) {
call(nil, "noms", "log", "-show-value", ds)
call(nil, "noms", "log", "--show-value", ds)
}
func runShow(db, ds string) {
@@ -100,7 +119,7 @@ func runSync(db, ds string) {
func getParent(db, ds string) string {
buf := &bytes.Buffer{}
call(buf, "noms", "log", "-n", "2", "-oneline", ds)
call(buf, "noms", "log", "-n", "2", "--oneline", ds)
// Output will look like:
// abc (Parent def)
// def (Parent None)