thoughtbot / factory_bot

A library for setting up Ruby objects as test data.
https://thoughtbot.com
MIT License
7.93k stars 2.6k forks source link

Populating an `hstore` in Factories causes a significant slowdown #1549

Open joshuamcginnis opened 2 years ago

joshuamcginnis commented 2 years ago

Description

I've noticed a significant performance hit when running rspec tests whenever there is a factory that populates an hstore column.

Reproduction Steps

Create an hstore column and set it like so:

FactoryBot.define do
  factory :artifact do
    subject { 'foo_subject' }
    subject_address { { 'street' => Faker::Address.street_address } }
  end
end

When an rspec test uses this factory with subject_address commented out, the test runs 3-6x faster. An example test to demonstrate the speed difference could be as simple as:


RSpec.describe Example do
  let(:artifact) { create(:artifact) }
  it 'does something' { artifact }
end

### System configuration
**factory_bot version**:  6.2.0
**rails version**:  7.0.3.1
**ruby version**:  3.1.2p20
composerinteralia commented 2 years ago

Given the factory definition in your comment, calling create(:artifact) is equivalent to:

artifact = Artifact.new
artifact.subject = "foo_subject"
artifact.subject_address = { 'street' => Faker::Address.street_address }
artifact.save!

Do you see the same slowdown if you run that instead of create(:artifact)?