Created
June 11, 2019 20:32
-
-
Save Inversion-des/e7e427c3a9bacb53d97ce78f2765cd43 to your computer and use it in GitHub Desktop.
tests for mongo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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