seaweedfs/weed/storage/needle_map/compact_map_perf_test.go

93 lines
2 KiB
Go
Raw Normal View History

2019-04-19 04:43:36 +00:00
package needle_map
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"
. "github.com/seaweedfs/seaweedfs/weed/storage/types"
)
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
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++ {
2020-07-11 13:35:54 +00:00
indexFile, ie := os.OpenFile("../../../test/data/sample.idx", os.O_RDWR|os.O_RDONLY, 0644)
2018-12-09 08:15:23 +00:00
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
}
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()
2019-04-19 07:39:34 +00:00
bytes := make([]byte, NeedleMapEntrySize)
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 {
2019-04-19 07:39:34 +00:00
for i := 0; i < count; i += NeedleMapEntrySize {
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 := BytesToSize(bytes[i+NeedleIdSize+OffsetSize : i+NeedleIdSize+OffsetSize+SizeSize])
2018-07-08 09:39:04 +00:00
2019-04-09 02:40:56 +00:00
if !offset.IsZero() {
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
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
2023-11-26 19:47:20 +00:00
}