-
-
Save mixonic/900117 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rvm use --create 1.9.2@list_benchmark |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment