Guest User

Untitled

a guest
Apr 15th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. #!/usr/bin/env ruby -KU
  2.  
  3. TIMES = (ENV['N'] || 10000).to_i
  4.  
  5. require 'rubygems'
  6. gem 'addressable', '~>2.0'
  7. gem 'faker', '~>0.3.1'
  8. gem 'rbench', '~>0.2.3'
  9. require 'addressable/uri'
  10. require 'faker'
  11. require 'rbench'
  12.  
  13. __DIR__ = File.dirname(__FILE__)
  14. $:.unshift "#{__DIR__}/../lib"
  15. require 'active_record'
  16.  
  17. conn = { :adapter => 'mysql',
  18. :database => 'activerecord_unittest',
  19. :username => 'rails', :password => '',
  20. :encoding => 'utf8' }
  21.  
  22. conn[:socket] = Pathname.glob(%w[
  23. /opt/local/var/run/mysql5/mysqld.sock
  24. /tmp/mysqld.sock
  25. /tmp/mysql.sock
  26. /var/mysql/mysql.sock
  27. /var/run/mysqld/mysqld.sock
  28. ]).find { |path| path.socket? }
  29.  
  30. ActiveRecord::Base.establish_connection(conn)
  31.  
  32. class User < ActiveRecord::Base
  33. connection.create_table :users, :force => true do |t|
  34. t.string :name, :email
  35. t.timestamps
  36. end
  37.  
  38. has_many :exhibits
  39. end
  40.  
  41. class Exhibit < ActiveRecord::Base
  42. connection.create_table :exhibits, :force => true do |t|
  43. t.belongs_to :user
  44. t.string :name
  45. t.text :notes
  46. t.timestamps
  47. end
  48.  
  49. belongs_to :user
  50.  
  51. def look; attributes end
  52. def feel; look; user.name end
  53.  
  54. def self.look(exhibits) exhibits.each { |e| e.look } end
  55. def self.feel(exhibits) exhibits.each { |e| e.feel } end
  56. end
  57.  
  58. sqlfile = "#{__DIR__}/performance.sql"
  59.  
  60. if File.exists?(sqlfile)
  61. mysql_bin = %w[mysql mysql5].select { |bin| `which #{bin}`.length > 0 }
  62. `#{mysql_bin} -u #{conn[:username]} #{"-p#{conn[:password]}" unless conn[:password].blank?} #{conn[:database]} < #{sqlfile}`
  63. else
  64. puts 'Generating data...'
  65.  
  66. # pre-compute the insert statements and fake data compilation,
  67. # so the benchmarks below show the actual runtime for the execute
  68. # method, minus the setup steps
  69.  
  70. # Using the same paragraph for all exhibits because it is very slow
  71. # to generate unique paragraphs for all exhibits.
  72. notes = Faker::Lorem.paragraphs.join($/)
  73. today = Date.today
  74.  
  75. puts 'Inserting 10,000 users and exhibits...'
  76. 10_000.times do
  77. user = User.create(
  78. :created_at => today,
  79. :name => Faker::Name.name,
  80. :email => Faker::Internet.email
  81. )
  82.  
  83. Exhibit.create(
  84. :created_at => today,
  85. :name => Faker::Company.name,
  86. :user => user,
  87. :notes => notes
  88. )
  89. end
  90.  
  91. mysqldump_bin = %w[mysqldump mysqldump5].select { |bin| `which #{bin}`.length > 0 }
  92. `#{mysqldump_bin} -u #{conn[:username]} #{"-p#{conn[:password]}" unless conn[:password].blank?} #{conn[:database]} exhibits users > #{sqlfile}`
  93. end
  94.  
  95.  
  96. def find_model
  97. ar_obj = Exhibit.find(1)
  98.  
  99. ar_obj.id
  100. end
  101.  
  102. def instantiate_model
  103. Exhibit.new
  104. end
  105.  
  106. def setting_attributes
  107. attrs = { :name => 'sam' }
  108. Exhibit.new(attrs)
  109. end
  110.  
  111. def model_first
  112. Exhibit.first.look
  113. end
  114.  
  115. def model_all_limit_100
  116. Exhibit.look Exhibit.all(:limit => 100)
  117. end
  118.  
  119. def model_all_limit_100_with_relationship
  120. Exhibit.feel Exhibit.all(:limit => 100, :include => :user)
  121. end
  122.  
  123. def model_all_limit_10000
  124. Exhibit.look Exhibit.all(:limit => 10000)
  125. end
  126.  
  127. def model_create
  128. Exhibit.create({
  129. :name => Faker::Company.name,
  130. :notes => Faker::Lorem.paragraphs.join($/),
  131. :created_at => Date.today
  132. })
  133. end
  134.  
  135. def model_attributes
  136. attrs_first = { :name => 'sam' }
  137. attrs_second = { :name => 'tom' }
  138. exhibit = Exhibit.new(attrs_first); exhibit.attributes = attrs_second
  139. end
  140.  
  141. def model_update
  142. Exhibit.first.update_attributes(:name => 'bob')
  143. end
  144.  
  145. def model_destroy
  146. Exhibit.first.destroy
  147. end
  148.  
  149.  
  150. require "ruby-prof"
  151. RubyProf.start
  152.  
  153. find_model
  154. instantiate_model
  155. setting_attributes
  156. model_first
  157. model_all_limit_100
  158. model_all_limit_100_with_relationship
  159. model_all_limit_10000
  160. model_create
  161. model_attributes
  162. model_update
  163. model_destroy
  164.  
  165. result = RubyProf.stop
  166. printer = RubyProf::CallStackPrinter.new(result)
  167. printer.print(File.open("output.html", "w"))
  168.  
  169. ActiveRecord::Migration.drop_table "exhibits"
  170. ActiveRecord::Migration.drop_table "users"
Add Comment
Please, Sign In to add comment