Skip to content

Instantly share code, notes, and snippets.

@rosenfeld
Created August 23, 2013 12:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rosenfeld/6318820 to your computer and use it in GitHub Desktop.
Save rosenfeld/6318820 to your computer and use it in GitHub Desktop.
factories overhead over fixtures
require 'sequel'
require 'benchmark'
# createdb factories_test
DB = Sequel.connect 'postgres://localhost/factories_test'
DB.drop_table? :test
DB.create_table :test do
primary_key :id
String :name, null: false
index :name
end
class Test < Sequel::Model(:test);end
Test.count # warm-up
Benchmark.bm 40 do |x|
x.report 'Batch insert' do
DB << "insert into test(name) values " + (1..100).map{|i| "('name#{i}')"}.join(', ')
end
x.report "insert one at a time" do
100.times do |i|
DB << "insert into test(name) values('name#{i}')"
end
end
x.report "insert one at a time simulating factories" do
100.times do |i|
Test.new.tap{|t| t.name = "name#{i}"}.save
end
end
end
# user system total real
# Batch insert 0.000000 0.000000 0.000000 ( 0.020025)
# insert one at a time 0.010000 0.010000 0.020000 ( 1.446176)
# insert one at a time simulating factories 0.080000 0.010000 0.090000 ( 1.604117)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment