-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.rb
83 lines (66 loc) · 1.54 KB
/
run.rb
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
require 'bundler/setup'
require 'active_record'
require 'memory_profiler'
require 'yaml'
# CONFIG
def db_config
conf = File.expand_path('../config/database.yml', __FILE__)
YAML.load_file(conf)['development']
end
def schemas
Integer(ENV['SCHEMAS'] || 50)
end
def tables
Integer(ENV['TABLES'] || 30)
end
def env
ENV['RACK_ENV'] || 'production'
end
# DB SETUP
def drop_db
%x{ dropdb --if-exists #{db_config['database']} -U#{db_config['username']} }
end
def create_db
%x{ createdb -E UTF8 #{db_config['database']} -U#{db_config['username']} } rescue nil
end
def setup_db
puts "setting up db"
drop_db
create_db
end
# SCHEMA POPULATION
def create_schemas
puts "creating schemas"
pool = ActiveRecord::Base.establish_connection(db_config)
connection = ActiveRecord::Base.connection
schemas.times do |x|
schema_name = "schema_#{x}"
connection.execute <<-SQL
CREATE SCHEMA IF NOT EXISTS "#{schema_name}";
SQL
tables.times do |y|
connection.create_table "#{schema_name}.foos_#{y}" do |t|
t.timestamps null: false
t.string :name
t.integer :some_count
t.text :big_text
t.json :a_bunch_of_json
end
end
end
ActiveRecord::Base.remove_connection(ActiveRecord::Base)
end
# PROFILING
def run_profiler
puts "profiling"
MemoryProfiler.report {
ActiveRecord::Base.establish_connection(db_config)
ActiveRecord::Base.configurations = {
env => ActiveRecord::Base.connection.pool.spec.config
}
}.pretty_print
end
# RUN
setup_db
create_schemas
run_profiler