Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Step 1, stackprof analysis
Analysing with stackprof, we get the confirmation that test slowness comes from sql requests:
Looking up the callers of
#exec_params
we can seethe
#foreign_keys
method seems to be the outlier by far. And the foreign key method is itself mostly called by#disable_referential_integrity
:And the method that seems to make most extensive calls for this is
#reset_fixtures
. Indeed,#reset_fixtures
is looping over an array of features, and for each calling#create_fixtures
, which itself calls#disable_referential_integrity
. Hence we call#disable_referential_integrity
once per feature. This is very costly. We could already try to refactor this out of the loop. By looking further in the codebase, this is at most a 3 items array, there might be a better option.Step 2, stackprof analysis without
#disable_referential_integrity
If we change
#disable_referential_integrity
to be a noop, we have a new interesting stackprof result. First, the test time is divided by 4. So there is something to do here. And then, theSchemaTest#setup
method is now taking 9.5% of the test time:activerecord-cockroachdb-adapter/test/excludes/SchemaTest.rb
Line 17 in 0fe2427
Hence this could be the next thing to tackle.
Besides that it seems like there is no specific local code to optimize:
This means that 94% of the samples are coming from the activerecord stack. With that said, most of these samples actually are happening while waiting for a sql query. If we isolate the only file sampled within the adapter lib:
We see that it still takes an active part of the samples (23%) are of its callees. Also there might be some inefficient queries in our codebase. However, within this file, the only homemade query that hits some sampling is the query in
#foreign_keys
. We should look at optimizing it as it is a frequently used method.On trying to reduce calls to
disable_referential_integrity
when do we
#disable_referential_integrity
?Ideas
Analysis: