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.

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