Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

81 rader
2.3 KiB

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