Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- require File.expand_path(File.dirname(__FILE__) + '/../../config/environment')
- require 'benchmark'
- module Database
- PERFORMANCE = {
- :database => 'benchmark',
- :adapter => 'postgresql',
- :host => 'localhost',
- :username => 'user',
- :password => 'xxxxxxxxxxxx',
- :encoding => 'utf8'
- }
- end
- ActiveRecord::Base.establish_connection Database::PERFORMANCE
- module App
- class CreatePerfomanceDB < ActiveRecord::Migration
- def self.up
- create_table :posts, :force => true do |t|
- t.text :data
- end
- 100.times do
- Post.create(:data => Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join ))
- end
- # Join
- create_table :posts_balls, :force => true do |t|
- t.integer :post_id, :ball_id
- end
- create_table :balls, :force => true do |t|
- t.text :data
- end
- add_index :posts_balls, :post_id
- add_index :posts_balls, :ball_id
- 100.times do
- Ball.create(:data => Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join ))
- end
- # HBTM
- create_table :cubes_posts, :id => false, :force => true do |t|
- t.integer :post_id, :cube_id
- end
- create_table :cubes, :force => true do |t|
- t.text :data
- end
- add_index :cubes_posts, :post_id
- add_index :cubes_posts, :cube_id
- 100.times do
- Cube.create(:data => Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join ))
- end
- end
- def self.down
- drop_table :posts
- drop_table :posts_balls
- drop_table :cubes_posts
- drop_table :balls
- drop_table :cubes
- end
- end
- class Post < ActiveRecord::Base
- # Join
- has_many :post_balls
- has_many :balls, :through => :post_balls
- #HBTM
- has_and_belongs_to_many :cubes
- end
- class PostBall < ActiveRecord::Base
- set_table_name 'posts_balls'
- belongs_to :post
- belongs_to :ball
- end
- class Cube < ActiveRecord::Base
- has_and_belongs_to_many :posts
- end
- class Ball < ActiveRecord::Base
- has_many :post_balls
- has_many :posts, :through => :post_balls
- end
- def self.adding
- puts "========= ADDING ========="
- posts = Post.find(:all)
- balls_array = Ball.find(:all)
- cubes_array = Cube.find(:all)
- Benchmark.bm do |rep|
- rep.report("Join method") do
- for post in posts
- balls_array.each{|ball| post.balls << ball}
- end
- end
- rep.report("HBTM method") do
- for post in posts
- cubes_array.each{|cube| post.cubes << cube}
- end
- end
- end
- end
- def self.adding_by_groups
- puts "========= ADDING BY GROUPS========="
- posts = Post.find(:all)
- balls_array = Ball.find(:all)
- cubes_array = Cube.find(:all)
- Benchmark.bm do |rep|
- rep.report("Join method") do
- for post in posts
- balls_array.in_groups_of(10){|b| post.balls << b}
- end
- end
- rep.report("HBTM method") do
- for post in posts
- cubes_array.in_groups_of(10){|c| post.cubes << c}
- end
- end
- end
- end
- def self.finding
- puts "========= FINDING ========="
- posts = Post.find(:all)
- Benchmark.bm do |rep|
- rep.report("Join method") do
- 10.times do
- posts.each{|post| post.balls :all, :group_by => :ball_id}
- end
- end
- rep.report("HBTM method") do
- 10.times do
- posts.each{|post| post.cubes :all, :group_by => :cube_id}
- end
- end
- end
- end
- def self.finding_back
- puts "========= FINDING BACK ========="
- balls_array = Ball.find(:all)
- cubes_array = Cube.find(:all)
- Benchmark.bm do |rep|
- rep.report("Join method") do
- 10.times do
- balls_array.each{|b| b.posts :all, :group_by => :post_id}
- end
- end
- rep.report("HBTM method") do
- 10.times do
- cubes_array.each{|c| c.posts :all, :group_by => :post_id}
- end
- end
- end
- end
- end
- App::CreatePerfomanceDB.up
- App.adding
- App.adding_by_groups
- App.finding
- App.finding_back
Add Comment
Please, Sign In to add comment