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.

83 lines
2.4 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. require "./spec_helper"
  2. describe Crystal::Scatter do
  3. it "add elements to RingGraphs" do
  4. rg = Crystal::Scatter::RingGraph.new 0
  5. rg.add(UInt64.new(1),[0,0,0,0,0,0],"OSD0")
  6. rg.add(UInt64.new(1),[0,0,0,0,0,1],"OSD1")
  7. rg.add(UInt64.new(1),[0,0,0,0,1,0],"OSD2")
  8. rg.get_weight.should eq(3)
  9. rg.generate_ring(BigRational.new(1))
  10. rg.first.first.first.first.first.first.range_effector.not_nil!.[0].should eq(0)
  11. rg.last.last.last.last.last.last.range_effector.not_nil!.[1].should eq(UInt64::MAX)
  12. end
  13. it "slices data into multiple pods" do
  14. mr = Crystal::Scatter::MetaRing.new(3)
  15. mr.add Crystal::Scatter::Daemon.new(1,[0,0,0,0,0,0],"OSD0")
  16. mr.add [Crystal::Scatter::Daemon.new(1,[0,0,0,0,0,1],"OSD1"),Crystal::Scatter::Daemon.new(1,[0,0,0,0,0,2],"OSD2")]
  17. (1..1000).each do |numb|
  18. slices = mr.get_slices_for(numb.to_s)
  19. slices_history = mr.get_slices_for(numb.to_s,2)
  20. set = Set(String).new
  21. slices.each do |sl|
  22. set<<sl.url
  23. end
  24. slices_history.slices.size.should eq(2)
  25. set.size.should eq(3)
  26. end
  27. end
  28. it "hashes data fast" do
  29. v = Crystal::Scatter::MetaRing.new(3)
  30. time = Time.now
  31. (1..100).each do |numb|
  32. v.hash(numb.to_s)
  33. end
  34. t = (Time.now - time)
  35. (t.milliseconds <= 1).should be_true
  36. end
  37. it "hashes data good" do
  38. v = Crystal::Scatter::MetaRing.new(3)
  39. hash_nb =1000000
  40. store = Hash(UInt64, Int32).new
  41. (1..hash_nb).each do |numb|
  42. h = v.hash(numb.to_s)
  43. nb=store.fetch(h,0)
  44. store[h]=nb+1
  45. end
  46. conflicts = hash_nb-store.size
  47. (conflicts <= 100).should be_true
  48. mod_array = Array(Int32).new(1000,0)
  49. slice_array = Array(Int32).new(1000,0)
  50. store.each_key do |key|
  51. key_mod = key%1000
  52. mod_array[key_mod]+=1
  53. key_slice = key/(UInt64::MAX/1000)
  54. slice_array[key_slice]+=1
  55. end
  56. good_mod = true
  57. good_slice = true
  58. mod_array.each do |v|
  59. good_mod&=(v > (hash_nb/1000*4/5))
  60. end
  61. slice_array.each do |v|
  62. good_slice&=(v > (hash_nb/1000*4/5))
  63. end
  64. good_mod.should be_true
  65. good_slice.should be_true
  66. end
  67. end