Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Your Test Suite is Making Too Many Database Calls!

Your Test Suite is Making Too Many Database Calls!

Originally given at RailsConf 2022.

On a recent project, I sped up a test suite 15% by making a change to a single factory. This suite, like many others (including yours!), was making way too many database calls. It’s so easy to accidentally add extra queries to factories and test setup and these can compound to shockingly large numbers.

The chaos is your opportunity! Learn to profile and fix hot spots, build big-picture understanding through diagrams, and write code that is resistant to extraneous queries. This talk will equip you to take back control of your build times and maybe impress your teammates in the process.

30b3743553757e5959534d3e1251634f?s=128

Joël Quenneville

May 23, 2022
Tweet

More Decks by Joël Quenneville

Other Decks in Programming

Transcript

  1. None
  2. None
  3. None
  4. None
  5. None
  6. None
  7. None
  8. # BAD it "#full_name concatenates first and last name" do

    user = create( :user, first_name: "Joël", last_name: "Quenneville" ) expect(user.full_name).to eq "Joël Quenneville" end
  9. # GOOD it "#full_name concatenates first and last name" do

    user = User.new( first_name: "Joël", last_name: "Quenneville" ) expect(user.full_name).to eq "Joël Quenneville" end
  10. None
  11. None
  12. None
  13. None
  14. None
  15. None
  16. None
  17. None
  18. None
  19. let(:user1) { create(:user) } let(:user2) { create(:user) } let(:organization) {

    create(:organization), users: [user1, user2] } # this test only needs an org it "does something" # this test needs and org + some users it "does something whith multiple users"
  20. None
  21. None
  22. None
  23. None
  24. None
  25. let(:contact) { create(:contact) } let(:user) { create(:user, admin: true, contacts:

    [contact]) } # NEEDS 1 query # MAKES 3 queries (2x CREATE + 1x UPDATE) it "does something for a regular user" do user.update(admin: false) ... end # NEEDS 2 queries # MAKES 2 queries it "does something for an admin with contact info"
  26. None
  27. # this test only needs an org it "does something"

    do org = create(:organization) ... end # this test needs and org + some users it "does something whith multiple users" do user1 = create(:user) user2 = create(:user) org = create(:organization, users: [user1, user2] ... end
  28. None
  29. None
  30. None
  31. None
  32. None
  33. None
  34. factory :organization do members { create_list(:user, 3 } sequence(:name) {

    |n| "Organization #{n}" } end
  35. it "does something" do # this makes 4 INSERT queries

    org = create(:organization) expect(org.do_something).to eq "something" end
  36. None
  37. factory :organization do members { create_list(:user, 3 } sequence(:name) {

    |n| "Organization #{n}" } end factory :user do association :organization end
  38. it "does something given a user" do user = create(:user)

    service = MyService.new(user) expect(service.call()).to eq "something" end
  39. None
  40. None
  41. factory :organization do sequence(:name) { |n| "Organization #{n}" } end

  42. factory :organization do sequence(:name) { |n| "Organization #{n}" } trait

    :with_users do members { create_list(:user, 3 } end end
  43. None
  44. ActiveSupport::Notifications. subscribe("factory_bot.run_factory")

  45. [TEST PROF INFO] Factories usage Total: 15285 Total top-level: 10286

    Total time: 04:31.222 (out of 07.16.124) Total uniq factories: 119 total top-level total time time per call top-level time name 6091 2715 115.7671s 0.0426s 50.2517s user 2142 2098 93.3152s 0.0444s 92.1915s post ...
  46. None
  47. None
  48. class Organization < ApplicationRecord before_create :generate_admin def generate_admin admin =

    User.create!( email: "admin@#{organization.domain}", admin: true ) self.admin = admin end end
  49. it "does something" do admin = create(:user, admin: true) org

    = create(:organization, admin: admin) # ... end
  50. None
  51. None
  52. it "does something" do admin = create(:user, admin: true) org

    = create(:organization) # CODE SMELL! # This now makes 3 INSERTs + 1 UPDATE calls org.update(admin: admin) # ... end
  53. None
  54. None
  55. None
  56. factory_bot_results = {} config.before(:suite) do ActiveSupport::Notifications.subscribe("factory_bot.run_factory") do |name, start, finish,

    id, payload| # aggregate results end config.after(:suite) do puts factory_bot_results end
  57. None
  58. None
  59. None
  60. None
  61. factory :organization_with_events do staff { create_list(:staff, 1) } venues {

    create_list(:venue, 1) after(:build) do |org| create_list(:event, 3, venue: org.venues.first) end end
  62. factory :organization_with_events do staff { create_list(:staff, 1) } venues {

    create_list(:venue, 1) after(:build) do |org| create_list(:event, 3, venue: org.venues.first, staff: org.staff.first) end end
  63. None
  64. None
  65. None
  66. None
  67. None
  68. None