Skip to content

Instantly share code, notes, and snippets.

@buger
Created March 18, 2012 13:42
Show Gist options
  • Save buger/2072746 to your computer and use it in GitHub Desktop.
Save buger/2072746 to your computer and use it in GitHub Desktop.
Mongo benchmark
require 'benchmark'
require 'mongo'
r = Random.new
$db = Mongo::Connection.new.db("test")
def create_collection site_id
coll = $db["rules_#{site_id}"]
coll.drop()
coll.create_index(
[
["rule_location", Mongo::GEO2D]
]
)
coll.create_index(
[
["rule_location", Mongo::GEO2D],
["rule_order_sum_start", Mongo::ASCENDING],
["rule_order_sum_end", Mongo::ASCENDING],
["rule_page_visit_count", Mongo::ASCENDING],
["rule_site_visit_count", Mongo::ASCENDING],
["rule_order_state", Mongo::ASCENDING]
],
{ name: "rule_index" }
)
coll
end
=begin
Example of generated rule:
{
"campaign_id" : 1,
"site_id" : 1,
"offer_value" : 11,
"offer_type" : "discount",
"rule_location" : [ 2, 2 ],
"rule_order_sum_start" : 40,
"rule_order_sum_end" : 60,
"rule_site_visit_count" : 6,
"rule_order_state" : 1,
"rule_product_price" : 2
},
{
"campaign_id" : 2,
"site_id" : 1,
"offer_value" : 10,
"offer_type" : "discount",
"rule_site_visit_count" : 5
}
=end
puts "filling mongo with rules:",
Benchmark.measure {
10.times do |site_id| # Clients count
rules = create_collection site_id
records = []
10.times do |campaign_id| # each client has 10 campaigns
300.times do |rule_id| # each campaign have 300 rules
record = {
campaign_id: campaign_id,
site_id: site_id,
offer_value: r.rand(10..20),
offer_type: "discount"
}
has_rule = false
if r.rand(2) == 1 # insert this rule only in every third record
record[:rule_location] = [r.rand(10), r.rand(20)]
has_rule = true
end
if r.rand(2) == 1
record[:rule_order_sum_start] = r.rand(30..60)
record[:rule_order_sum_end] = r.rand(60..80)
has_rule = true
end
if r.rand(2) == 1
record[:rule_page_visit_count] = 1 + rand(10)
has_rule = true
end
if r.rand(2) == 1
record[:rule_site_visit_count] = 1 + rand(10)
has_rule = true
end
if r.rand(2) == 1
record[:rule_order_sum_start] = r.rand(30..60)
record[:rule_order_sum_end] = r.rand(60..80)
has_rule = true
end
unless has_rule # at least one rules should be
record[:rule_order_state] = rand(3)
record[:rule_page_visit_count] = 1 + rand(10)
end
records.push record
end
end
rules.insert records
end
}
result = nil
puts Benchmark.measure {
10000.times do
user = {
location: [r.rand(10), r.rand(10)],
order_sum: r.rand(20..100),
site_visit_count: r.rand(20),
page_visit_count: r.rand(20),
order_state: r.rand(3),
site_id: r.rand(9)
}
result = $db["rules_#{user[:site_id]}"].find(
{
"$and" => [
{
"$or" => [
{ rule_location: user[:location] },
{ rule_location: {"$exists" => false} }
]
},
{
"$or" => [
{ rule_order_sum_start: { "$lte" => user[:order_sum] } },
{ rule_order_sum_start: { "$exists" => false } }
]
},
{
"$or" => [
{ rule_order_sum_end: { "$gte" => user[:order_sum] } },
{ rule_order_sum_end: { "$exists" => false } }
]
},
{
"$or" => [
{ rule_page_visit_count: { "$gte" => user[:page_visit_count] } },
{ rule_page_visit_count: { "$exists" => false } }
]
},
{
"$or" => [
{ rule_site_visit_count: { "$gte" => user[:site_visit_count] } },
{ rule_site_visit_count: { "$exists" => false } }
]
},
{
"$or" => [
{ rule_order_state: user[:order_state]},
{ rule_order_state: nil }
]
}
]
}
)
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment