Guest User

Untitled

a guest
Mar 10th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. require File.expand_path(File.dirname(__FILE__) + '/../../config/environment')
  4. require 'benchmark'
  5. module Database
  6. PERFORMANCE = {
  7. :database => 'benchmark',
  8. :adapter => 'postgresql',
  9. :host => 'localhost',
  10. :username => 'user',
  11. :password => 'xxxxxxxxxxxx',
  12. :encoding => 'utf8'
  13. }
  14. end
  15.  
  16. ActiveRecord::Base.establish_connection Database::PERFORMANCE
  17.  
  18. module App
  19. class CreatePerfomanceDB < ActiveRecord::Migration
  20. def self.up
  21. create_table :posts, :force => true do |t|
  22. t.text :data
  23. end
  24.  
  25. 100.times do
  26. Post.create(:data => Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join ))
  27. end
  28.  
  29. # Join
  30. create_table :posts_balls, :force => true do |t|
  31. t.integer :post_id, :ball_id
  32. end
  33. create_table :balls, :force => true do |t|
  34. t.text :data
  35. end
  36. add_index :posts_balls, :post_id
  37. add_index :posts_balls, :ball_id
  38.  
  39. 100.times do
  40. Ball.create(:data => Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join ))
  41. end
  42.  
  43. # HBTM
  44. create_table :cubes_posts, :id => false, :force => true do |t|
  45. t.integer :post_id, :cube_id
  46. end
  47. create_table :cubes, :force => true do |t|
  48. t.text :data
  49. end
  50. add_index :cubes_posts, :post_id
  51. add_index :cubes_posts, :cube_id
  52.  
  53. 100.times do
  54. Cube.create(:data => Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join ))
  55. end
  56. end
  57.  
  58. def self.down
  59. drop_table :posts
  60. drop_table :posts_balls
  61. drop_table :cubes_posts
  62. drop_table :balls
  63. drop_table :cubes
  64. end
  65. end
  66. class Post < ActiveRecord::Base
  67. # Join
  68. has_many :post_balls
  69. has_many :balls, :through => :post_balls
  70. #HBTM
  71. has_and_belongs_to_many :cubes
  72. end
  73.  
  74. class PostBall < ActiveRecord::Base
  75. set_table_name 'posts_balls'
  76. belongs_to :post
  77. belongs_to :ball
  78. end
  79.  
  80. class Cube < ActiveRecord::Base
  81. has_and_belongs_to_many :posts
  82. end
  83.  
  84.  
  85. class Ball < ActiveRecord::Base
  86. has_many :post_balls
  87. has_many :posts, :through => :post_balls
  88. end
  89.  
  90. def self.adding
  91. puts "========= ADDING ========="
  92. posts = Post.find(:all)
  93. balls_array = Ball.find(:all)
  94. cubes_array = Cube.find(:all)
  95. Benchmark.bm do |rep|
  96. rep.report("Join method") do
  97. for post in posts
  98. balls_array.each{|ball| post.balls << ball}
  99. end
  100. end
  101. rep.report("HBTM method") do
  102. for post in posts
  103. cubes_array.each{|cube| post.cubes << cube}
  104. end
  105. end
  106. end
  107. end
  108.  
  109. def self.adding_by_groups
  110. puts "========= ADDING BY GROUPS========="
  111. posts = Post.find(:all)
  112. balls_array = Ball.find(:all)
  113. cubes_array = Cube.find(:all)
  114. Benchmark.bm do |rep|
  115. rep.report("Join method") do
  116. for post in posts
  117. balls_array.in_groups_of(10){|b| post.balls << b}
  118. end
  119. end
  120. rep.report("HBTM method") do
  121. for post in posts
  122. cubes_array.in_groups_of(10){|c| post.cubes << c}
  123. end
  124. end
  125. end
  126. end
  127.  
  128. def self.finding
  129. puts "========= FINDING ========="
  130. posts = Post.find(:all)
  131. Benchmark.bm do |rep|
  132. rep.report("Join method") do
  133. 10.times do
  134. posts.each{|post| post.balls :all, :group_by => :ball_id}
  135. end
  136. end
  137. rep.report("HBTM method") do
  138. 10.times do
  139. posts.each{|post| post.cubes :all, :group_by => :cube_id}
  140. end
  141. end
  142. end
  143. end
  144.  
  145. def self.finding_back
  146. puts "========= FINDING BACK ========="
  147. balls_array = Ball.find(:all)
  148. cubes_array = Cube.find(:all)
  149. Benchmark.bm do |rep|
  150. rep.report("Join method") do
  151. 10.times do
  152. balls_array.each{|b| b.posts :all, :group_by => :post_id}
  153. end
  154. end
  155. rep.report("HBTM method") do
  156. 10.times do
  157. cubes_array.each{|c| c.posts :all, :group_by => :post_id}
  158. end
  159. end
  160. end
  161. end
  162.  
  163. end
  164.  
  165. App::CreatePerfomanceDB.up
  166. App.adding
  167. App.adding_by_groups
  168. App.finding
  169. App.finding_back
Add Comment
Please, Sign In to add comment