forked from rcrowley/go-metrics
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial implementation of GaugeFloat64
- Loading branch information
1 parent
ea09f7d
commit 68b1307
Showing
14 changed files
with
177 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package metrics | ||
|
||
import "sync" | ||
|
||
// GaugeFloat64s hold a float64 value that can be set arbitrarily. | ||
type GaugeFloat64 interface { | ||
Snapshot() GaugeFloat64 | ||
Update(float64) | ||
Value() float64 | ||
} | ||
|
||
// GetOrRegisterGaugeFloat64 returns an existing GaugeFloat64 or constructs and registers a | ||
// new StandardGaugeFloat64. | ||
func GetOrRegisterGaugeFloat64(name string, r Registry) GaugeFloat64 { | ||
if nil == r { | ||
r = DefaultRegistry | ||
} | ||
return r.GetOrRegister(name, NewGaugeFloat64()).(GaugeFloat64) | ||
} | ||
|
||
// NewGaugeFloat64 constructs a new StandardGaugeFloat64. | ||
func NewGaugeFloat64() GaugeFloat64 { | ||
if UseNilMetrics { | ||
return NilGaugeFloat64{} | ||
} | ||
return &StandardGaugeFloat64{ | ||
value: 0.0, | ||
} | ||
} | ||
|
||
// NewRegisteredGaugeFloat64 constructs and registers a new StandardGaugeFloat64. | ||
func NewRegisteredGaugeFloat64(name string, r Registry) GaugeFloat64 { | ||
c := NewGaugeFloat64() | ||
if nil == r { | ||
r = DefaultRegistry | ||
} | ||
r.Register(name, c) | ||
return c | ||
} | ||
|
||
// GaugeSnapshotFloat64 is a read-only copy of another GaugeFloat64. | ||
type GaugeSnapshotFloat64 float64 | ||
|
||
// Snapshot returns the snapshot. | ||
func (g GaugeSnapshotFloat64) Snapshot() GaugeFloat64 { return g } | ||
|
||
// Update panics. | ||
func (GaugeSnapshotFloat64) Update(float64) { | ||
panic("Update called on a GaugeSnapshotFloat64") | ||
} | ||
|
||
// Value returns the value at the time the snapshot was taken. | ||
func (g GaugeSnapshotFloat64) Value() float64 { return float64(g) } | ||
|
||
// NilGauge is a no-op Gauge. | ||
type NilGaugeFloat64 struct{} | ||
|
||
// Snapshot is a no-op. | ||
func (NilGaugeFloat64) Snapshot() GaugeFloat64 { return NilGaugeFloat64{} } | ||
|
||
// Update is a no-op. | ||
func (NilGaugeFloat64) Update(v float64) {} | ||
|
||
// Value is a no-op. | ||
func (NilGaugeFloat64) Value() float64 { return 0.0 } | ||
|
||
// StandardGaugeFloat64 is the standard implementation of a GaugeFloat64 and uses | ||
// sync.Mutex to manage a single float64 value. | ||
type StandardGaugeFloat64 struct { | ||
value float64 | ||
sync.Mutex | ||
} | ||
|
||
// Snapshot returns a read-only copy of the gauge. | ||
func (g *StandardGaugeFloat64) Snapshot() GaugeFloat64 { | ||
return GaugeSnapshotFloat64(g.Value()) | ||
} | ||
|
||
// Update updates the gauge's value. | ||
func (g *StandardGaugeFloat64) Update(v float64) { | ||
g.Lock() | ||
defer g.Unlock() | ||
g.value = v | ||
} | ||
|
||
// Value returns the gauge's current value. | ||
func (g *StandardGaugeFloat64) Value() float64 { | ||
g.Lock() | ||
defer g.Unlock() | ||
return g.value | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package metrics | ||
|
||
import "testing" | ||
|
||
|
||
func BenchmarkGuageFloat64(b *testing.B) { | ||
g := NewGaugeFloat64() | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
g.Update(float64(i)) | ||
} | ||
} | ||
|
||
func TestGaugeFloat64(t *testing.T) { | ||
g := NewGaugeFloat64() | ||
g.Update(float64(47.0)) | ||
if v := g.Value(); float64(47.0) != v { | ||
t.Errorf("g.Value(): 47.0 != %v\n", v) | ||
} | ||
} | ||
|
||
func TestGaugeFloat64Snapshot(t *testing.T) { | ||
g := NewGaugeFloat64() | ||
g.Update(float64(47.0)) | ||
snapshot := g.Snapshot() | ||
g.Update(float64(0)) | ||
if v := snapshot.Value(); float64(47.0) != v { | ||
t.Errorf("g.Value(): 47.0 != %v\n", v) | ||
} | ||
} | ||
|
||
func TestGetOrRegisterGaugeFloat64(t *testing.T) { | ||
r := NewRegistry() | ||
NewRegisteredGaugeFloat64("foo", r).Update(float64(47.0)) | ||
t.Logf("registry: %v", r) | ||
if g := GetOrRegisterGaugeFloat64("foo", r); float64(47.0) != g.Value() { | ||
t.Fatal(g) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters