Skip to content

Instantly share code, notes, and snippets.

@coorasse
Last active January 8, 2026 08:42
Show Gist options
  • Select an option

  • Save coorasse/ba4c83f5e42fff7766ef5f4a52e0928e to your computer and use it in GitHub Desktop.

Select an option

Save coorasse/ba4c83f5e42fff7766ef5f4a52e0928e to your computer and use it in GitHub Desktop.
Rails machine performance
require 'benchmark'
results = {
timestamp: Time.now,
hostname: `hostname`.strip,
cpu_info: `cat /proc/cpuinfo 2>/dev/null | grep "model name" | head -1`.strip,
}
# CPU benchmark - integer operations
results[:cpu_integer] = Benchmark.measure do
sum = 0
10_000_000.times { |i| sum += i }
end.real
# CPU benchmark - floating point
results[:cpu_float] = Benchmark.measure do
result = 1.0
1_000_000.times { |i| result = Math.sqrt(result + i) }
end.real
# Memory allocation
results[:memory_alloc] = Benchmark.measure do
arrays = []
1000.times { arrays << Array.new(10_000) { rand } }
end.real
# String operations (common in web apps)
results[:string_ops] = Benchmark.measure do
100_000.times { "hello world".upcase.reverse.downcase }
end.real
# Database query (if you have a model)
if defined?(User)
results[:db_query] = Benchmark.measure do
100.times { User.limit(10).to_a }
end.real
end
# Calculate composite score (lower is better)
score = (results[:cpu_integer] + results[:cpu_float] +
results[:memory_alloc] + results[:string_ops]) * 1000
results[:composite_score] = score.round(2)
puts "\n" + "="*50
puts "PERFORMANCE BENCHMARK RESULTS"
puts "="*50
puts "Timestamp: #{results[:timestamp]}"
puts "Host: #{results[:hostname]}"
puts "\nCPU Integer Ops: #{results[:cpu_integer].round(3)}s"
puts "CPU Float Ops: #{results[:cpu_float].round(3)}s"
puts "Memory Allocation: #{results[:memory_alloc].round(3)}s"
puts "String Operations: #{results[:string_ops].round(3)}s"
puts "DB Query: #{results[:db_query].round(3)}s" if results[:db_query]
puts "\nCOMPOSITE SCORE: #{results[:composite_score]}"
puts "(Lower is better - run multiple times and average)"
puts "="*50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment