Created
May 2, 2020 23:42
-
-
Save kamipo/36d869fff81cf878658adc26ee38ea1b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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", "< 2.8" | |
end | |
require "active_record" | |
require "minitest/autorun" | |
require "logger" | |
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:") | |
ActiveRecord::Schema.define do | |
create_table :coupons, force: true do |t| | |
t.text :name | |
end | |
create_table :sites, force: true do |t| | |
t.text :name | |
end | |
create_table :products, force: true do |t| | |
t.text :name | |
end | |
create_table :coupons_sites, force: true, id: false do |t| | |
t.references :coupon | |
t.references :site | |
end | |
create_table :coupons_products, force: true, id: false do |t| | |
t.references :coupon | |
t.references :product | |
end | |
end | |
class Coupon < ActiveRecord::Base | |
has_and_belongs_to_many :products | |
has_and_belongs_to_many :sites | |
end | |
class Product < ActiveRecord::Base | |
has_and_belongs_to_many :coupons | |
end | |
class Site < ActiveRecord::Base | |
has_and_belongs_to_many :coupons | |
end | |
require "objspace" | |
require "active_support/core_ext/numeric/conversions" | |
puts | |
puts | |
puts "Memory usage: #{ObjectSpace.memsize_of_all.to_s(:human_size)}" | |
Coupon.transaction do | |
1.upto(250_000) do |i| | |
Coupon.create! | |
if i % 500 == 0 | |
GC.start | |
puts "Created #{ i } coupons..." | |
puts "Memory usage: #{ObjectSpace.memsize_of_all.to_s(:human_size)}" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment