Skip to content

Instantly share code, notes, and snippets.

@aishfenton
Created December 5, 2012 23:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aishfenton/4220686 to your computer and use it in GitHub Desktop.
Save aishfenton/4220686 to your computer and use it in GitHub Desktop.
Benchmark BloomFilter serialization
require "benchmark"
require "mongoid"
require "bloomfilter-rb"
module BloomFilter
class Native
def self.unpack(str)
bitstring = str.unpack("B*").first
bytes = []
bitstring.each_byte { |b| bytes << (b & 1) }
bytes.pack("c*")
end
def self.demongoize(hash)
return nil unless hash
opts, bitfield = hash["opts"], hash["bitfield"].data
opts = Hash[opts.map {|k,v| [k.to_sym,v] } ]
bf = BloomFilter::Native.new(opts)
bf.bf.load(bitfield)
bf
end
def mongoize
{
bitfield: Moped::BSON::Binary.new(:generic, self.bitmap),
opts: @opts
}
end
end
end
class Test
include Mongoid::Document
field :name, type: String
field :users, type: BloomFilter::Native
end
def create_ids(n)
n.times.map do
str = ""
64.times do
str.concat(65 + Random.rand(26))
end
str
end
end
# ==============
# Tests
# ==============
Mongoid.load!("mongoid.yml", :development)
Mongoid.identity_map_enabled = false
Test.all.delete
def create_bf(size, false_positive = 0.01)
bitsize = (-(size * Math.log(false_positive)) / Math.log(2)**2).to_i
bf = BloomFilter::Native.new(:size => bitsize, :hashes => 7, :seed => 1, :bucket => 1)
create_ids(size).each do |id|
bf.insert(id)
end
bf.insert("contains me")
#p bf.stats
bf
end
def save_bf(bf)
Test.create(name: "Me", users: bf)
end
def load_bfs
facts = Test.where(name: "Me")
raise "Didn't deserialize" unless facts.all? { |f| f.users.include?("contains me") }
end
Benchmark.bm do |r|
r.report("create") { @bf = create_bf(200_000) }
r.report("save") { 30.times { save_bf(@bf) } }
r.report("load") { load_bfs }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment