<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:www.refactormycode.com,2007:users474</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/474" rel="self"/>
  <title>Kevin Williams</title>
  <updated>Wed Feb 20 04:38:08 -0800 2008</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code240</id>
    <published>2008-02-20T04:38:08-08:00</published>
    <updated>2012-02-06T06:47:42-08:00</updated>
    <title>[Ruby] DataMapper threaded benchmark</title>
    <content type="html">&lt;p&gt;third of three - slowest of those tested, even though it's thread-safe - why?&lt;/p&gt;

&lt;pre&gt;require 'rubygems'
require 'data_mapper'
require 'benchmark'

DataMapper::Database.setup({ :adapter =&amp;gt; 'sqlite3', :database =&amp;gt; 'perf_dm.db' })

# set up DataMapper
class DmItem
  include DataMapper::Persistence
  set_table_name 'items'
  property :name, :string
  property :description, :text
  property :active, :boolean
  property :created_at, :datetime
end
database.save(DmItem)
# DataMapper::Persistence.auto_migrate!

1000.times do |i|
  x = DmItem.new(:name =&amp;gt; &amp;quot;record_#{i}&amp;quot;, :description =&amp;gt; &amp;quot;test record&amp;quot;, :active =&amp;gt; i.remainder(3).zero?)
  x.save
end

# run benchmarks
Benchmark.bmbm do |x|
  
  x.report('DataMapper single-thread') do
    100.times do
      DmItem.all(:active =&amp;gt; false)
    end
  end
  
  x.report('DataMapper threaded') do
    threads = []
    10.times do
      t = Thread.new do
        10.times do
          DmItem.all(:active =&amp;gt; false)
        end
      end
      threads.push(t)
    end
    threads.each { |t| t.join }
  end
  
end

&lt;/pre&gt;</content>
    <author>
      <name>Kevin Williams</name>
      <email>kevwil@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/240-datamapper-threaded-benchmark" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code239</id>
    <published>2008-02-20T04:35:56-08:00</published>
    <updated>2012-01-20T03:21:44-08:00</updated>
    <title>[Ruby] Sequel threaded benchmark</title>
    <content type="html">&lt;p&gt;second of three, not as fast as ActiveRecord - why?&lt;/p&gt;

&lt;pre&gt;require 'rubygems'
require 'sequel'
require 'benchmark'

# set up Sequel
Sequel::Model.db = DB = Sequel.sqlite('perf_sequel.db')

class SequelItem &amp;lt; Sequel::Model(:items)
  set_schema do
    primary_key :id
    varchar :name
    text :description
    boolean :active
    timestamp :created_at
  end
end

# create schema and populate
SequelItem.create_table!

1000.times do |i|
  DB[:items].insert(:name =&amp;gt; &amp;quot;record_#{i}&amp;quot;, :description =&amp;gt; &amp;quot;test record&amp;quot;, :active =&amp;gt; i.remainder(3).zero?, :created_at =&amp;gt; Time.now)
end

# run benchmarks
Benchmark.bmbm do |x|
  
  x.report('sequel single-thread') do
    100.times do
      SequelItem.where(:active =&amp;gt; false).all
    end
  end
  
  x.report('sequel threaded') do
    threads = []
    10.times do
      t = Thread.new do
        10.times do
          SequelItem.where(:active =&amp;gt; false).all
        end
      end
      threads.push(t)
    end
    threads.each { |t| t.join }
  end
  
end

SequelItem.drop_table

&lt;/pre&gt;</content>
    <author>
      <name>Kevin Williams</name>
      <email>kevwil@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/239-sequel-threaded-benchmark" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code238</id>
    <published>2008-02-20T04:33:28-08:00</published>
    <updated>2012-01-20T14:09:18-08:00</updated>
    <title>[Ruby] ActiveRecord threaded benchmark</title>
    <content type="html">&lt;p&gt;first of three benchmarks, expected AR to be slowest but it was fastest - why?&lt;/p&gt;

&lt;pre&gt;require 'rubygems'
require 'active_record'
require 'benchmark'

ActiveRecord::Base.establish_connection :adapter =&amp;gt; 'sqlite3', :database =&amp;gt; 'perf_ar.db'

# set up ActiveRecord
class ArItem &amp;lt; ActiveRecord::Base
  set_table_name 'items'
end

# created schema and data
class NewItem &amp;lt; 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 =&amp;gt; &amp;quot;record_#{i}&amp;quot;, :description =&amp;gt; &amp;quot;test record&amp;quot;, :active =&amp;gt; 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 =&amp;gt; [&amp;quot;active = ?&amp;quot;, 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 =&amp;gt; [&amp;quot;active = ?&amp;quot;, false])
        end
      end
      threads.push(t)
    end
    threads.each { |t| t.join }
  end
  
end
ActiveRecord::Base.verify_active_connections!
NewItem.down

&lt;/pre&gt;</content>
    <author>
      <name>Kevin Williams</name>
      <email>kevwil@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/238-activerecord-threaded-benchmark" rel="alternate"/>
  </entry>
</feed>

