/
benchmark.rb
41 lines (33 loc) · 952 Bytes
/
benchmark.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
require "harness"
require "securerandom"
Dir.chdir __dir__
use_gemfile
require "active_record"
ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
ActiveRecord::Schema.define do
create_table :posts, force: true do |t|
t.string :title, null: false
t.string :body
t.string :type_name, null: false
t.string :key, null: false
t.integer :upvotes, null: false
t.integer :author_id, null: false
t.timestamps
end
end
class Post < ActiveRecord::Base; end
50000.times {
Post.create!(title: SecureRandom.alphanumeric(30),
type_name: SecureRandom.alphanumeric(10),
key: SecureRandom.alphanumeric(10),
body: SecureRandom.alphanumeric(100),
upvotes: rand(30),
author_id: rand(30))
}
# heat any caches
Post.where(id: 1).first.title
run_benchmark(10) do
1000.times do |i|
Post.where(id: i + 1).first.title
end
end