Skip to content

Instantly share code, notes, and snippets.

@kamipo
Created June 13, 2020 05:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kamipo/4002c96a02859d8fe6503e26d7be4ad8 to your computer and use it in GitHub Desktop.
Save kamipo/4002c96a02859d8fe6503e26d7be4ad8 to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "rails", github: "rails/rails"
gem "sqlite3"
gem "benchmark-ips"
gem "benchmark-memory"
end
require "active_record"
require "minitest/autorun"
require "logger"
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Schema.define do
create_table :models, force: true do |t|
t.string :col1
t.string :col2
t.string :col3
t.string :col4
t.string :col5
t.string :col6
t.string :col7
t.string :col8
t.string :col9
end
end
ActiveRecord::Base.immutable_strings_by_default = true
class Model < ActiveRecord::Base; end
ENV["RECORDS"] ||= "10000"
Model.transaction do
ENV["RECORDS"].to_i.times do |i|
Model.create!(
col1: "hello, world!",
col2: "hello, world!",
col3: "hello, world!",
col4: "hello, world!",
col5: "hello, world!",
col6: "hello, world!",
col7: "hello, world!",
col8: "hello, world!",
col9: "hello, world!"
)
end
end
benchmark = -> x {
x.report("attribute access") do
records = Model.find_by_sql("SELECT * FROM models")
records.each do |record|
record.id
record.col1
record.col2
record.col3
record.col4
record.col5
record.col6
record.col7
record.col8
record.col9
end
end
}
puts "IPS"
Benchmark.ips(&benchmark)
puts "MEMORY"
Benchmark.memory(&benchmark)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment