-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add stats API * update Readme
- Loading branch information
Showing
8 changed files
with
139 additions
and
46 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,29 @@ | ||
package internal | ||
|
||
type Stats struct { | ||
hits uint64 | ||
misses uint64 | ||
} | ||
|
||
func newStats(hits uint64, misses uint64) Stats { | ||
return Stats{ | ||
hits: hits, | ||
misses: misses, | ||
} | ||
} | ||
|
||
func (s Stats) Hits() uint64 { | ||
return s.hits | ||
} | ||
|
||
func (s Stats) Misses() uint64 { | ||
return s.misses | ||
} | ||
|
||
func (s Stats) HitRatio() float64 { | ||
total := s.hits + s.misses | ||
if total == 0 { | ||
return 0.0 | ||
} | ||
return float64(s.hits) / float64(total) | ||
} |
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,44 @@ | ||
package theine_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/Yiling-J/theine-go" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestStats(t *testing.T) { | ||
client, err := theine.NewBuilder[int, int](1000).Build() | ||
require.Nil(t, err) | ||
st := client.Stats() | ||
require.Equal(t, uint64(0), st.Hits()) | ||
require.Equal(t, uint64(0), st.Misses()) | ||
require.Equal(t, float64(0), st.HitRatio()) | ||
|
||
client.Set(1, 1, 1) | ||
for i := 0; i < 2000; i++ { | ||
_, ok := client.Get(1) | ||
require.True(t, ok) | ||
} | ||
|
||
st = client.Stats() | ||
require.Equal(t, uint64(2000), st.Hits()) | ||
require.Equal(t, uint64(0), st.Misses()) | ||
require.Equal(t, float64(1), st.HitRatio()) | ||
|
||
for i := 0; i < 10000; i++ { | ||
_, ok := client.Get(1) | ||
require.True(t, ok) | ||
} | ||
|
||
for i := 0; i < 10000; i++ { | ||
_, ok := client.Get(2) | ||
require.False(t, ok) | ||
} | ||
|
||
st = client.Stats() | ||
require.Equal(t, uint64(12000), st.Hits()) | ||
require.Equal(t, uint64(10000), st.Misses()) | ||
require.Equal(t, float64(12000)/float64(12000+10000), st.HitRatio()) | ||
|
||
} |