Skip to content

Instantly share code, notes, and snippets.

@Inversion-des
Created June 11, 2019 20:32
Show Gist options
  • Save Inversion-des/e7e427c3a9bacb53d97ce78f2765cd43 to your computer and use it in GitHub Desktop.
Save Inversion-des/e7e427c3a9bacb53d97ce78f2765cd43 to your computer and use it in GitHub Desktop.
tests for mongo
require 'mongo'
Mongo::Logger.level = Logger::FATAL
client = Mongo::Client.new('mongodb://127.0.0.1:27364/test')
#client = Mongo::Client.new('mongodb://%2Ftmp%2Fmongodb-27364.sock/test')
collection = client[:people]
collection.delete_many
def watch(title:)
ttime = nil
_time_start = Time.now
yield
ttime = Time.now - _time_start # total time
puts "#{'%-35s' % title} -- total: #{'%.3f' % ttime} s, item time = #{'%.3f' % (ttime/@n*1000)} ms"
end
@n = 500
watch(title:"insert #{@n} (w:0)") do
@n.times do
doc = { name: "Steve_#{@n}", hobbies: [ 'hiking', 'tennis', 'fly fishing' ] }
result = collection.with(:write => { :w => 0 }).insert_one(doc)
end
end
collection.delete_many
watch(title:"insert #{@n} (w:1)") do
@n.times do
doc = { name: "Steve_#{@n}", hobbies: [ 'hiking', 'tennis', 'fly fishing' ] }
result = collection.insert_one(doc)
end
end
watch(title:"#{@n} times find all") do
@n.times do
collection.find.to_a.to_s
end
end
watch(title:"#{@n} times find first (full)") do
@n.times do
collection.find.first.to_s
end
end
watch(title:"#{@n} times find first (only name)") do
@n.times do
collection.find({}, projection:{name:1, _id:0}).first
end
end
watch(title:"#{@n} times count()") do
@n.times do
collection.count()
end
end
require 'mongo'
client = Mongo::MongoClient.new 'localhost', 27364
db = client.db 'test'
collection = db[:people]
collection.remove
def watch(title:nil)
ttime = nil
_time_start = Time.now
yield
ttime = Time.now - _time_start # total time
puts "#{'%-35s' % title} -- total: #{'%.3f' % ttime} s, item time = #{'%.3f' % (ttime/@n*1000)} ms"
end
@n = 500
watch(title:"insert #{@n} (w:0)") do
@n.times do
doc = { name: "Steve_#{@n}", hobbies: [ 'hiking', 'tennis', 'fly fishing' ] }
result = collection.insert(doc, { :w => 0 })
end
end
collection.remove
watch(title:"insert #{@n} (w:1)") do
@n.times do
doc = { name: "Steve_#{@n}", hobbies: [ 'hiking', 'tennis', 'fly fishing' ] }
result = collection.insert(doc)
end
end
watch(title:"#{@n} times find all") do
@n.times do
collection.find.to_a.to_s
end
end
watch(title:"#{@n} times find first (full)") do
@n.times do
collection.find_one.to_s
end
end
watch(title:"#{@n} times find first (only name)") do
@n.times do
collection.find_one({}, fields:{name:1, _id:0})
end
end
watch(title:"#{@n} times count()") do
@n.times do
collection.count()
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment