seaweedfs/weed/storage/needle/compact_map_perf_test.go

87 lines
1.8 KiB
Go
Raw Normal View History

2017-05-27 13:51:25 +08:00
package needle
import (
"testing"
2018-12-09 13:45:14 +08:00
"os"
"log"
2018-07-22 08:39:10 +08:00
"github.com/chrislusf/seaweedfs/weed/util"
2018-12-09 13:45:14 +08:00
. "github.com/chrislusf/seaweedfs/weed/storage/types"
)
2018-12-09 13:45:14 +08:00
/*
To see the memory usage:
go test -run TestMemoryUsage -memprofile=mem.out
go tool pprof needle.test mem.out
*/
func TestMemoryUsage(t *testing.T) {
2017-05-27 13:51:25 +08:00
indexFile, ie := os.OpenFile("../../../test/sample.idx", os.O_RDWR|os.O_RDONLY, 0644)
2013-01-17 16:56:56 +08:00
if ie != nil {
log.Fatalln(ie)
}
2017-05-27 13:51:25 +08:00
loadNewNeedleMap(indexFile)
2013-01-17 16:56:56 +08:00
2018-12-09 13:45:14 +08:00
indexFile.Close()
}
2017-05-27 13:51:25 +08:00
func loadNewNeedleMap(file *os.File) {
2013-01-17 16:56:56 +08:00
m := NewCompactMap()
2018-12-09 13:45:14 +08:00
bytes := make([]byte, NeedleEntrySize*1024)
2013-01-17 16:56:56 +08:00
count, e := file.Read(bytes)
for count > 0 && e == nil {
2018-12-09 13:45:14 +08:00
for i := 0; i < count; i += NeedleEntrySize {
2018-07-22 08:39:10 +08:00
key := BytesToNeedleId(bytes[i : i+NeedleIdSize])
offset := BytesToOffset(bytes[i+NeedleIdSize : i+NeedleIdSize+OffsetSize])
size := util.BytesToUint32(bytes[i+NeedleIdSize+OffsetSize : i+NeedleIdSize+OffsetSize+SizeSize])
2018-07-08 17:39:04 +08:00
2013-01-17 16:56:56 +08:00
if offset > 0 {
m.Set(NeedleId(key), offset, size)
2013-01-17 16:56:56 +08:00
} else {
//delete(m, key)
}
}
2013-01-17 16:56:56 +08:00
count, e = file.Read(bytes)
}
2018-12-09 13:45:14 +08:00
m.report()
}
// report memory usage
func (cm *CompactMap) report() {
overFlowCount := 0;
overwrittenByOverflow := 0;
entryCount := 0
compactSectionCount := 0
compactSectionEntryCount := 0
for _, cs := range cm.list {
compactSectionCount++
cs.RLock()
for range cs.overflow {
overFlowCount++
entryCount++
}
for _, v := range cs.values {
compactSectionEntryCount++
if _, found := cs.overflow[v.Key]; !found {
entryCount++
} else {
overwrittenByOverflow++
}
}
cs.RUnlock()
}
println("overFlowCount", overFlowCount)
println("overwrittenByOverflow", overwrittenByOverflow)
println("entryCount", entryCount)
println("compactSectionCount", compactSectionCount)
println("compactSectionEntryCount", compactSectionEntryCount)
}