2009-02-10 19:17:55 +00:00
|
|
|
#!/usr/bin/env ruby
|
2011-08-25 19:08:21 +00:00
|
|
|
$LOAD_PATH[0,0] = File.join(File.dirname(__FILE__), '..', 'lib')
|
2009-02-10 19:17:55 +00:00
|
|
|
#
|
|
|
|
# Note: Ruby 1.9 is faster than 1.8, as expected.
|
2009-11-18 15:46:22 +00:00
|
|
|
# This suite will be run against the installed version of ruby-mongo-driver.
|
2010-04-08 03:47:06 +00:00
|
|
|
# The c-extension, bson_ext, will be used if installed.
|
2009-02-10 19:17:55 +00:00
|
|
|
|
2009-11-16 15:40:16 +00:00
|
|
|
require 'rubygems'
|
2009-02-10 19:17:55 +00:00
|
|
|
require 'mongo'
|
2010-01-13 18:11:09 +00:00
|
|
|
require 'benchmark'
|
2009-02-10 19:17:55 +00:00
|
|
|
|
2009-08-20 14:50:48 +00:00
|
|
|
include Mongo
|
2009-02-10 19:17:55 +00:00
|
|
|
|
2009-02-26 15:39:39 +00:00
|
|
|
TRIALS = 2
|
|
|
|
PER_TRIAL = 5000
|
|
|
|
BATCH_SIZE = 100
|
2009-02-10 19:17:55 +00:00
|
|
|
|
2009-02-26 15:39:39 +00:00
|
|
|
SMALL = {}
|
|
|
|
MEDIUM = {
|
|
|
|
'integer' => 5,
|
|
|
|
'number' => 5.05,
|
|
|
|
'boolean' => false,
|
|
|
|
'array' => ['test', 'benchmark']
|
|
|
|
}
|
|
|
|
LARGE = {
|
|
|
|
'base_url' => 'http://www.example.com/test-me',
|
|
|
|
'total_word_count' => 6743,
|
|
|
|
'access_time' => Time.now,
|
|
|
|
'meta_tags' => {
|
|
|
|
'description' => 'i am a long description string',
|
|
|
|
'author' => 'Holly Man',
|
|
|
|
'dynamically_created_meta_tag' => 'who know\n what'
|
|
|
|
},
|
|
|
|
'page_structure' => {
|
|
|
|
'counted_tags' => 3450,
|
|
|
|
'no_of_js_attached' => 10,
|
|
|
|
'no_of_images' => 6
|
|
|
|
},
|
|
|
|
'harvested_words' => ['10gen','web','open','source','application','paas',
|
|
|
|
'platform-as-a-service','technology','helps',
|
|
|
|
'developers','focus','building','mongodb','mongo'] * 20
|
|
|
|
}
|
|
|
|
|
2010-01-07 19:39:02 +00:00
|
|
|
def print_headings
|
2010-09-14 15:09:34 +00:00
|
|
|
puts "\nMongoDB Ruby Driver -- Standard Benchmark"
|
|
|
|
puts Time.now.utc.strftime("%d-%b-%Y")
|
|
|
|
puts `ruby -v` + "\n"
|
|
|
|
puts "Latest Commit:"
|
|
|
|
puts `git log | head -n4`
|
|
|
|
puts "#{PER_TRIAL} documents or queries per trial. Batches of #{BATCH_SIZE} on batch inserts."
|
2010-01-07 19:39:02 +00:00
|
|
|
printf("\n%s%-10s %-15s %-10s %-15s\n\n", "Test".ljust(40, ' '), "(real)", "(real ops/s)", "(user)", "(user ops/s)")
|
|
|
|
end
|
|
|
|
|
|
|
|
def report(str, t, u=nil)
|
|
|
|
printf("%s %-10.2f %-15d %-10.2f %-15d\n", str.ljust(40, '.'), t, (PER_TRIAL / t), u, PER_TRIAL / u)
|
2009-02-10 19:17:55 +00:00
|
|
|
end
|
|
|
|
|
2009-10-16 17:38:13 +00:00
|
|
|
def profile(str)
|
|
|
|
if ENV['MONGO_PROFILE']
|
|
|
|
require 'rubygems'
|
|
|
|
require 'ruby-prof'
|
|
|
|
prof_results = RubyProf.profile do
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
File.open("benchmark/#{str}.html", "w") do |f|
|
|
|
|
RubyProf::GraphHtmlPrinter.new(prof_results).print(f, :min_percent => 5)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-10-01 13:52:29 +00:00
|
|
|
def benchmark(str, n, coll_name, data, create_index=false, verbosity=true)
|
2009-11-16 23:34:25 +00:00
|
|
|
coll = @db.collection(coll_name)
|
|
|
|
coll.create_index('x') if create_index
|
2009-10-16 17:38:13 +00:00
|
|
|
profile(str) do
|
2010-01-07 19:39:02 +00:00
|
|
|
GC.start
|
|
|
|
tm = Benchmark::Tms.new
|
|
|
|
td = tm.add do
|
|
|
|
n.times { |i| yield(coll, i) }
|
|
|
|
end
|
2010-10-01 13:52:29 +00:00
|
|
|
report(str, td.real, td.utime) if verbosity
|
2009-10-16 17:38:13 +00:00
|
|
|
end
|
2009-02-10 20:12:36 +00:00
|
|
|
end
|
|
|
|
|
2009-02-10 19:17:55 +00:00
|
|
|
host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
|
2009-08-20 22:48:09 +00:00
|
|
|
port = ENV['MONGO_RUBY_DRIVER_PORT'] || Connection::DEFAULT_PORT
|
2009-02-10 19:17:55 +00:00
|
|
|
|
2009-08-20 22:48:09 +00:00
|
|
|
connection = Connection.new(host, port)
|
2009-03-24 16:01:08 +00:00
|
|
|
connection.drop_database("benchmark")
|
2009-11-12 22:22:24 +00:00
|
|
|
@db = connection.db('benchmark')
|
2009-02-10 19:17:55 +00:00
|
|
|
|
2010-10-01 13:52:29 +00:00
|
|
|
def benchmark_insert(desc, coll_name, data, verbosity=true)
|
|
|
|
benchmark(desc, PER_TRIAL, coll_name, data, verbosity) do |coll, i|
|
2009-11-16 23:34:25 +00:00
|
|
|
data['x'] = i
|
|
|
|
coll.insert(data)
|
2009-11-17 22:47:49 +00:00
|
|
|
data.delete(:_id)
|
2009-11-12 22:22:24 +00:00
|
|
|
end
|
|
|
|
end
|
2009-02-10 19:17:55 +00:00
|
|
|
|
2009-11-16 23:34:25 +00:00
|
|
|
def benchmark_insert_index(desc, coll_name, data)
|
|
|
|
benchmark(desc, PER_TRIAL, coll_name, data, true) do |coll, i|
|
|
|
|
data['x'] = i
|
|
|
|
coll.insert(data)
|
2009-11-17 22:47:49 +00:00
|
|
|
data.delete(:_id)
|
2009-11-12 22:22:24 +00:00
|
|
|
end
|
|
|
|
end
|
2009-02-26 15:39:39 +00:00
|
|
|
|
2010-01-07 19:39:02 +00:00
|
|
|
|
|
|
|
print_headings
|
|
|
|
|
2010-10-01 13:52:29 +00:00
|
|
|
if RUBY_PLATFORM =~ /java/
|
|
|
|
puts "***WARMUP***"
|
|
|
|
benchmark_insert('insert (small, no index)', 'small_none', SMALL, false)
|
|
|
|
benchmark_insert('insert (medium, no index)', 'medium_none', MEDIUM, false)
|
|
|
|
benchmark_insert('insert (large, no index)', 'large_none', LARGE, false)
|
|
|
|
puts "***WARMUP***"
|
|
|
|
end
|
2010-01-07 19:39:02 +00:00
|
|
|
benchmark_insert('insert (small, no index)', 'small_none', SMALL)
|
2009-11-12 22:22:24 +00:00
|
|
|
benchmark_insert('insert (medium, no index)', 'medium_none', MEDIUM)
|
|
|
|
benchmark_insert('insert (large, no index)', 'large_none', LARGE)
|
|
|
|
|
2010-10-01 13:52:29 +00:00
|
|
|
|
2010-01-07 19:39:02 +00:00
|
|
|
benchmark_insert_index('insert (small, index)', 'small_indexed', SMALL)
|
2009-11-12 22:22:24 +00:00
|
|
|
benchmark_insert_index('insert (medium, index)', 'medium_indexed', MEDIUM)
|
|
|
|
benchmark_insert_index('insert (large, index)', 'large_indexed', LARGE)
|
2010-01-07 19:39:02 +00:00
|
|
|
|
2009-11-16 23:34:25 +00:00
|
|
|
def benchmark_insert_batch(desc, coll_name, data)
|
2010-01-07 19:39:02 +00:00
|
|
|
benchmark(desc, PER_TRIAL / BATCH_SIZE, coll_name, data) do |coll, i|
|
2009-11-16 23:34:25 +00:00
|
|
|
data['x'] = i
|
|
|
|
coll.insert([data] * BATCH_SIZE)
|
2009-11-17 22:47:49 +00:00
|
|
|
data.delete(:_id)
|
2009-11-12 22:22:24 +00:00
|
|
|
end
|
|
|
|
end
|
2009-02-26 15:39:39 +00:00
|
|
|
|
2010-01-07 19:39:02 +00:00
|
|
|
benchmark_insert_batch('insert batch (small, index)', 'small_bulk', SMALL)
|
2009-11-12 22:22:24 +00:00
|
|
|
benchmark_insert_batch('insert batch (medium, index)', 'medium_bulk', MEDIUM)
|
|
|
|
benchmark_insert_batch('insert batch (large, index)', 'large_bulk', LARGE)
|
2009-02-26 15:39:39 +00:00
|
|
|
|
2009-11-16 23:34:25 +00:00
|
|
|
def benchmark_find_one(desc, coll_name, data)
|
2010-01-07 19:39:02 +00:00
|
|
|
benchmark(desc, PER_TRIAL, coll_name, data) do |coll, i|
|
2009-11-16 23:34:25 +00:00
|
|
|
coll.find_one({'x' => data})
|
2009-11-12 22:22:24 +00:00
|
|
|
end
|
|
|
|
end
|
2009-02-26 15:39:39 +00:00
|
|
|
|
2009-11-12 22:22:24 +00:00
|
|
|
benchmark_find_one('find_one (small, no index)', 'small_none', PER_TRIAL / 2)
|
|
|
|
benchmark_find_one('find_one (medium, no index)', 'medium_none', PER_TRIAL / 2)
|
|
|
|
benchmark_find_one('find_one (large, no index)', 'large_none', PER_TRIAL / 2)
|
2010-10-01 13:52:29 +00:00
|
|
|
benchmark_find_one('find_one (small, no index)', 'small_none', PER_TRIAL / 2)
|
|
|
|
benchmark_find_one('find_one (medium, no index)', 'medium_none', PER_TRIAL / 2)
|
|
|
|
benchmark_find_one('find_one (large, no index)', 'large_none', PER_TRIAL / 2)
|
|
|
|
|
|
|
|
|
2009-11-16 23:34:25 +00:00
|
|
|
|
2009-11-12 22:22:24 +00:00
|
|
|
benchmark_find_one('find_one (small, indexed)', 'small_indexed', PER_TRIAL / 2)
|
|
|
|
benchmark_find_one('find_one (medium, indexed)', 'medium_indexed', PER_TRIAL / 2)
|
|
|
|
benchmark_find_one('find_one (large, indexed)', 'large_indexed', PER_TRIAL / 2)
|
|
|
|
|
2009-11-16 23:34:25 +00:00
|
|
|
def benchmark_find_all(desc, coll_name, data)
|
2010-01-07 19:39:02 +00:00
|
|
|
benchmark(desc, PER_TRIAL, coll_name, data) do |coll, i|
|
2009-11-16 23:34:25 +00:00
|
|
|
coll.find({'x' => data}).each {|o| o}
|
2009-11-12 22:22:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-07 19:39:02 +00:00
|
|
|
benchmark_find_all('find (small, no index)', 'small_none', PER_TRIAL / 2)
|
|
|
|
benchmark_find_all('find (medium, no index)', 'medium_none', PER_TRIAL / 2)
|
|
|
|
benchmark_find_all('find (large, no index)', 'large_none', PER_TRIAL / 2)
|
2009-11-16 23:34:25 +00:00
|
|
|
|
2010-01-07 19:39:02 +00:00
|
|
|
benchmark_find_all('find (small, indexed)', 'small_indexed', PER_TRIAL / 2)
|
|
|
|
benchmark_find_all('find (medium, indexed)', 'medium_indexed', PER_TRIAL / 2)
|
|
|
|
benchmark_find_all('find (large, indexed)', 'large_indexed', PER_TRIAL / 2)
|
2009-11-12 22:22:24 +00:00
|
|
|
|
2010-01-07 19:39:02 +00:00
|
|
|
benchmark_find_all('find_range (small, indexed)', 'small_indexed',
|
2009-11-12 22:22:24 +00:00
|
|
|
{"$gt" => PER_TRIAL / 2, "$lt" => PER_TRIAL / 2 + BATCH_SIZE})
|
2010-01-07 19:39:02 +00:00
|
|
|
benchmark_find_all('find_range (medium, indexed)', 'medium_indexed',
|
2009-11-12 22:22:24 +00:00
|
|
|
{"$gt" => PER_TRIAL / 2, "$lt" => PER_TRIAL / 2 + BATCH_SIZE})
|
2010-01-07 19:39:02 +00:00
|
|
|
benchmark_find_all('find_range (large, indexed)', 'large_indexed',
|
2009-11-12 22:22:24 +00:00
|
|
|
{"$gt" => PER_TRIAL / 2, "$lt" => PER_TRIAL / 2 + BATCH_SIZE})
|