Skip to content

Comments

Speed up TestCreditLines by avoiding CouchDB user operations#37428

Closed
dannyroberts wants to merge 2 commits intomasterfrom
dmr/speed-up-test-credit-lines
Closed

Speed up TestCreditLines by avoiding CouchDB user operations#37428
dannyroberts wants to merge 2 commits intomasterfrom
dmr/speed-up-test-credit-lines

Conversation

@dannyroberts
Copy link
Member

Product Description

N/A — test-only change.

Technical Summary

TestCreditLines was taking 200+ seconds in CI due to CouchDB operations:
creating CommCareUser documents, querying CouchDB views to count them via
calculate_users_in_all_domains, and deleting them in teardown.

The invoicing code only reads DomainUserHistory SQL records, so we can
create those directly instead of round-tripping through CouchDB. This follows
the same pattern already used by TestSubscriptionChangeTransfersSubscriptionLevelCredit
in the same file.

Also removes _generate_users_fee_to_credit_against which was dead code
(defined but never called).

Feature Flag

N/A

Safety Assurance

Safety story

Test-only change. The refactored tests exercise the same invoicing and credit
line logic — the only difference is how DomainUserHistory records are
created (directly via SQL instead of via CouchDB user creation + counting).

Automated test coverage

The 5 test methods in TestCreditLines themselves verify the behavior is
unchanged.

QA Plan

CI passing is sufficient.

Rollback instructions

  • This PR can be reverted after deploy with no further considerations

Labels & Review

  • Risk label is set correctly
  • The set of people pinged as reviewers is appropriate for the level of risk of the change

Replace CouchDB CommCareUser creation and calculate_users_in_all_domains
calls with direct DomainUserHistory SQL records. The invoicing code only
reads DomainUserHistory, so creating CouchDB users just to count them is
unnecessary overhead causing 200+ second setup times in CI.

Also removes dead code (_generate_users_fee_to_credit_against was never
called).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@dannyroberts dannyroberts added the DON'T REVIEW YET This PR is not ready for review. Commits may be rebased or force-pushed. Don't waste your time. label Feb 23, 2026
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@dannyroberts dannyroberts force-pushed the dmr/speed-up-test-credit-lines branch from 952d61d to fdd1f05 Compare February 24, 2026 13:46
@dannyroberts
Copy link
Member Author

dannyroberts commented Feb 24, 2026

(This comment was generated by Claude, but I've reviewed it and stand behind it—this is my current best understanding.)

Investigation results

The 200+ second setup time attributed to TestCreditLines in CI is not caused by the test itself. It's the one-time DeferredDatabaseContext.setup_databases() cost (CouchDB, Elasticsearch, BlobDB, SQL database initialization) that gets charged to whichever TestCase runs first in each shard.

Evidence: all three shards show the same ~200s setup on their first DB test, regardless of which test it is:

Shard First DB test Setup time
bf TestCreditLines 183.95s
6a TestUpgradeSoftwarePlanToLatestVersion 204.70s
05 TestKeepSoftwarePlanConsistentManagementCommand 211.94s

The changes in this PR (replacing CouchDB CommCareUser creation with direct DomainUserHistory SQL records) are a valid ~20s improvement to the per-test overhead, but they don't address the headline number since that's infrastructure setup.

Closing since the original premise (TestCreditLines setup being unusually slow) turned out to be a misattribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

DON'T REVIEW YET This PR is not ready for review. Commits may be rebased or force-pushed. Don't waste your time.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant