package badger
import (
"fmt"
"math"
)
func (db *DB ) PrintHistogram (keyPrefix []byte ) {
if db == nil {
fmt .Println ("\nCannot build histogram: DB is nil." )
return
}
histogram := db .buildHistogram (keyPrefix )
fmt .Printf ("Histogram of key sizes (in bytes)\n" )
histogram .keySizeHistogram .printHistogram ()
fmt .Printf ("Histogram of value sizes (in bytes)\n" )
histogram .valueSizeHistogram .printHistogram ()
}
type histogramData struct {
bins []int64
countPerBin []int64
totalCount int64
min int64
max int64
sum int64
}
type sizeHistogram struct {
keySizeHistogram, valueSizeHistogram histogramData
}
func newSizeHistogram() *sizeHistogram {
keyBins := createHistogramBins (1 , 16 )
valueBins := createHistogramBins (1 , 30 )
return &sizeHistogram {
keySizeHistogram : histogramData {
bins : keyBins ,
countPerBin : make ([]int64 , len (keyBins )+1 ),
max : math .MinInt64 ,
min : math .MaxInt64 ,
sum : 0 ,
},
valueSizeHistogram : histogramData {
bins : valueBins ,
countPerBin : make ([]int64 , len (valueBins )+1 ),
max : math .MinInt64 ,
min : math .MaxInt64 ,
sum : 0 ,
},
}
}
func createHistogramBins(minExponent , maxExponent uint32 ) []int64 {
var bins []int64
for i := minExponent ; i <= maxExponent ; i ++ {
bins = append (bins , int64 (1 )<<i )
}
return bins
}
func (histogram *histogramData ) Update (value int64 ) {
if value > histogram .max {
histogram .max = value
}
if value < histogram .min {
histogram .min = value
}
histogram .sum += value
histogram .totalCount ++
for index := 0 ; index <= len (histogram .bins ); index ++ {
if index == len (histogram .bins ) {
histogram .countPerBin [index ]++
break
}
if value < histogram .bins [index ] {
histogram .countPerBin [index ]++
break
}
}
}
func (db *DB ) buildHistogram (keyPrefix []byte ) *sizeHistogram {
txn := db .NewTransaction (false )
defer txn .Discard ()
itr := txn .NewIterator (DefaultIteratorOptions )
defer itr .Close ()
badgerHistogram := newSizeHistogram ()
for itr .Seek (keyPrefix ); itr .ValidForPrefix (keyPrefix ); itr .Next () {
item := itr .Item ()
badgerHistogram .keySizeHistogram .Update (item .KeySize ())
badgerHistogram .valueSizeHistogram .Update (item .ValueSize ())
}
return badgerHistogram
}
func (histogram histogramData ) printHistogram () {
fmt .Printf ("Total count: %d\n" , histogram .totalCount )
fmt .Printf ("Min value: %d\n" , histogram .min )
fmt .Printf ("Max value: %d\n" , histogram .max )
fmt .Printf ("Mean: %.2f\n" , float64 (histogram .sum )/float64 (histogram .totalCount ))
fmt .Printf ("%24s %9s\n" , "Range" , "Count" )
numBins := len (histogram .bins )
for index , count := range histogram .countPerBin {
if count == 0 {
continue
}
if index == len (histogram .countPerBin )-1 {
lowerBound := int (histogram .bins [numBins -1 ])
fmt .Printf ("[%10d, %10s) %9d\n" , lowerBound , "infinity" , count )
continue
}
upperBound := int (histogram .bins [index ])
lowerBound := 0
if index > 0 {
lowerBound = int (histogram .bins [index -1 ])
}
fmt .Printf ("[%10d, %10d) %9d\n" , lowerBound , upperBound , count )
}
fmt .Println ()
}
The pages are generated with Golds v0.8.4 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .