Created
June 12, 2019 19:23
-
-
Save Inversion-des/18cc3a25f01450f98cbfdd66dc0d735d to your computer and use it in GitHub Desktop.
Pry session performance impact test
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
puts 'start' | |
require 'mongo' | |
Mongo::Logger.level = Logger::FATAL | |
$client = Mongo::Client.new('mongodb://127.0.0.1:15000/test', :connect => :direct, min_pool_size:1) | |
@item_tyme_by_title = {} | |
def watch(title:) | |
ttime = nil | |
_time_start = Time.now | |
yield | |
ttime = Time.now - _time_start # total time | |
item_time = ttime/@n*1000 | |
# set base | |
@item_tyme_by_title[title] ||= item_time | |
k = item_time / @item_tyme_by_title[title] | |
dk = k-1 | |
puts "#{'%-35s' % title} -- total: #{'%7.3f' % ttime} s, item time = #{'%7.3f' % item_time} ms (#{'%5.2f' % k}x) (#{'+' if dk >0}#{'%4.2f' % dk})" | |
end | |
def tests | |
collection = $client[:people] | |
collection.delete_many | |
@n = 500 | |
watch(title:"insert #{@n} (w:0)") do | |
@n.times do |n| | |
doc = {_id:n, 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 |n| | |
doc = {_id:n, 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 | |
res = nil | |
watch(title:"#{@n} times find first (full)") do | |
@n.times do | |
res = collection.find.first.to_s | |
end | |
end | |
watch(title:"#{@n} times find first (full by id)") do | |
@n.times do | |
res = collection.find({:_id=>3}).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 | |
@n = 1 | |
2.times do |n| | |
sleep 0.05 | |
watch(title:"single find first (full) (#{n})") do | |
@n.times do | |
collection.find.first.to_s | |
end | |
end | |
end | |
end | |
puts '1.1) Normal' | |
tests | |
puts | |
puts '1.2) Normal again' | |
tests | |
puts | |
puts | |
th = Thread.new do | |
puts '- Starting Pry...' | |
require 'pry' | |
Pry.config.color = false | |
Pry.hooks.add_hook(:before_session, 'show ex title') do |output, binding, pry| | |
$pry = pry | |
end | |
binding.pry | |
puts '- Pry finished' | |
end | |
sleep 3 | |
puts | |
puts | |
puts '2) With active Pry session' | |
tests | |
puts | |
puts 'closing pry session' | |
$pry.eval 'exit' | |
sleep 1 | |
puts | |
puts | |
puts '3) After closed Pry session' | |
tests | |
puts | |
puts '- done -' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment