Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'rubygems'
- require 'active_record'
- require 'benchmark'
- # tweak to do immediate type translation
- class ActiveRecord::ConnectionAdapters::SQLite3Adapter
- # class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
- attr_reader :connection
- end
- ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => 'perf_ar.db'
- # ActiveRecord::Base.establish_connection(:adapter => 'postgresql',
- # :host => 'localhost', :port => 5432, :encoding => 'UTF8',
- # :username => 'rails_user', :password => 'rails', :database => 'new_blog_dev')
- ActiveRecord::Base.connection.connection.type_translation = true
- # set up ActiveRecord
- class ArItem < ActiveRecord::Base
- set_table_name 'items'
- end
- # created schema and data
- class NewItem < ActiveRecord::Migration
- def self.up
- create_table :items do |t|
- t.column :name, :string
- t.column :description, :text
- t.column :active, :boolean
- t.column :created_at, :datetime
- end
- end
- def self.down
- drop_table :items
- end
- end
- NewItem.up
- 1000.times do |i|
- ArItem.create(:name => "record_#{i}", :description => "test record", :active => i.remainder(3).zero?)
- end
- # run benchmarks
- Benchmark.bmbm do |x|
- x.report('active_record single-thread') do
- 100.times do
- ArItem.find(:all, :conditions => ["active = ?", false])
- end
- end
- x.report('active_record threaded') do
- ActiveRecord::Base.allow_concurrency = true
- threads = []
- 10.times do
- t = Thread.new do
- 10.times do
- ArItem.find(:all, :conditions => ["active = ?", false])
- end
- end
- threads.push(t)
- end
- threads.each { |t| t.join }
- end
- end
- NewItem.down
Add Comment
Please, Sign In to add comment