aboutsummaryrefslogtreecommitdiff
path: root/weed/storage/needle_map/compact_map_perf_test.go
blob: 081fb34e9f5b2ab3087438c692a178b9c6692c28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package needle_map

import (
	"fmt"
	"log"
	"os"
	"runtime"
	"testing"
	"time"

	. "github.com/chrislusf/seaweedfs/weed/storage/types"
)

/*

To see the memory usage:

go test -run TestMemoryUsage
The TotalAlloc section shows the memory increase for each iteration.

go test -run TestMemoryUsage -memprofile=mem.out
go tool pprof --alloc_space needle.test mem.out


*/

func TestMemoryUsage(t *testing.T) {

	var maps []*CompactMap
	totalRowCount := uint64(0)

	startTime := time.Now()
	for i := 0; i < 10; i++ {
		indexFile, ie := os.OpenFile("../../../test/data/sample.idx", os.O_RDWR|os.O_RDONLY, 0644)
		if ie != nil {
			log.Fatalln(ie)
		}
		m, rowCount := loadNewNeedleMap(indexFile)
		maps = append(maps, m)
		totalRowCount += rowCount

		indexFile.Close()

		PrintMemUsage(totalRowCount)
		now := time.Now()
		fmt.Printf("\tTaken = %v\n", now.Sub(startTime))
		startTime = now
	}

}

func loadNewNeedleMap(file *os.File) (*CompactMap, uint64) {
	m := NewCompactMap()
	bytes := make([]byte, NeedleMapEntrySize)
	rowCount := uint64(0)
	count, e := file.Read(bytes)
	for count > 0 && e == nil {
		for i := 0; i < count; i += NeedleMapEntrySize {
			rowCount++
			key := BytesToNeedleId(bytes[i : i+NeedleIdSize])
			offset := BytesToOffset(bytes[i+NeedleIdSize : i+NeedleIdSize+OffsetSize])
			size := BytesToSize(bytes[i+NeedleIdSize+OffsetSize : i+NeedleIdSize+OffsetSize+SizeSize])

			if !offset.IsZero() {
				m.Set(NeedleId(key), offset, size)
			} else {
				m.Delete(key)
			}
		}

		count, e = file.Read(bytes)
	}

	return m, rowCount

}

func PrintMemUsage(totalRowCount uint64) {

	runtime.GC()
	var m runtime.MemStats
	runtime.ReadMemStats(&m)
	// For info on each, see: https://golang.org/pkg/runtime/#MemStats
	fmt.Printf("Each %.2f Bytes", float64(m.TotalAlloc)/float64(totalRowCount))
	fmt.Printf("\tAlloc = %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", m.NumGC)
}
func bToMb(b uint64) uint64 {
	return b / 1024 / 1024
}