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.

82 line
2.3 KiB

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