lib/ur: Prevent trivial race in CPU bench

This commit is contained in:
Jakob Borg
2019-08-25 23:43:28 +02:00
parent c2ea9d119d
commit 0832285d79

View File

@@ -17,6 +17,7 @@ import (
"runtime" "runtime"
"sort" "sort"
"strings" "strings"
"sync"
"time" "time"
"github.com/syncthing/syncthing/lib/build" "github.com/syncthing/syncthing/lib/build"
@@ -425,8 +426,16 @@ func (*Service) String() string {
return "ur.Service" return "ur.Service"
} }
var (
blocksResult []protocol.BlockInfo // so the result is not optimized away
blocksResultMut sync.Mutex
)
// CpuBench returns CPU performance as a measure of single threaded SHA-256 MiB/s // CpuBench returns CPU performance as a measure of single threaded SHA-256 MiB/s
func CpuBench(iterations int, duration time.Duration, useWeakHash bool) float64 { func CpuBench(iterations int, duration time.Duration, useWeakHash bool) float64 {
blocksResultMut.Lock()
defer blocksResultMut.Unlock()
dataSize := 16 * protocol.MinBlockSize dataSize := 16 * protocol.MinBlockSize
bs := make([]byte, dataSize) bs := make([]byte, dataSize)
rand.Reader.Read(bs) rand.Reader.Read(bs)
@@ -441,8 +450,6 @@ func CpuBench(iterations int, duration time.Duration, useWeakHash bool) float64
return perf return perf
} }
var blocksResult []protocol.BlockInfo // so the result is not optimized away
func cpuBenchOnce(duration time.Duration, useWeakHash bool, bs []byte) float64 { func cpuBenchOnce(duration time.Duration, useWeakHash bool, bs []byte) float64 {
t0 := time.Now() t0 := time.Now()
b := 0 b := 0