Skip to content

Instantly share code, notes, and snippets.

@gmcgibbon
Created May 6, 2018 00:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gmcgibbon/5dcb74d9063c37d9ae24f43665964f7b to your computer and use it in GitHub Desktop.
Save gmcgibbon/5dcb74d9063c37d9ae24f43665964f7b to your computer and use it in GitHub Desktop.
Benchmark ActiveRecord::Associations::CollectionAssociation#delete
begin
require "bundler/inline"
rescue LoadError => e
$stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
raise e
end
gemfile(true) do
source "https://rubygems.org"
gem "rails", github: "rails/rails"
gem "sqlite3"
gem "benchmark-ips"
gem "kalibera"
end
require "active_record"
require 'benchmark/ips'
require "logger"
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.logger = Logger.new(nil)
ActiveRecord::Schema.define do
create_table :posts, force: true do |t|
end
create_table :comments, force: true do |t|
t.integer :post_id
end
end
class Post < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :post
end
post = Post.create!
comments = Comment.transaction do
100.times.map { post.comments.create! }
end
rm_comments = comments.sample(50)
Benchmark.ips do |bm|
bm.config(stats: :bootstrap)
bm.report("deleting #{rm_comments.count} records") do
post.comments.delete(*rm_comments)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment