expvar: swap Float sync. from mutex to atomic.
Float type from a mutex to atomic bit array in a manner akin to
Google Guava's AtomicDouble[0], including adding a benchmark for the
type (benchcmp included below) along with some expvar_test.go cruft
being fixed.
benchmark old ns/op new ns/op delta
BenchmarkFloatSet 115 9.37 -91.85%
BenchmarkFloatAdd 114 17.1 -85.00%
benchmark old allocs new allocs delta
BenchmarkFloatSet 0 0 +0.00%
BenchmarkFloatAdd 0 0 +0.00%
benchmark old bytes new bytes delta
BenchmarkFloatSet 0 0 +0.00%
BenchmarkFloatAdd 0 0 +0.00%
[0] - http://goo.gl/m4dtlI
Change-Id: I4ce6a913734ec692e3ed243f6e6f7c11da4c6036
Reviewed-on: https://go-review.googlesource.com/3687
Reviewed-by: Rob Pike <r@golang.org>
diff --git a/src/expvar/expvar_test.go b/src/expvar/expvar_test.go
index 11e6497..8bc633e 100644
--- a/src/expvar/expvar_test.go
+++ b/src/expvar/expvar_test.go
@@ -7,11 +7,13 @@
import (
"bytes"
"encoding/json"
+ "math"
"net"
"net/http/httptest"
"runtime"
"strconv"
"sync"
+ "sync/atomic"
"testing"
)
@@ -70,6 +72,10 @@
})
}
+func (v *Float) val() float64 {
+ return math.Float64frombits(atomic.LoadUint64(&v.f))
+}
+
func TestFloat(t *testing.T) {
RemoveAll()
reqs := NewFloat("requests-float")
@@ -82,8 +88,8 @@
reqs.Add(1.5)
reqs.Add(1.25)
- if reqs.f != 2.75 {
- t.Errorf("reqs.f = %v, want 2.75", reqs.f)
+ if v := reqs.val(); v != 2.75 {
+ t.Errorf("reqs.val() = %v, want 2.75", v)
}
if s := reqs.String(); s != "2.75" {
@@ -91,8 +97,8 @@
}
reqs.Add(-2)
- if reqs.f != 0.75 {
- t.Errorf("reqs.f = %v, want 0.75", reqs.f)
+ if v := reqs.val(); v != 0.75 {
+ t.Errorf("reqs.val() = %v, want 0.75", v)
}
}
@@ -157,8 +163,8 @@
if x := colors.m["blue"].(*Int).i; x != 4 {
t.Errorf("colors.m[\"blue\"] = %v, want 4", x)
}
- if x := colors.m[`green "midori"`].(*Float).f; x != 4.125 {
- t.Errorf("colors.m[`green \"midori\"] = %v, want 3.14", x)
+ if x := colors.m[`green "midori"`].(*Float).val(); x != 4.125 {
+ t.Errorf("colors.m[`green \"midori\"] = %v, want 4.125", x)
}
// colors.String() should be '{"red":3, "blue":4}',