Skip to content

Commit

Permalink
chore(stats): move cache stats to cache package (#927)
Browse files Browse the repository at this point in the history
* chore(stats): move cache stats to cache package

* chore(stats): move cache stats to cache package
  • Loading branch information
darrenvechain authored Dec 26, 2024
1 parent ccbb1a6 commit 1557868
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 14 deletions.
12 changes: 6 additions & 6 deletions thor/cachestats.go → cache/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@

// 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
package cache

import "sync/atomic"

// CacheStats is a utility for collecting cache hit/miss.
type CacheStats struct {
// Stats is a utility for collecting cache hit/miss.
type Stats struct {
hit, miss atomic.Int64
flag atomic.Int32
}

// Hit records a hit.
func (cs *CacheStats) Hit() int64 { return cs.hit.Add(1) }
func (cs *Stats) Hit() int64 { return cs.hit.Add(1) }

// Miss records a miss.
func (cs *CacheStats) Miss() int64 { return cs.miss.Add(1) }
func (cs *Stats) 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) {
func (cs *Stats) Stats() (bool, int64, int64) {
hit := cs.hit.Load()
miss := cs.miss.Load()
lookups := hit + miss
Expand Down
4 changes: 2 additions & 2 deletions thor/cachestats_test.go → cache/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// 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
package cache

import (
"testing"
Expand All @@ -12,7 +12,7 @@ import (
)

func TestCacheStats(t *testing.T) {
cs := &CacheStats{}
cs := &Stats{}
cs.Hit()
cs.Miss()
_, hit, miss := cs.Stats()
Expand Down
7 changes: 4 additions & 3 deletions chain/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/pkg/errors"
"github.com/syndtr/goleveldb/leveldb/util"
"github.com/vechain/thor/v2/block"
cache2 "github.com/vechain/thor/v2/cache"
"github.com/vechain/thor/v2/co"
"github.com/vechain/thor/v2/kv"
"github.com/vechain/thor/v2/muxdb"
Expand Down Expand Up @@ -54,9 +55,9 @@ type Repository struct {
receipts *cache

stats struct {
summaries thor.CacheStats
txs thor.CacheStats
receipts thor.CacheStats
summaries cache2.Stats
txs cache2.Stats
receipts cache2.Stats
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions muxdb/internal/trie/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

lru "github.com/hashicorp/golang-lru"
"github.com/qianbin/directcache"
"github.com/vechain/thor/v2/thor"
"github.com/vechain/thor/v2/cache"
"github.com/vechain/thor/v2/trie"
)

Expand All @@ -25,8 +25,8 @@ type Cache struct {
committedNodes *directcache.Cache
// caches root nodes.
roots *lru.ARCCache
nodeStats thor.CacheStats
rootStats thor.CacheStats
nodeStats cache.Stats
rootStats cache.Stats
lastLogTime int64
}

Expand Down

0 comments on commit 1557868

Please sign in to comment.