seaweedfs/weed/storage/needle/compact_map_perf_test.go

94 lines
2.0 KiB
Go
Raw Normal View History

2017-05-27 13:51:25 +08:00
package needle
import (
2018-12-09 16:15:23 +08:00
"fmt"
"log"
2018-12-09 17:27:25 +08:00
"os"
2018-12-09 16:15:23 +08:00
"runtime"
"testing"
2018-12-15 21:55:56 +08:00
"time"
2018-12-09 13:45:14 +08:00
. "github.com/chrislusf/seaweedfs/weed/storage/types"
2018-12-09 16:15:23 +08:00
"github.com/chrislusf/seaweedfs/weed/util"
)
2018-12-09 13:45:14 +08:00
/*
To see the memory usage:
2018-12-09 16:15:23 +08:00
go test -run TestMemoryUsage
The TotalAlloc section shows the memory increase for each iteration.
2018-12-09 13:45:14 +08:00
go test -run TestMemoryUsage -memprofile=mem.out
2018-12-09 16:15:23 +08:00
go tool pprof --alloc_space needle.test mem.out
2018-12-09 13:45:14 +08:00
2018-12-09 17:27:25 +08:00
*/
2018-12-09 13:45:14 +08:00
func TestMemoryUsage(t *testing.T) {
2018-12-09 16:15:23 +08:00
var maps []*CompactMap
2019-03-14 14:07:24 +08:00
totalRowCount := uint64(0)
2018-12-09 16:15:23 +08:00
2018-12-15 21:55:56 +08:00
startTime := time.Now()
2018-12-09 16:15:23 +08:00
for i := 0; i < 10; i++ {
indexFile, ie := os.OpenFile("../../../test/sample.idx", os.O_RDWR|os.O_RDONLY, 0644)
if ie != nil {
log.Fatalln(ie)
}
2019-03-14 14:07:24 +08:00
m, rowCount := loadNewNeedleMap(indexFile)
maps = append(maps, m)
totalRowCount += rowCount
2013-01-17 16:56:56 +08:00
2018-12-09 16:15:23 +08:00
indexFile.Close()
2019-03-14 14:07:24 +08:00
PrintMemUsage(totalRowCount)
2018-12-15 21:55:56 +08:00
now := time.Now()
fmt.Printf("\tTaken = %v\n", now.Sub(startTime))
startTime = now
2018-12-09 16:15:23 +08:00
}
2018-12-09 13:45:14 +08:00
}
2019-03-14 14:07:24 +08:00
func loadNewNeedleMap(file *os.File) (*CompactMap, uint64) {
2013-01-17 16:56:56 +08:00
m := NewCompactMap()
2018-12-09 16:15:23 +08:00
bytes := make([]byte, NeedleEntrySize)
2019-03-14 14:07:24 +08:00
rowCount := uint64(0)
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 {
2019-03-14 14:07:24 +08:00
rowCount++
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
2019-04-09 10:40:56 +08:00
if !offset.IsZero() {
m.Set(NeedleId(key), offset, size)
2013-01-17 16:56:56 +08:00
} else {
2018-12-09 16:15:23 +08:00
m.Delete(key)
2013-01-17 16:56:56 +08:00
}
}
2013-01-17 16:56:56 +08:00
count, e = file.Read(bytes)
}
2018-12-09 13:45:14 +08:00
2019-03-14 14:07:24 +08:00
return m, rowCount
2018-12-09 13:45:14 +08:00
}
2019-03-14 14:07:24 +08:00
func PrintMemUsage(totalRowCount uint64) {
2018-12-15 21:55:56 +08:00
runtime.GC()
2018-12-09 16:15:23 +08:00
var m runtime.MemStats
runtime.ReadMemStats(&m)
// For info on each, see: https://golang.org/pkg/runtime/#MemStats
2019-03-14 14:14:40 +08:00
fmt.Printf("Each %.2f Bytes", float64(m.TotalAlloc)/float64(totalRowCount))
2019-03-14 14:07:24 +08:00
fmt.Printf("\tAlloc = %v MiB", bToMb(m.Alloc))
2018-12-09 16:15:23 +08:00
fmt.Printf("\tTotalAlloc = %v MiB", bToMb(m.TotalAlloc))
fmt.Printf("\tSys = %v MiB", bToMb(m.Sys))
2018-12-15 21:55:56 +08:00
fmt.Printf("\tNumGC = %v", m.NumGC)
2018-12-09 16:15:23 +08:00
}
func bToMb(b uint64) uint64 {
return b / 1024 / 1024
}