2017-05-27 05:51:25 +00:00
|
|
|
package needle
|
2012-09-29 23:07:24 +00:00
|
|
|
|
|
|
|
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"
|
2012-09-29 23:07:24 +00:00
|
|
|
"testing"
|
2018-12-15 13:55:56 +00:00
|
|
|
"time"
|
2014-10-26 18:34:55 +00:00
|
|
|
|
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"
|
2012-09-29 23:07:24 +00:00
|
|
|
)
|
|
|
|
|
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
|
|
|
|
2012-09-29 23:07:24 +00:00
|
|
|
func TestMemoryUsage(t *testing.T) {
|
|
|
|
|
2018-12-09 08:15:23 +00:00
|
|
|
var maps []*CompactMap
|
2019-03-14 06:07:24 +00:00
|
|
|
totalRowCount := uint64(0)
|
2018-12-09 08:15:23 +00:00
|
|
|
|
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)
|
|
|
|
}
|
2019-03-14 06:07:24 +00:00
|
|
|
m, rowCount := loadNewNeedleMap(indexFile)
|
|
|
|
maps = append(maps, m)
|
|
|
|
totalRowCount += rowCount
|
2013-01-17 08:56:56 +00:00
|
|
|
|
2018-12-09 08:15:23 +00:00
|
|
|
indexFile.Close()
|
|
|
|
|
2019-03-14 06:07:24 +00:00
|
|
|
PrintMemUsage(totalRowCount)
|
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
|
|
|
|
2012-09-29 23:07:24 +00:00
|
|
|
}
|
|
|
|
|
2019-03-14 06:07:24 +00:00
|
|
|
func loadNewNeedleMap(file *os.File) (*CompactMap, uint64) {
|
2013-01-17 08:56:56 +00:00
|
|
|
m := NewCompactMap()
|
2018-12-09 08:15:23 +00:00
|
|
|
bytes := make([]byte, NeedleEntrySize)
|
2019-03-14 06:07:24 +00:00
|
|
|
rowCount := uint64(0)
|
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 {
|
2019-03-14 06:07:24 +00:00
|
|
|
rowCount++
|
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 {
|
2018-07-08 09:28:04 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2012-09-29 23:07:24 +00:00
|
|
|
|
2013-01-17 08:56:56 +00:00
|
|
|
count, e = file.Read(bytes)
|
|
|
|
}
|
2018-12-09 05:45:14 +00:00
|
|
|
|
2019-03-14 06:07:24 +00:00
|
|
|
return m, rowCount
|
2018-12-09 05:45:14 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-03-14 06:07:24 +00:00
|
|
|
func PrintMemUsage(totalRowCount uint64) {
|
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
|
2019-03-14 06:14:40 +00:00
|
|
|
fmt.Printf("Each %.2f Bytes", float64(m.TotalAlloc)/float64(totalRowCount))
|
2019-03-14 06:07:24 +00:00
|
|
|
fmt.Printf("\tAlloc = %v MiB", bToMb(m.Alloc))
|
2018-12-09 08:15:23 +00:00
|
|
|
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
|
2012-09-29 23:07:24 +00:00
|
|
|
}
|