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.

169 lines
4.1 KiB

  1. package storage
  2. import (
  3. "math/rand"
  4. "os"
  5. "runtime"
  6. "testing"
  7. "github.com/chrislusf/weed-fs/go/glog"
  8. )
  9. var testIndexFilename string = "../../test/sample.idx"
  10. func TestCdbMap0Convert(t *testing.T) {
  11. indexFile, err := os.Open(testIndexFilename)
  12. if err != nil {
  13. t.Fatalf("cannot open %s: %s", testIndexFilename, err)
  14. }
  15. defer indexFile.Close()
  16. cdbFn := testIndexFilename + ".cdb"
  17. t.Logf("converting %s to %s", cdbFn, cdbFn)
  18. if err = ConvertIndexToCdb(cdbFn, indexFile); err != nil {
  19. t.Fatalf("error while converting: %s", err)
  20. }
  21. }
  22. func TestCdbMap1Mem(t *testing.T) {
  23. var nm NeedleMapper
  24. i := 0
  25. visit := func(nv NeedleValue) error {
  26. i++
  27. return nil
  28. }
  29. a := getMemStats()
  30. t.Logf("opening %s.cdb", testIndexFilename)
  31. nm, err := OpenCdbMap(testIndexFilename + ".cdb")
  32. if err != nil {
  33. t.Fatalf("error opening cdb: %s", err)
  34. }
  35. b := getMemStats()
  36. glog.V(0).Infof("opening cdb consumed %d bytes", b-a)
  37. defer nm.Close()
  38. a = getMemStats()
  39. if err = nm.Visit(visit); err != nil {
  40. t.Fatalf("error visiting %s: %s", nm, err)
  41. }
  42. b = getMemStats()
  43. glog.V(0).Infof("visit cdb %d consumed %d bytes", i, b-a)
  44. nm.Close()
  45. indexFile, err := os.Open(testIndexFilename)
  46. if err != nil {
  47. t.Fatalf("error opening idx: %s", err)
  48. }
  49. a = getMemStats()
  50. nm, err = LoadNeedleMap(indexFile)
  51. if err != nil {
  52. t.Fatalf("error loading idx: %s", err)
  53. }
  54. defer nm.Close()
  55. b = getMemStats()
  56. glog.V(0).Infof("opening idx consumed %d bytes", b-a)
  57. i = 0
  58. a = getMemStats()
  59. if err = nm.Visit(visit); err != nil {
  60. t.Fatalf("error visiting %s: %s", nm, err)
  61. }
  62. b = getMemStats()
  63. glog.V(0).Infof("visit idx %d consumed %d bytes", i, b-a)
  64. }
  65. func BenchmarkCdbMap9List(t *testing.B) {
  66. t.StopTimer()
  67. indexFile, err := os.Open(testIndexFilename)
  68. if err != nil {
  69. t.Fatalf("cannot open %s: %s", testIndexFilename, err)
  70. }
  71. defer indexFile.Close()
  72. a := getMemStats()
  73. t.Logf("opening %s", indexFile)
  74. idx, err := LoadNeedleMap(indexFile)
  75. if err != nil {
  76. t.Fatalf("cannot load %s: %s", indexFile.Name(), err)
  77. }
  78. defer idx.Close()
  79. b := getMemStats()
  80. glog.V(0).Infof("LoadNeedleMap consumed %d bytes", b-a)
  81. cdbFn := testIndexFilename + ".cdb"
  82. a = getMemStats()
  83. t.Logf("opening %s", cdbFn)
  84. m, err := OpenCdbMap(cdbFn)
  85. if err != nil {
  86. t.Fatalf("error opening %s: %s", cdbFn, err)
  87. }
  88. defer m.Close()
  89. b = getMemStats()
  90. glog.V(0).Infof("OpenCdbMap consumed %d bytes", b-a)
  91. i := 0
  92. glog.V(0).Infoln("checking whether the cdb contains every key")
  93. t.StartTimer()
  94. err = idx.Visit(func(nv NeedleValue) error {
  95. if i > t.N || rand.Intn(10) < 9 {
  96. return nil
  97. }
  98. i++
  99. if i%1000 == 0 {
  100. glog.V(0).Infof("%d. %s", i, nv)
  101. }
  102. if nv2, ok := m.Get(uint64(nv.Key)); !ok || nv2 == nil {
  103. t.Errorf("%d in index, not in cdb", nv.Key)
  104. } else if nv2.Key != nv.Key {
  105. t.Errorf("requested key %d from cdb, got %d", nv.Key, nv2.Key)
  106. } else if nv2.Offset != nv.Offset {
  107. t.Errorf("offset is %d in index, %d in cdb", nv.Offset, nv2.Offset)
  108. } else if nv2.Size != nv.Size {
  109. t.Errorf("size is %d in index, %d in cdb", nv.Size, nv2.Size)
  110. }
  111. t.SetBytes(int64(nv.Size))
  112. return nil
  113. })
  114. t.StopTimer()
  115. if err != nil {
  116. t.Errorf("error visiting index: %s", err)
  117. }
  118. i = 0
  119. glog.V(0).Infoln("checking wheter the cdb contains no stray keys")
  120. t.StartTimer()
  121. err = m.Visit(func(nv NeedleValue) error {
  122. if i > t.N || rand.Intn(10) < 9 {
  123. return nil
  124. }
  125. if nv2, ok := m.Get(uint64(nv.Key)); !ok || nv2 == nil {
  126. t.Errorf("%d in cdb, not in index", nv.Key)
  127. } else if nv2.Key != nv.Key {
  128. t.Errorf("requested key %d from index, got %d", nv.Key, nv2.Key)
  129. } else if nv2.Offset != nv.Offset {
  130. t.Errorf("offset is %d in cdb, %d in index", nv.Offset, nv2.Offset)
  131. } else if nv2.Size != nv.Size {
  132. t.Errorf("size is %d in cdb, %d in index", nv.Size, nv2.Size)
  133. }
  134. i++
  135. if i%1000 == 0 {
  136. glog.V(0).Infof("%d. %s", i, nv)
  137. }
  138. t.SetBytes(int64(nv.Size))
  139. return nil
  140. })
  141. t.StopTimer()
  142. if err != nil {
  143. t.Errorf("error visiting index: %s", err)
  144. }
  145. }
  146. var mem = new(runtime.MemStats)
  147. // returns MemStats.Alloc after a GC
  148. func getMemStats() int64 {
  149. runtime.GC()
  150. runtime.ReadMemStats(mem)
  151. return int64(mem.Alloc)
  152. }