Skip to content

Instantly share code, notes, and snippets.

@smook1980
Last active August 29, 2015 14:05
Show Gist options
  • Select an option

  • Save smook1980/b43ec2bdfaa9ef475b39 to your computer and use it in GitHub Desktop.

Select an option

Save smook1980/b43ec2bdfaa9ef475b39 to your computer and use it in GitHub Desktop.
Testing MRI threads with ActiveRecord's sql server driver. Shows the sql server driver blocks on IO under MRI.
#! /usr/bin/env ruby
# Adapted from slide 5 at http://www.slideshare.net/igrigorik/no-callbacks-no-threads-railsconf-2010
require 'bundler/setup'
require 'active_record'
require 'tiny_tds'
require 'active_record/connection_adapters/sqlserver_adapter'
ActiveRecord::Base.establish_connection(
:adapter => 'sqlserver',
:host => 'localdb',
:database => 'some_datarbase',
:username => 'sa',
:password => 'youknowit',
:timeout => 1500,
:pool => 10
)
stime = Time.now
threads = []
10.times do |n|
threads << Thread.new do
ActiveRecord::Base.connection_pool.with_connection do |conn|
# Block connection for one second.
res = conn.execute("WAITFOR DELAY '00:00:10'")
end
end
end
threads.each do |t|
begin
t.join
rescue Exception => e
puts "Exception in thread: #{e.class}"
end
end
puts "\n\nElapsed real time: #{Time.now - stime} seconds"
# On my computer:
#
# * mysql: 10.19 seconds, as expected for broken mysql adapter that holds onto
# ruby GIL
#
# * mysql2 with either ruby 1.8.7 OR ruby 1.9.2: 2.1 seconds
# (still surprisingly more than the 1s and change expected. still a lot of
# overhead from context switching and/or connection pool checkout?
# But not as disastrous as 'mysql'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment