EctoDoctor.RepeatedQueries (Ecto Doctor v0.0.1)

Copy Markdown View Source

Catches N+1 queries while your tests run and while you use the app in dev.

It reports a query that ran at least :threshold times, two by default, from the same call stack in one unit of work. Each test, request, LiveView event and Oban job is a unit. Only reads count, never inserts, updates or deletes.

Turn on stack traces for the repo in config/dev.exs and config/test.exs so findings point at your code.

config :my_app, MyApp.Repo, stacktrace: true

Attach it in test/test_helper.exs and scan every test from your case templates.

EctoDoctor.RepeatedQueries.attach(repo: MyApp.Repo)

# in the setup of DataCase and ConnCase
setup tags do
  :ok = EctoDoctor.RepeatedQueries.scan_test(tags)
  MyApp.DataCase.setup_sandbox(tags)
  :ok
end

A test tagged @tag repeated_queries: false isn't scanned. mix ecto_doctor.install does all of this for you.

Adopting it in an existing app

Until the baseline file exists findings only print. Run ECTO_DOCTOR_RECORD=1 mix test once to record the N+1 queries you have today. From then on only new ones fail a test.

Options

  • :repo - Required. Names the repo to watch or a list of repos.

  • :threshold (pos_integer/0) - Sets how many identical queries make an N+1. The default value is 2.

  • :mode - Chooses whether a test with a new N+1 fails or only prints it. The default value is :raise.

  • :baseline - Leaves out the N+1 queries listed in this file. The default value is ".ecto_doctor_baseline.exs".

  • :apps (list of atom/0) - Picks the apps whose code counts in call stacks. Defaults to the repos' apps.

  • :allow (list of String.t/0) - Ignores N+1 queries whose call stack has a file path containing one of these. The default value is [].

  • :focus (list of String.t/0) - Reports only N+1 queries whose call stack has a file path containing one of these. The default value is [].

  • :ignore_queries (list of String.t/0) - Ignores queries whose SQL contains one of these. The default value is [].

  • :ignore_frames (list of atom/0) - Leaves these modules out of the compared call stacks, such as wrappers around Repo. The default value is [].

  • :min_duration_ms (non_neg_integer/0) - Reports only N+1 queries that took at least this long in total. The default value is 0.

  • :sample_rate - Sets the share of requests and jobs to scan, from 0 to 1. Tests are always scanned. The default value is 1.0.

  • :log (boolean/0) - Prints each N+1 to standard error. The default value is true.

  • :github_annotations (boolean/0) - Prints GitHub Actions annotations. On by default inside GitHub Actions.

  • :json_report - Appends each N+1 to this file as a line of JSON. The default value is nil.

Telemetry

Each finding emits [:ecto_doctor, :repeated_query] with :count and :duration_ms. Its metadata holds :sql, :frames, :fix, :key and :label.

Summary

Functions

Starts watching the repo's queries.

Stops watching.

Runs the function in a scan of its own and returns its N+1 queries without reporting them.

Leaves the code in the function out of the scan it runs in and returns what the function returns.

Scans the code in the function and reports its N+1 queries without raising.

Scans one ExUnit test and fails it on a new N+1 query.

Functions

attach(opts)

@spec attach(keyword()) :: :ok

Starts watching the repo's queries.

Call it once in test/test_helper.exs or when your app starts in dev.

detach()

@spec detach() :: :ok

Stops watching.

findings(fun)

@spec findings((-> term())) :: [EctoDoctor.RepeatedQueries.Finding.t()]

Runs the function in a scan of its own and returns its N+1 queries without reporting them.

The assertions in EctoDoctor.RepeatedQueries.Assertions use it.

pause(fun)

@spec pause((-> result)) :: result when result: term()

Leaves the code in the function out of the scan it runs in and returns what the function returns.

scan(label \\ nil, fun)

@spec scan(nil | String.t(), (-> result)) :: result when result: term()

Scans the code in the function and reports its N+1 queries without raising.

The label shows in each finding. It returns what the function returns.

EctoDoctor.RepeatedQueries.scan("nightly import", &Importer.run_nightly/0)

scan_test(context)

@spec scan_test(map()) :: :ok

Scans one ExUnit test and fails it on a new N+1 query.

setup {EctoDoctor.RepeatedQueries, :scan_test}