<commit_before>package needle

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

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

/*

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

	startTime := time.Now()
	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))

		indexFile.Close()

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

}

func loadNewNeedleMap(file *os.File) *CompactMap {
	m := NewCompactMap()
	bytes := make([]byte, NeedleEntrySize)
	count, e := file.Read(bytes)
	for count > 0 && e == nil {
		for i := 0; i < count; i += NeedleEntrySize {
			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])

			if offset > 0 {
				m.Set(NeedleId(key), offset, size)
			} else {
				m.Delete(key)
			}
		}

		count, e = file.Read(bytes)
	}

	return m

}

func PrintMemUsage() {

	runtime.GC()
	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", m.NumGC)
}
func bToMb(b uint64) uint64 {
	return b / 1024 / 1024
}<commit_msg>print out per entry memory usage<commit_after>package needle

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

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

/*

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/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, NeedleEntrySize)
	rowCount := uint64(0)
	count, e := file.Read(bytes)
	for count > 0 && e == nil {
		for i := 0; i < count; i += NeedleEntrySize {
			rowCount++
			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])

			if offset > 0 {
				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 %v Bytes", m.Alloc/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
}
{'fraction_non_alphanumeric': 0.15758951084215836, 'fraction_numerical': 0.014624306606152295, 'mean_word_length': 3.8084848484848486, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2757261', 'n_tokens_mistral': 1699, 'n_tokens_neox': 1556, 'n_words': 338}