public
Last active

  • Download Gist
.rvmrc
1
rvm use --create 1.9.2@list_benchmark
list_benchmark.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
numbers = ARGV.map(&:to_i)
 
require 'active_record'
require 'ranked-model'
require '../acts_as_list/init' # from github.com/vpereira/acts_as_list
require 'resort'
require 'benchmark'
require 'sqlite3'
require 'logger'
 
ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3',
:database => ':memory:'
)
 
ActiveRecord::Schema.define do
create_table :articles do |t|
t.string :name
 
t.boolean :first
t.references :next
 
t.timestamps
end
 
add_index :articles, :next_id
add_index :articles, :first
 
create_table :ducks do |t|
t.string :name
t.integer :row_order
t.timestamps
end
 
add_index :ducks, :row_order
 
create_table :list_items do |t|
t.string :name
t.integer :position
t.timestamps
end
 
add_index :list_items, :position
end
 
ActiveRecord::Base.logger = Logger.new('benchmark.log')
 
class Duck < ActiveRecord::Base
include RankedModel
ranks :row_order
end
 
class Article < ActiveRecord::Base
resort!
end
 
class ListItem < ActiveRecord::Base
acts_as_list
scope(:ordered, lambda{
order(ListItem.arel_table[:position])
})
end
 
unless numbers.empty?
numbers.each do |number|
 
iterations = (number).to_i
 
puts "\n\nResort with #{number} elements"
Benchmark.bm(20) do |x|
x.report("Create:") {iterations.times { |i| Article.create(:name => "Article #{i}")}}
x.report("Order:") { Article.ordered }
x.report("Prepend:") { iterations.times { Article.last.prepend } }
x.report("Append after:") { iterations.times { Article.last.append_to(Article.first) } }
x.report("Destroy:") { Article.all.each {|article| article.destroy } }
end
 
puts "\n\nRankedModel with #{number} elements"
Benchmark.bm(20) do |x|
x.report("Create:") {iterations.times { |i| Duck.create(:name => "Duck #{i}")}}
x.report("Order:") { Duck.rank(:row_order).all }
x.report("Prepend:") { iterations.times { Duck.last.update_attribute(:row_order_position, :first) } }
x.report("Append after:") { iterations.times { Duck.last.update_attribute(:row_order_position, Duck.first.row_order + 1)} }
x.report("Destroy:") { Duck.all.each {|duck| duck.destroy } }
end
 
puts "\n\nActsAsList with #{number} elements"
Benchmark.bm(20) do |x|
x.report("Create:") {iterations.times { |i| ListItem.create(:name => "ListItem #{i}")}}
x.report("Order:") { ListItem.ordered.all }
x.report("Prepend:") { iterations.times { ListItem.last.move_to_top } }
x.report("Append after:") { iterations.times { ListItem.last.insert_at(ListItem.first.position + 1)} }
x.report("Destroy:") { ListItem.all.each {|item| item.destroy } }
end
 
Duck.destroy_all
Article.destroy_all
ListItem.destroy_all
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.