Speed up functional test execution (collection creation is the bottleneck)
Summary
The functional test suite (MongoDB.EntityFrameworkCore.FunctionalTests) spends most of its wall-clock time creating collections on the test MongoDB server, not running queries. Creating a collection costs about 40ms on the default Docker disk, and the server serializes creates (about 21 per second regardless of client concurrency). The suite creates about 11,000 collections per run. Putting the server data directory on tmpfs cut the suite from 639s to 164s with no test changes.
Background
- Measured on EF10 (Debug EF10), one version at a time, against mongodb/mongodb-atlas-local:latest in Docker on Windows (WSL2), 64 cores.
- Suite size: 7,463 tests in FunctionalTests (7,336 without the Encryption tests).
- Before this investigation, a full run of all three EF versions in parallel took about 15 minutes per round, and one version alone took about 9.5 to 10.5 minutes.
Findings
1. Test parallelization attributes
- [assembly:CollectionBehavior(DisableTestParallelization = true)] was removed from SpecificationTests/Usings.cs and FunctionalTests/Usings.cs.
- All 164 classes in FunctionalTests/Query carried [XUnitCollection("QueryTests")] and the 12 classes in FunctionalTests/Mapping carried [XUnitCollection("MappingTests")]. These were plain collection names (no CollectionDefinition, no ICollectionFixture), so they only forced serial execution. Each class uses its own IClassFixture<TemporaryDatabaseFixture> database. The attributes were removed from 176 files.
- Shared static state checked in Query and Mapping is thread-safe (Interlocked in TestDatabaseNamer and the collection-name fallback, ConcurrentDictionary in SingleEntityDbContext, SemaphoreSlim in TestServer). No environment-variable changes, BSON serializer or convention registration, or query-mode mutation was found in Query or Mapping. This was a targeted grep, not a read of all 176 classes.
- Stability runs (EF8, EF9 and EF10 in parallel, per-process Atlas-local container):
- SpecificationTests with parallelization enabled: 10 rounds x 3 versions, 0 failures. Passed counts were EF8 4714, EF9 5027, EF10 4768, identical every round.
- FunctionalTests with the attributes removed: 3 of the planned 10 rounds completed (9 runs), 0 failures. Passed counts were EF8 7322, EF9 7376, EF10 7456, identical every round. The loop was stopped after round 3.
- These changes are in the working tree and not yet committed.
2. Thread caps and class splitting do not help
- maxParallelThreads on EF10 alone: unlimited 588s, 16 threads 631s, 8 threads 586s, 4 threads 583s (all 7,456 passed). The cap does take effect: with a cap of 1, a three-class check took 80s wall against 67s unlimited.
- Sampled mongod CPU: median about 17%, 90th percentile about 65 to 75%, peak about 4 cores of 64. The container was not CPU saturated.
- Running three EF versions at once costs about 1.7x for a single class (NativeGroupByOverJoinTests: 22s alone, 37s per version when all three run), not the roughly 30x seen in the full loop.
- Per-test durations in the full-project loop were inflated by queueing: NativeReferenceIncludeTests and NativeNonKeyCorrelationTests showed about 700s each in the loop but took 33s together when run alone.
- Conclusion: splitting the "long tail" classes into more collections would not have helped while collection creation was the bottleneck.
3. Collection creation is serialized and slow on disk
Micro-benchmark on an idle Atlas-local container (200 operations each):
| Operation | Cost |
|---|---|
| Insert one doc into an existing collection | 4.5 ms |
| Explicit createCollection | 40.1 ms |
| Implicit create via first insert | 42.7 ms |
| New database + collection + insert | 45.5 ms |
A new database costs only about 5ms more than a new collection, so collection count matters more than database count.
Concurrent creates (each client creates 100 collections via first insert):
| Parallel clients | Avg per-create | Creates | Throughput |
|---|---|---|---|
| 1 | 42 ms | 100 | about 21/s |
| 4 | 158 ms | 400 | about 24/s |
| 16 | 747 ms | 1,600 | about 21/s |
| 32 | 1,538 ms | 3,200 | about 20/s |
Throughput is flat, so creation is serialized in the server. Plain mongo:8 behaves the same (37 ms alone, 734 ms with 16 clients), so this is not specific to Atlas-local.
With the data directory on tmpfs (mongo:8): 2.3 ms alone and 23.9 ms with 16 clients, about 16x faster. Disk durability (Docker VM disk syncs) is the cost. (An attempt to also use --nojournal did not start, so it was not measured.)
4. Volume created by each suite
Measured against a dedicated server with MONGODB_URI set, EF10, single run:
| SpecificationTests | FunctionalTests | |
|---|---|---|
| Tests | 4,768 passed + 14 skipped | 7,463 total |
| Wall clock | 39s | 573s (disk) |
| Databases | 24 | 277 |
| Collections | 149 | 11,023 |
| Collections per test | about 0.03 | about 1.5 |
| Cumulative write latency | 116 s | 13,389 s |
| Cumulative command latency | 275 s | 3,502 s |
Roughly 11,000 creates at about 45ms each is about 500s of serialized work, which accounts for nearly all of the 573s run.
5. tmpfs data directory result (FunctionalTests, EF10, encryption excluded)
| Data directory | Wall clock | Result |
|---|---|---|
| Docker disk (default) | 639s | 7330 passed, 0 failed, 6 skipped |
| tmpfs | 164s | 7330 passed, 0 failed, 6 skipped |
About 3.9x faster with no test changes. Cumulative write latency dropped from 13.4M ms to 82K ms and command latency from 3.5M ms to 17K ms.
Setup notes:
- mongod in the Atlas-local image runs as uid 1000. A plain -
tmpfs /data/dbis root-owned and mongod fails with "Permission denied" creating /data/db/journal. Use -tmpfs /data/db:rw,uid=1000,gid=1000,mode=0755 (and the same for /data/mongot). - The remaining tail with tmpfs, once encryption was excluded, was RepresentedPropertyPushdownTests (1,524 tests, about 106s of summed test time). Splitting it may now help, but only at the margin.
SpecificationTests issues (small cost, worth fixing)
The spec tests are meant to avoid creating databases, but two things in the provider's test infrastructure defeat that:
- NorthwindQueryMongoFixture (and the other spec fixtures) set StoreName as an instance property initialised with TestDatabaseNamer.GetUniqueDatabaseName("Northwind"). Every fixture instance gets a new name, so about 22 classes created 20 separate Northwind databases, each seeded separately.
- MongoTestStoreFactory.GetOrCreate just calls Create, so no store is ever reused. EF's relational providers cache the store by name so seeding happens once per process.
Proposed fix: make the name a static readonly field (unique per process) and cache the MongoTestStore by name in GetOrCreate. Estimated saving is several seconds of the 39s spec run; this has not been prototyped or measured.
Proposed work
- Run the test container with a tmpfs data directory (largest win, least code). Add a tmpfs mount with uid=1000,gid=1000,mode=0755 for /data/db and /data/mongot in the code that builds the container (TestServer / MongoDbAtlasBuilder, probably WithTmpfsMount). This only helps runs that use the testcontainer; it does not help a user-supplied MONGODB_URI server.
- Reduce collection creation in the functional tests, for runs against an external server and as a further saving: pool and reuse collections per fixture (clear between tests) instead of creating one per test. This is more invasive, since tests depend on collection names, indexes and per-test options.
- Fix the spec test Northwind store sharing (see above).
- Consider splitting RepresentedPropertyPushdownTests (and any other remaining large classes) once creation cost is down.
- Commit or revert the parallelization attribute removals (in the working tree now) once stability has been confirmed with a full 10-round run on all three EF versions.
Caveats and open items
- The FunctionalTests stability loop finished only 3 of 10 planned rounds; the planned 20-round run of all three projects on EF8, EF9 and EF10 was not started.
- The Encryption tests (QueryableEncryption 76, AutoEncryption 10) failed with "CryptSharedLib is required, but was not found or not loaded" when MONGODB_URI pointed at a plain container in this setup. They passed when each test process started its own container (the earlier loops). Their behavior on tmpfs, and in CI, was not checked.
- tmpfs timings are from one machine (Windows, Docker/WSL2, 64 cores, about 125 GB RAM). Gains in CI may differ, depending on the Docker storage driver and host.
- Test-run durations from TRX files sum wall-clock time under load and overstate the cost of individual tests; do not use them to rank class cost without checking against an isolated run.
- The ticket id and the exact files to change should be confirmed when the ticket is filed.