You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
1.7 KiB

6 years ago
6 years ago
7 years ago
7 years ago
  1. package needle
  2. import (
  3. "fmt"
  4. "log"
  5. "os"
  6. "runtime"
  7. "testing"
  8. . "github.com/chrislusf/seaweedfs/weed/storage/types"
  9. "github.com/chrislusf/seaweedfs/weed/util"
  10. )
  11. /*
  12. To see the memory usage:
  13. go test -run TestMemoryUsage
  14. The TotalAlloc section shows the memory increase for each iteration.
  15. go test -run TestMemoryUsage -memprofile=mem.out
  16. go tool pprof --alloc_space needle.test mem.out
  17. */
  18. func TestMemoryUsage(t *testing.T) {
  19. var maps []*CompactMap
  20. for i := 0; i < 10; i++ {
  21. indexFile, ie := os.OpenFile("../../../test/sample.idx", os.O_RDWR|os.O_RDONLY, 0644)
  22. if ie != nil {
  23. log.Fatalln(ie)
  24. }
  25. maps = append(maps, loadNewNeedleMap(indexFile))
  26. indexFile.Close()
  27. PrintMemUsage()
  28. }
  29. }
  30. func loadNewNeedleMap(file *os.File) *CompactMap {
  31. m := NewCompactMap()
  32. bytes := make([]byte, NeedleEntrySize)
  33. count, e := file.Read(bytes)
  34. for count > 0 && e == nil {
  35. for i := 0; i < count; i += NeedleEntrySize {
  36. key := BytesToNeedleId(bytes[i : i+NeedleIdSize])
  37. offset := BytesToOffset(bytes[i+NeedleIdSize : i+NeedleIdSize+OffsetSize])
  38. size := util.BytesToUint32(bytes[i+NeedleIdSize+OffsetSize : i+NeedleIdSize+OffsetSize+SizeSize])
  39. if offset > 0 {
  40. m.Set(NeedleId(key), offset, size)
  41. } else {
  42. m.Delete(key)
  43. }
  44. }
  45. count, e = file.Read(bytes)
  46. }
  47. return m
  48. }
  49. func PrintMemUsage() {
  50. var m runtime.MemStats
  51. runtime.ReadMemStats(&m)
  52. // For info on each, see: https://golang.org/pkg/runtime/#MemStats
  53. fmt.Printf("Alloc = %v MiB", bToMb(m.Alloc))
  54. fmt.Printf("\tTotalAlloc = %v MiB", bToMb(m.TotalAlloc))
  55. fmt.Printf("\tSys = %v MiB", bToMb(m.Sys))
  56. fmt.Printf("\tNumGC = %v\n", m.NumGC)
  57. }
  58. func bToMb(b uint64) uint64 {
  59. return b / 1024 / 1024
  60. }