« Back to Index

Benchmark MRI vs JRuby

View original Gist on GitHub

Benchmark MRI vs JRuby.rb

require "net/http"

num_iterations = 20
num_threads    = 4
total_time     = 0.0

# Try requesting a URL 200 times, on 4 separate threads, 20 times
num_iterations.times do |iter|
  threads = []
  time_start = Time.now

  num_threads.times do |n|
    p "Thread: #{n}"
    threads << Thread.new(n) do |t|
      200.times do |i|
        Net::HTTP.get(URI.parse("http://www.google.com/"))
        p "Iteration: #{i}"
      end
    end
  end

  threads.each { |thread| thread.join }
  p "Done"

  time_end = Time.now
  time_ms = (time_end - time_start) * 1000
  puts "TEST #{iter}: Time elapsed = #{time_ms}ms"
  total_time += time_ms
end

puts "Average completion time: #{total_time/num_iterations}"