tane/apps/app_seeds/test/data/inventory_scale_test.dart
vjrj 3de01bd948 perf(inventory): lazy list, photo thumbnails, indexes, debounced reload
Scale the local inventory to 10k+ varieties:
- Render the list with ListView.builder over a flattened header/item model
  instead of building every tile upfront.
- Store a small regenerable JPEG thumbnail per photo (schema v14) and use it
  for the 48px list avatar; full bytes stay for offer image hosting. Existing
  photos are backfilled lazily at startup. Thumbnail is local-only (excluded
  from CRDT sync and backups by the JSON codec).
- Add indexes on varieties(is_deleted,is_draft), attachments(parent_type,
  parent_id,kind), lots(variety_id) via @TableIndex.
- Debounce watchInventoryView (~250ms) so a burst of table writes triggers one
  reload, not seven.
- cacheWidth/cacheHeight on the list avatar decode.
- Scale test raised 3k -> 10k; migration test v13 -> v14.
2026-07-20 22:53:48 +02:00

55 lines
1.8 KiB
Dart

import 'package:commons_core/commons_core.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:tane/data/variety_repository.dart';
import 'package:tane/db/database.dart';
import 'package:tane/db/enums.dart';
import '../support/test_support.dart';
/// A real seed bank can hold thousands of accessions. This guards that the
/// list query stays a handful of batched queries (not N+1) and returns in a
/// human blink even at that scale — the pre-beta performance check.
void main() {
late AppDatabase db;
late VarietyRepository repo;
setUp(() {
db = newTestDatabase();
repo = newTestRepository(db);
});
tearDown(() => db.close());
test('the inventory view loads 10000 varieties quickly', () async {
const count = 10000;
for (var i = 0; i < count; i++) {
final id = await repo.addQuickVariety(
label: 'Variety $i',
category: i.isEven ? 'Poaceae' : 'Fabaceae',
);
// Every few varieties, add a lot so the batched lot/type/share queries
// do real work.
if (i % 5 == 0) {
await repo.addLot(
varietyId: id,
harvestYear: 2020 + (i % 5),
quantity: const Quantity(kind: QuantityKind.handful),
offerStatus: i % 25 == 0 ? OfferStatus.shared : OfferStatus.private,
);
}
}
final sw = Stopwatch()..start();
final view = await repo.watchInventoryView().first;
sw.stop();
expect(view.items, hasLength(count));
// Generous ceiling for CI (the point is to catch an N+1 regression, not to
// micro-benchmark). Includes the ~250ms debounce on the first emit; typical
// local runs are well under 2s for 10k rows with the v14 indexes.
expect(
sw.elapsedMilliseconds,
lessThan(6000),
reason: 'inventory view took ${sw.elapsedMilliseconds}ms for $count rows',
);
});
}