Skip to content

Instantly share code, notes, and snippets.

@larskanis
Created January 26, 2019 18:52
Show Gist options
  • Save larskanis/7755170c6b880e61931a524603b7d336 to your computer and use it in GitHub Desktop.
Save larskanis/7755170c6b880e61931a524603b7d336 to your computer and use it in GitHub Desktop.
Benchmark for native timestamp conversion on PostgreSQL adapter
# frozen_string_literal: true
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'pg', '~> 1.1'
gem 'activerecord', path: '../activerecord'
gem 'benchmark-ips'
end
require 'active_record'
require 'benchmark/ips'
ActiveRecord::Base.establish_connection(
:adapter => "postgresql",
:database => "test_db"
)
pg = ActiveRecord::Base.connection.raw_connection
pg.async_exec <<SQL
drop table if exists topics
SQL
pg.async_exec <<SQL
CREATE TABLE topics (
id integer NOT NULL,
title character varying NOT NULL,
last_posted_at timestamp without time zone,
created_at timestamp without time zone NOT NULL,
updated_at timestamp without time zone NOT NULL
)
SQL
class Topic < ActiveRecord::Base
end
Topic.transaction do
topic = {
}
Topic.columns.each do |c|
topic[c.name.to_sym] = case c.type
when :integer then 1
when :datetime then Time.now
else "HELLO WORLD"
end
end
2000.times do |id|
topic[:id] = id
Topic.create!(topic)
end
end
def top_1000
a = []
Topic.limit(1000).each do |t|
a << t.attributes
end
a
end
Benchmark.ips do |x|
x.report("top_1000") do |i|
while i > 0
top_1000
i -= 1
end
end
end
# rails branch master:
#
# Warming up --------------------------------------
# top_1000 2.000 i/100ms
# Calculating -------------------------------------
# top_1000 22.378 (± 0.0%) i/s - 112.000 in 5.006214s
#
# rails branch native-timestamps:
#
# Warming up --------------------------------------
# top_1000 3.000 i/100ms
# Calculating -------------------------------------
# top_1000 39.373 (± 2.5%) i/s - 198.000 in 5.030601s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment