seaweedfs/weed/storage/needle/compact_map_perf_test.go

88 lines
1.8 KiB
Go
Raw Normal View History

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