Skip to content

Instantly share code, notes, and snippets.

@cosmo0920
Created October 27, 2015 04:17
Show Gist options
  • Save cosmo0920/b33c96e071c5c6923304 to your computer and use it in GitHub Desktop.
Save cosmo0920/b33c96e071c5c6923304 to your computer and use it in GitHub Desktop.
Mongo Ruby Driver benchmark
require 'mongo'
require 'benchmark'
# setup
Mongo::Logger.level = Logger::WARN
options = {}
options[:database] = 'latest_bench'
client = Mongo::Client.new(["localhost:27017"], options)
puts "insert one by one"
# insert one by one
Benchmark.bm do |x|
x.report { 10000.times { |i| client['test_collection'].insert_one("i#{i}" => i) } }
end
# bulk insert
records = []
data = 10000.times do |i|
records << {"i#{i}" => i}
end
puts "bulk insert"
Benchmark.bm do |x|
x.report { client['test_collection'].insert_many(records) }
end
# teardown
client['test_collection'].drop
require 'mongo'
require 'benchmark'
# setup
connection = Mongo::Connection.new("localhost", "27017")
db = connection.db('legacy_bench')
collection = db.collection('test_collection')
puts "insert one by one (legacy)"
# insert one by one
Benchmark.bm do |x|
x.report { 10000.times { |i| collection.insert("i#{i}" => i) } }
end
# bulk insert
records = []
data = 10000.times do |i|
records << {"i#{i}" => i}
end
puts "bulk insert (legacy)"
Benchmark.bm do |x|
x.report { collection.insert(records) }
end
# teardown
collection.drop
connection.drop_database('legacy_bench')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment