database_cleaner-active_record icon indicating copy to clipboard operation
database_cleaner-active_record copied to clipboard

Possibly excessive transaction count with ActiveRecord

Open tobypinder opened this issue 9 years ago • 0 comments

I am using a standard :transaction DatabaseCleaner block with rspec:

    config.around(:each) do |example|
      DatabaseCleaner[:active_record, connection: :test].cleaning do
        example.run
      end
    end

When trying to profile the queries in the test suite, I noticed there was a large number of COMMIT statements: one for each test, and equivalent to the ROLLBACK query count. After digging, it seems the strategy runs a blank transaction in the DatabaseCleaner#begin block: https://github.com/DatabaseCleaner/database_cleaner/blob/cda982c9b4a8f3f116ed7e39766eb1447e0197aa/lib/database_cleaner/active_record/transaction.rb#L12

From what I can tell with git's blame/log, this was added in 1af146fb80fef05acca0d1a322bb6987af645cc0 and then modified as a fix for DatabaseCleaner/database_cleaner#200. These seem to indicate that this empty transaction only needs to be run once, perhaps at the start of the suite, or perhaps with a flag indicating it's been run once and doesn't need to run again.

It's clearly not the end of the world, but I am currently on a spec optimisation push and this extra BEGIN/COMMIT per test is having a not-insignificant (but admittedly not major) impact on test time: My metrics indicate it's adding about 15 seconds of time to a suite with 6200ish tests.

Happy to help and create a pull request if there's consensus that I haven't misunderstood the purpose of that statement!

tobypinder avatar Mar 25 '15 14:03 tobypinder