Skip to content

Instantly share code, notes, and snippets.

@kapso
Created December 12, 2012 19:19
Show Gist options
  • Select an option

  • Save kapso/4270720 to your computer and use it in GitHub Desktop.

Select an option

Save kapso/4270720 to your computer and use it in GitHub Desktop.
def connection(options = { pool: true })
if options[:pool]
Mongo::Connection.new("localhost", 27017, pool_size: 15, pool_timeout: 5)
else
Mongo::Connection.new("localhost", 27017)
end
end
def do_write(counter = nil)
if counter
connection.db("pool_test_db", pool: true).collection("products").insert({ _id: "D#{counter}", name: "Nikon D#{counter}" }, safe: true)
else
10.times do |i|
connection.db("pool_test_db").collection("products").insert({ _id: "D#{i}", name: "Nikon D#{i}" }, safe: true)
end
end
end
def clean
connection.db("pool_test_db").collection("products").drop
end
def do_read
doc = connection.db("pool_test_db").collection("products").find(_id: "D1").to_a.first
puts "Doc -> #{doc.inspect}"
end
def read_all
doc_size = 4000
sleep 2
doc_size.times do |i|
doc = connection.db("pool_test_db").collection("products").find(_id: "D#{i}").to_a.first
puts "Trying to read doc #{i}. Result Doc is -> #{doc.inspect}"
end
end
def thread_write
pool = ThreadPool.new(40)
puts "Thread pool created..."
sleep 5
puts "Started..."
doc_size = 4000
doc_size.times do |i|
# Thread.new { do_write(i) }
pool.process { do_write(i); puts "Written #{i}\n" }
end
puts "Completed!!"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment