Skip to content

Instantly share code, notes, and snippets.

@nasospsa
Last active February 21, 2017 17:02
Show Gist options
  • Save nasospsa/f7fadd6c2f3510b420525f960dc12ddd to your computer and use it in GitHub Desktop.
Save nasospsa/f7fadd6c2f3510b420525f960dc12ddd to your computer and use it in GitHub Desktop.
Rails script benchmarking
def start_script
puts "starting script..."
# Remove the X to enable the parameters for tuning.
# These are the default values as of Ruby 2.2.0.
@child = spawn(<<-EOC.split.join(" "))
XRUBY_GC_HEAP_FREE_SLOTS=4096
XRUBY_GC_HEAP_INIT_SLOTS=10000
XRUBY_GC_HEAP_GROWTH_FACTOR=1.8
XRUBY_GC_HEAP_GROWTH_MAX_SLOTS=0
XRUBY_GC_HEAP_OLDOBJECT_LIMIT_FACTOR=2.0
XRUBY_GC_MALLOC_LIMIT=16777216
XRUBY_GC_MALLOC_LIMIT_MAX=33554432
XRUBY_GC_MALLOC_LIMIT_GROWTH_FACTOR=1.4
XRUBY_GC_OLDMALLOC_LIMIT=16777216
XRUBY_GC_OLDMALLOC_LIMIT_MAX=134217728
XRUBY_GC_OLDMALLOC_LIMIT_GROWTH_FACTOR=1.2
rails runner scripts/rbup.rb & echo $! >> tmp/pids/rbup.pid
EOC
sleep 0.1 until alive?
end
def alive?
lines = `ps -p #{server_pid} | wc -l`.to_i
lines == 2
end
def stop_script
puts "stoping script..."
Process.kill :KILL, server_pid if server_pid && alive?
Process.wait @child
delete_pid
end
def delete_pid
`if test -f tmp/pids/rbup.pid; then rm tmp/pids/rbup.pid; fi`
end
def server_pid
`cat tmp/pids/rbup.pid`.to_i
end
def memory_size_mb
(`ps -o rss= -p #{server_pid}`.to_i * 1024).to_f / 2**20
end
stop_script if @child
start_script
seconds = 0
used_mb = 0
max_mem = 0
while alive?
used_mb = memory_size_mb
max_mem = [max_mem, used_mb].max
puts "#{seconds}sec - #{'%.2f' % used_mb.round} MB"
seconds += 1
sleep 1
end
stop_script
puts "Total Time: #{seconds}sec"
puts "Max Memory: #{'%.2f' % max_mem} MB"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment