2017-05-27 13:51:25 +08:00
|
|
|
package needle
|
2012-09-30 07:07:24 +08:00
|
|
|
|
|
|
|
import (
|
2018-12-09 16:15:23 +08:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"runtime"
|
2012-09-30 07:07:24 +08:00
|
|
|
"testing"
|
2018-12-09 13:45:14 +08:00
|
|
|
"os"
|
2014-10-27 02:34:55 +08:00
|
|
|
|
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"
|
2012-09-30 07:07:24 +08:00
|
|
|
)
|
|
|
|
|
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
|
|
|
|
|
|
|
*/
|
|
|
|
|
2012-09-30 07:07:24 +08:00
|
|
|
func TestMemoryUsage(t *testing.T) {
|
|
|
|
|
2018-12-09 16:15:23 +08:00
|
|
|
var maps []*CompactMap
|
|
|
|
|
|
|
|
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 16:56:56 +08:00
|
|
|
|
2018-12-09 16:15:23 +08:00
|
|
|
indexFile.Close()
|
|
|
|
|
|
|
|
PrintMemUsage()
|
|
|
|
}
|
2018-12-09 13:45:14 +08:00
|
|
|
|
2012-09-30 07:07:24 +08:00
|
|
|
}
|
|
|
|
|
2018-12-09 16:15:23 +08:00
|
|
|
func loadNewNeedleMap(file *os.File) *CompactMap {
|
2013-01-17 16:56:56 +08:00
|
|
|
m := NewCompactMap()
|
2018-12-09 16:15:23 +08:00
|
|
|
bytes := make([]byte, NeedleEntrySize)
|
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 {
|
2018-07-08 17:28:04 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2012-09-30 07:07:24 +08:00
|
|
|
|
2013-01-17 16:56:56 +08:00
|
|
|
count, e = file.Read(bytes)
|
|
|
|
}
|
2018-12-09 13:45:14 +08:00
|
|
|
|
2018-12-09 16:15:23 +08:00
|
|
|
return m
|
2018-12-09 13:45:14 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-12-09 16:15:23 +08:00
|
|
|
func PrintMemUsage() {
|
|
|
|
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))
|
|
|
|
fmt.Printf("\tNumGC = %v\n", m.NumGC)
|
|
|
|
}
|
|
|
|
func bToMb(b uint64) uint64 {
|
|
|
|
return b / 1024 / 1024
|
2012-09-30 07:07:24 +08:00
|
|
|
}
|