-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extract cachestats to record cache stats (#924)
* extracting cachestats to record cache stats and apply to muxdb and repository * add tests for cachestats
- Loading branch information
Showing
8 changed files
with
185 additions
and
87 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
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,35 @@ | ||
// Copyright (c) 2024 The VeChainThor developers | ||
|
||
// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying | ||
// file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html> | ||
package thor | ||
|
||
import "sync/atomic" | ||
|
||
// CacheStats is a utility for collecting cache hit/miss. | ||
type CacheStats struct { | ||
hit, miss atomic.Int64 | ||
flag atomic.Int32 | ||
} | ||
|
||
// Hit records a hit. | ||
func (cs *CacheStats) Hit() int64 { return cs.hit.Add(1) } | ||
|
||
// Miss records a miss. | ||
func (cs *CacheStats) Miss() int64 { return cs.miss.Add(1) } | ||
|
||
// Stats returns the number of hits and misses and whether | ||
// the hit rate was changed comparing to the last call. | ||
func (cs *CacheStats) Stats() (bool, int64, int64) { | ||
hit := cs.hit.Load() | ||
miss := cs.miss.Load() | ||
lookups := hit + miss | ||
|
||
hitRate := float64(0) | ||
if lookups > 0 { | ||
hitRate = float64(hit) / float64(lookups) | ||
} | ||
flag := int32(hitRate * 1000) | ||
|
||
return cs.flag.Swap(flag) != flag, hit, miss | ||
} |
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,35 @@ | ||
// Copyright (c) 2024 The VeChainThor developers | ||
|
||
// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying | ||
// file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html> | ||
|
||
package thor | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCacheStats(t *testing.T) { | ||
cs := &CacheStats{} | ||
cs.Hit() | ||
cs.Miss() | ||
_, hit, miss := cs.Stats() | ||
|
||
assert.Equal(t, int64(1), hit) | ||
assert.Equal(t, int64(1), miss) | ||
|
||
changed, _, _ := cs.Stats() | ||
assert.False(t, changed) | ||
|
||
cs.Hit() | ||
cs.Miss() | ||
assert.Equal(t, int64(3), cs.Hit()) | ||
|
||
changed, hit, miss = cs.Stats() | ||
|
||
assert.Equal(t, int64(3), hit) | ||
assert.Equal(t, int64(2), miss) | ||
assert.True(t, changed) | ||
} |