Skip to content

Instantly share code, notes, and snippets.

@prepor
Created March 2, 2012 08:02
Show Gist options
  • Save prepor/1956665 to your computer and use it in GitHub Desktop.
Save prepor/1956665 to your computer and use it in GitHub Desktop.
# A sample Gemfile
source "http://rubygems.org"
gem "em-synchrony", git: "https://github.com/igrigorik/em-synchrony.git"
gem "mysql2", ["= 0.3.11"]
gem "activerecord"
GIT
remote: https://github.com/igrigorik/em-synchrony.git
revision: 0d176e900c589d1a8a04429bc91594cd842c3ec7
specs:
em-synchrony (1.0.0)
eventmachine (>= 1.0.0.beta.1)
GEM
remote: http://rubygems.org/
specs:
activemodel (3.2.2)
activesupport (= 3.2.2)
builder (~> 3.0.0)
activerecord (3.2.2)
activemodel (= 3.2.2)
activesupport (= 3.2.2)
arel (~> 3.0.2)
tzinfo (~> 0.3.29)
activesupport (3.2.2)
i18n (~> 0.6)
multi_json (~> 1.0)
arel (3.0.2)
builder (3.0.0)
eventmachine (1.0.0.beta.4)
i18n (0.6.0)
multi_json (1.1.0)
mysql2 (0.3.11)
tzinfo (0.3.31)
PLATFORMS
ruby
DEPENDENCIES
activerecord
em-synchrony!
mysql2 (= 0.3.11)
require 'bundler'
Bundler.setup
require 'em-synchrony'
require 'active_record'
#require 'rack/fiber_pool'
ActiveRecord::Base.establish_connection({:host => 'localhost',
:port => 3306,
:adapter => "em_mysql2",
:database => 'widgets',
:username => 'root',
:password => '',
:encoding => 'utf8',
:pool => 5})
class Widget < ActiveRecord::Base
end
EM.synchrony do
timer = Time.now
fibers = []
1.upto(10) do
fibers << Fiber.new do
time = Benchmark.realtime {
1.upto(10000) do |index|
Widget.find_by_id(index)
end
}
puts "#{time*1000} milliseconds"
end
end
fibers.each { |t| t.resume }
Fiber.new do
loop do
unless fibers.any?(&:alive?)
puts ""
puts "Real mysql connections: #{ObjectSpace.each_object(Mysql2::EM::Client).count}"
puts "Total time: #{Time.now - timer}"
EM.stop
end
EM::Synchrony.sleep 0.5
end
end.resume
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment