From e14741a58cbe0b4ebf71faa5119d4df6b7beb763 Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Mon, 6 Feb 2017 13:42:39 +0100 Subject: [PATCH] cmd/syncthing: Speed up the CPU benchmark Not the measured performance, just the wall clock time it takes to complete. The random generation was dominating. --- cmd/syncthing/usage_report.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/cmd/syncthing/usage_report.go b/cmd/syncthing/usage_report.go index 08d212c37..15f0f5fd3 100644 --- a/cmd/syncthing/usage_report.go +++ b/cmd/syncthing/usage_report.go @@ -288,9 +288,13 @@ func (s *usageReportingService) Stop() { // cpuBench returns CPU performance as a measure of single threaded SHA-256 MiB/s func cpuBench(iterations int, duration time.Duration, useWeakHash bool) float64 { + dataSize := 16 * protocol.BlockSize + bs := make([]byte, dataSize) + rand.Reader.Read(bs) + var perf float64 for i := 0; i < iterations; i++ { - if v := cpuBenchOnce(duration, useWeakHash); v > perf { + if v := cpuBenchOnce(duration, useWeakHash, bs); v > perf { perf = v } } @@ -300,17 +304,13 @@ func cpuBench(iterations int, duration time.Duration, useWeakHash bool) float64 var blocksResult []protocol.BlockInfo // so the result is not optimized away -func cpuBenchOnce(duration time.Duration, useWeakHash bool) float64 { - dataSize := 16 * protocol.BlockSize - bs := make([]byte, dataSize) - rand.Reader.Read(bs) - +func cpuBenchOnce(duration time.Duration, useWeakHash bool, bs []byte) float64 { t0 := time.Now() b := 0 for time.Since(t0) < duration { r := bytes.NewReader(bs) - blocksResult, _ = scanner.Blocks(r, protocol.BlockSize, int64(dataSize), nil, useWeakHash) - b += dataSize + blocksResult, _ = scanner.Blocks(r, protocol.BlockSize, int64(len(bs)), nil, useWeakHash) + b += len(bs) } d := time.Since(t0) return float64(int(float64(b)/d.Seconds()/(1<<20)*100)) / 100