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.

86 lines
1.8 KiB

7 years ago
7 years ago
7 years ago
  1. package needle
  2. import (
  3. "testing"
  4. "os"
  5. "log"
  6. "github.com/chrislusf/seaweedfs/weed/util"
  7. . "github.com/chrislusf/seaweedfs/weed/storage/types"
  8. )
  9. /*
  10. To see the memory usage:
  11. go test -run TestMemoryUsage -memprofile=mem.out
  12. go tool pprof needle.test mem.out
  13. */
  14. func TestMemoryUsage(t *testing.T) {
  15. indexFile, ie := os.OpenFile("../../../test/sample.idx", os.O_RDWR|os.O_RDONLY, 0644)
  16. if ie != nil {
  17. log.Fatalln(ie)
  18. }
  19. loadNewNeedleMap(indexFile)
  20. indexFile.Close()
  21. }
  22. func loadNewNeedleMap(file *os.File) {
  23. m := NewCompactMap()
  24. bytes := make([]byte, NeedleEntrySize*1024)
  25. count, e := file.Read(bytes)
  26. for count > 0 && e == nil {
  27. for i := 0; i < count; i += NeedleEntrySize {
  28. key := BytesToNeedleId(bytes[i : i+NeedleIdSize])
  29. offset := BytesToOffset(bytes[i+NeedleIdSize : i+NeedleIdSize+OffsetSize])
  30. size := util.BytesToUint32(bytes[i+NeedleIdSize+OffsetSize : i+NeedleIdSize+OffsetSize+SizeSize])
  31. if offset > 0 {
  32. m.Set(NeedleId(key), offset, size)
  33. } else {
  34. //delete(m, key)
  35. }
  36. }
  37. count, e = file.Read(bytes)
  38. }
  39. m.report()
  40. }
  41. // report memory usage
  42. func (cm *CompactMap) report() {
  43. overFlowCount := 0;
  44. overwrittenByOverflow := 0;
  45. entryCount := 0
  46. compactSectionCount := 0
  47. compactSectionEntryCount := 0
  48. for _, cs := range cm.list {
  49. compactSectionCount++
  50. cs.RLock()
  51. for range cs.overflow {
  52. overFlowCount++
  53. entryCount++
  54. }
  55. for _, v := range cs.values {
  56. compactSectionEntryCount++
  57. if _, found := cs.overflow[v.Key]; !found {
  58. entryCount++
  59. } else {
  60. overwrittenByOverflow++
  61. }
  62. }
  63. cs.RUnlock()
  64. }
  65. println("overFlowCount", overFlowCount)
  66. println("overwrittenByOverflow", overwrittenByOverflow)
  67. println("entryCount", entryCount)
  68. println("compactSectionCount", compactSectionCount)
  69. println("compactSectionEntryCount", compactSectionEntryCount)
  70. }