Skip to content

Commit

Permalink
GO-5154 Devise metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
fat-fellow committed Feb 21, 2025
1 parent 8b236de commit 091a0c8
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
24 changes: 19 additions & 5 deletions nodestorage/spacestorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,25 @@ type ChangeSizeStats struct {
Total int `json:"total"`
}

type PerObjectSizeStats struct {
LenTotalMax int `json:"len"`
LenTotalP95 float64 `json:"lenTotalP95"`
LenTotalMedian float64 `json:"lenTotalMedian"`
SizeTotalMax int `json:"sizeTotal"`
SizeTotalP95 float64 `json:"SizeTotalP95"`
SizeTotalMedian float64 `json:"SizeTotalMedian"`
SizeMax int `json:"sizeMax"`
SizeP95 float64 `json:"sizeP95"`
SizeMedian float64 `json:"sizeMedian"`
}

type ObjectSpaceStats struct {
ObjectsCount int `json:"objectsCount,omitempty"`
DeletedObjectsCount int `json:"deletedObjectsCount"`
ChangesCount int `json:"changesCount"`
ChangeSize ChangeSizeStats `json:"changeSizeStats,omitempty"`
TreeStats []TreeStat `json:"treeStats,omitempty"`
ObjectsCount int `json:"objectsCount,omitempty"`
DeletedObjectsCount int `json:"deletedObjectsCount"`
ChangesCount int `json:"changesCount"`
ChangeSize ChangeSizeStats `json:"changeSizeStats,omitempty"`
PerObjectSize PerObjectSizeStats `json:"perObjectSizeStats,omitempty"`
TreeStats []TreeStat `json:"treeStats,omitempty"`
treeMap map[string]TreeStat
}

Expand All @@ -37,6 +50,7 @@ type TreeStat struct {
SnapshotsCount int `json:"snapshotsCount"`
MaxSnapshotCounter int `json:"maxSnapshotCounter"`
ChangesSumSize int `json:"payloadSize"`
ChangeMaxSize int `json:"changeMaxSize"`
}

type SpaceStats struct {
Expand Down
32 changes: 31 additions & 1 deletion nodestorage/stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"cmp"
"context"
"fmt"

anystore "github.com/anyproto/any-store"
"github.com/anyproto/any-store/query"
"github.com/anyproto/any-sync/commonspace/headsync/headstorage"
Expand Down Expand Up @@ -69,6 +68,9 @@ func (r *nodeStorage) GetSpaceStats(ctx context.Context, treeTop int) (spaceStat
lengths = append(lengths, chSize)
snapshotCounter := doc.Value().GetInt(objecttree.SnapshotCounterKey)
treeStat.ChangesSumSize += chSize
if treeStat.ChangeMaxSize > chSize {
treeStat.ChangeMaxSize = chSize
}
changesSize += chSize
if snapshotCounter > treeStat.MaxSnapshotCounter {
treeStat.MaxSnapshotCounter = snapshotCounter
Expand All @@ -94,6 +96,7 @@ func (r *nodeStorage) GetSpaceStats(ctx context.Context, treeTop int) (spaceStat
if len(lengths) > 0 {
spaceStats.ChangeSize.MaxLen = lengths[len(lengths)-1]
}
calculateStatsPerObject(&spaceStats)
spaceStats.ChangeSize.Total = changesSize

if treeTop > 0 {
Expand All @@ -115,6 +118,33 @@ func (r *nodeStorage) GetSpaceStats(ctx context.Context, treeTop int) (spaceStat
return
}

func calculateStatsPerObject(stats *ObjectSpaceStats) {
var changesCounts []int
var changesSumSizes []int
var changesMaxSizes []int

for _, stat := range stats.treeMap {
changesCounts = append(changesCounts, stat.ChangesCount)
changesSumSizes = append(changesSumSizes, stat.ChangesSumSize)
changesMaxSizes = append(changesMaxSizes, stat.ChangeMaxSize)
}

slices.Sort(changesCounts)
stats.PerObjectSize.LenTotalMax = changesCounts[len(changesCounts)-1]
stats.PerObjectSize.LenTotalP95 = calcP95(changesCounts)
stats.PerObjectSize.LenTotalMedian = calcMedian(changesCounts)

slices.Sort(changesSumSizes)
stats.PerObjectSize.SizeTotalMax = changesSumSizes[len(changesSumSizes)-1]
stats.PerObjectSize.SizeTotalP95 = calcP95(changesSumSizes)
stats.PerObjectSize.SizeTotalMedian = calcMedian(changesSumSizes)

slices.Sort(changesMaxSizes)
stats.PerObjectSize.SizeMax = changesSumSizes[len(changesMaxSizes)-1]
stats.PerObjectSize.SizeP95 = calcP95(changesMaxSizes)
stats.PerObjectSize.SizeMedian = calcMedian(changesMaxSizes)
}

func calcMedian(sortedLengths []int) (median float64) {
mid := len(sortedLengths) / 2
if len(sortedLengths)%2 == 0 {
Expand Down

0 comments on commit 091a0c8

Please sign in to comment.