-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
16d93ed
commit a13a914
Showing
7 changed files
with
422 additions
and
10 deletions.
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
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,110 @@ | ||
package stats | ||
|
||
type ( | ||
// Handler defines the interface that the Transport uses to collect cache metrics. | ||
// Note that implementations of this interface must be thread-safe; the methods of a Handler | ||
// can be called from concurrent goroutines. | ||
Handler interface { | ||
IncrHit() | ||
IncrMiss() | ||
IncrLocalHit() | ||
IncrLocalMiss() | ||
IncrRemoteHit() | ||
IncrRemoteMiss() | ||
IncrQuery() | ||
IncrQueryFail(err error) | ||
} | ||
|
||
Handlers struct { | ||
disable bool | ||
handlers []Handler | ||
} | ||
) | ||
|
||
// NewHandles creates a new instance of Handlers. | ||
func NewHandles(disable bool, handlers ...Handler) Handler { | ||
return &Handlers{ | ||
disable: disable, | ||
handlers: handlers, | ||
} | ||
} | ||
|
||
func (hs *Handlers) IncrHit() { | ||
if hs.disable { | ||
return | ||
} | ||
|
||
for _, h := range hs.handlers { | ||
h.IncrHit() | ||
} | ||
} | ||
|
||
func (hs *Handlers) IncrMiss() { | ||
if hs.disable { | ||
return | ||
} | ||
|
||
for _, h := range hs.handlers { | ||
h.IncrMiss() | ||
} | ||
} | ||
|
||
func (hs *Handlers) IncrLocalHit() { | ||
if hs.disable { | ||
return | ||
} | ||
|
||
for _, h := range hs.handlers { | ||
h.IncrLocalHit() | ||
} | ||
} | ||
|
||
func (hs *Handlers) IncrLocalMiss() { | ||
if hs.disable { | ||
return | ||
} | ||
|
||
for _, h := range hs.handlers { | ||
h.IncrLocalMiss() | ||
} | ||
} | ||
|
||
func (hs *Handlers) IncrRemoteHit() { | ||
if hs.disable { | ||
return | ||
} | ||
|
||
for _, h := range hs.handlers { | ||
h.IncrRemoteHit() | ||
} | ||
} | ||
|
||
func (hs *Handlers) IncrRemoteMiss() { | ||
if hs.disable { | ||
return | ||
} | ||
|
||
for _, h := range hs.handlers { | ||
h.IncrRemoteMiss() | ||
} | ||
} | ||
|
||
func (hs *Handlers) IncrQuery() { | ||
if hs.disable { | ||
return | ||
} | ||
|
||
for _, h := range hs.handlers { | ||
h.IncrQuery() | ||
} | ||
} | ||
|
||
func (hs *Handlers) IncrQueryFail(err error) { | ||
if hs.disable { | ||
return | ||
} | ||
|
||
for _, h := range hs.handlers { | ||
h.IncrQueryFail(err) | ||
} | ||
} |
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,89 @@ | ||
package stats | ||
|
||
import ( | ||
"errors" | ||
"sync/atomic" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type testHandler struct { | ||
Hit uint64 | ||
Miss uint64 | ||
LocalHit uint64 | ||
LocalMiss uint64 | ||
RemoteHit uint64 | ||
RemoteMiss uint64 | ||
Query uint64 | ||
QueryFail uint64 | ||
} | ||
|
||
func TestNewHandles(t *testing.T) { | ||
tests := []struct { | ||
input bool | ||
expect uint64 | ||
}{ | ||
{ | ||
input: false, | ||
expect: 1, | ||
}, | ||
{ | ||
input: true, | ||
expect: 0, | ||
}, | ||
} | ||
for _, v := range tests { | ||
var handler testHandler | ||
h := NewHandles(v.input, &handler) | ||
h.IncrHit() | ||
h.IncrMiss() | ||
h.IncrLocalHit() | ||
h.IncrLocalMiss() | ||
h.IncrRemoteHit() | ||
h.IncrRemoteMiss() | ||
h.IncrQuery() | ||
h.IncrQueryFail(errors.New("any")) | ||
|
||
assert.Equal(t, v.expect, handler.Hit) | ||
assert.Equal(t, v.expect, handler.Miss) | ||
assert.Equal(t, v.expect, handler.LocalHit) | ||
assert.Equal(t, v.expect, handler.LocalMiss) | ||
assert.Equal(t, v.expect, handler.RemoteHit) | ||
assert.Equal(t, v.expect, handler.RemoteMiss) | ||
assert.Equal(t, v.expect, handler.Query) | ||
assert.Equal(t, v.expect, handler.QueryFail) | ||
} | ||
} | ||
|
||
func (h *testHandler) IncrHit() { | ||
atomic.AddUint64(&h.Hit, 1) | ||
} | ||
|
||
func (h *testHandler) IncrMiss() { | ||
atomic.AddUint64(&h.Miss, 1) | ||
} | ||
|
||
func (h *testHandler) IncrLocalHit() { | ||
atomic.AddUint64(&h.LocalHit, 1) | ||
} | ||
|
||
func (h *testHandler) IncrLocalMiss() { | ||
atomic.AddUint64(&h.LocalMiss, 1) | ||
} | ||
|
||
func (h *testHandler) IncrRemoteHit() { | ||
atomic.AddUint64(&h.RemoteHit, 1) | ||
} | ||
|
||
func (h *testHandler) IncrRemoteMiss() { | ||
atomic.AddUint64(&h.RemoteMiss, 1) | ||
} | ||
|
||
func (h *testHandler) IncrQuery() { | ||
atomic.AddUint64(&h.Query, 1) | ||
} | ||
|
||
func (h *testHandler) IncrQueryFail(err error) { | ||
atomic.AddUint64(&h.QueryFail, 1) | ||
} |
Oops, something went wrong.