Skip to content

Instantly share code, notes, and snippets.

@ghiculescu
Created June 17, 2021 19:20
  • Select an option

Select an option

Revisions

  1. ghiculescu created this gist Jun 17, 2021.
    51 changes: 51 additions & 0 deletions bench.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    # 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", branch: "main"
    # gem "rails", github: "ghiculescu/rails", branch: "has-many-build-perf-regression"
    gem "sqlite3"
    gem "benchmark-ips"
    end

    require "active_record"
    require "active_support"
    require "logger"

    ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
    ActiveRecord::Base.logger = Logger.new(STDOUT)

    ActiveRecord::Schema.define do
    create_table :authors, force: true do |t|
    end
    create_table :posts, force: true do |t|
    t.references :author
    end
    end

    class Author < ActiveRecord::Base
    has_many :posts
    end

    class Post < ActiveRecord::Base
    belongs_to :author
    end

    author = Author.create!

    posts = 100_000.times.map { |n| { author_id: author.id } }
    Post.insert_all(posts)

    author = Author.find(author.id)
    author.posts.load_target

    Benchmark.ips do |x|
    x.report("main branch") { author.posts.build }
    # x.report("my branch") { author.posts.build }
    x.compare!
    end