From 1392d0bc148dbd62cac1df49a677325a9605328b Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Tue, 20 Oct 2015 08:27:22 +0200 Subject: [PATCH] Print the single thread hash performance at startup --- cmd/syncthing/main.go | 17 +++++++++++++++++ cmd/syncthing/usage_report.go | 24 +++++++++++++----------- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/cmd/syncthing/main.go b/cmd/syncthing/main.go index 68b610039..5ffe5409f 100644 --- a/cmd/syncthing/main.go +++ b/cmd/syncthing/main.go @@ -516,6 +516,7 @@ func syncthingMain() { l.Infoln(LongVersion) l.Infoln("My ID:", myID) + printHashRate() // Emit the Starting event, now that we know who we are. @@ -836,6 +837,22 @@ func syncthingMain() { os.Exit(code) } +// printHashRate prints the hashing performance in MB/s, formatting it with +// appropriate precision for the value, i.e. 182 MB/s, 18 MB/s, 1.8 MB/s, 0.18 +// MB/s. +func printHashRate() { + hashRate := cpuBench(3, 100*time.Millisecond) + + decimals := 0 + if hashRate < 1 { + decimals = 2 + } else if hashRate < 10 { + decimals = 1 + } + + l.Infof("Single thread hash performance is ~%.*f MB/s", decimals, hashRate) +} + func loadConfig(cfgFile string) (*config.Wrapper, string, error) { info, err := os.Stat(cfgFile) if err != nil { diff --git a/cmd/syncthing/usage_report.go b/cmd/syncthing/usage_report.go index 06a8a2bd7..7b1f8304d 100644 --- a/cmd/syncthing/usage_report.go +++ b/cmd/syncthing/usage_report.go @@ -112,15 +112,7 @@ func reportData(cfg *config.Wrapper, m *model.Model) map[string]interface{} { var mem runtime.MemStats runtime.ReadMemStats(&mem) res["memoryUsageMiB"] = (mem.Sys - mem.HeapReleased) / 1024 / 1024 - - var perf float64 - for i := 0; i < 5; i++ { - p := cpuBench() - if p > perf { - perf = p - } - } - res["sha256Perf"] = perf + res["sha256Perf"] = cpuBench(5, 125*time.Millisecond) bytes, err := memorySize() if err == nil { @@ -301,7 +293,17 @@ func (s *usageReportingService) Stop() { } // cpuBench returns CPU performance as a measure of single threaded SHA-256 MiB/s -func cpuBench() float64 { +func cpuBench(iterations int, duration time.Duration) float64 { + var perf float64 + for i := 0; i < iterations; i++ { + if v := cpuBenchOnce(duration); v > perf { + perf = v + } + } + return perf +} + +func cpuBenchOnce(duration time.Duration) float64 { chunkSize := 100 * 1 << 10 h := sha256.New() bs := make([]byte, chunkSize) @@ -309,7 +311,7 @@ func cpuBench() float64 { t0 := time.Now() b := 0 - for time.Since(t0) < 125*time.Millisecond { + for time.Since(t0) < duration { h.Write(bs) b += chunkSize }