hll-err.rb 638 B

123456789101112131415161718192021222324252627
  1. # hll-err.rb - Copyright (C) 2014 Salvatore Sanfilippo
  2. # BSD license, See the COPYING file for more information.
  3. #
  4. # Check error of HyperLogLog Redis implementation for different set sizes.
  5. require 'rubygems'
  6. require 'redis'
  7. require 'digest/sha1'
  8. r = Redis.new
  9. r.del('hll')
  10. i = 0
  11. while true do
  12. 100.times {
  13. elements = []
  14. 1000.times {
  15. ele = Digest::SHA1.hexdigest(i.to_s)
  16. elements << ele
  17. i += 1
  18. }
  19. r.pfadd('hll',*elements)
  20. }
  21. approx = r.pfcount('hll')
  22. abs_err = (approx-i).abs
  23. rel_err = 100.to_f*abs_err/i
  24. puts "#{i} vs #{approx}: #{rel_err}%"
  25. end