Skip to content

perf(warehouse): D — prefetch Shopify variant theo lô cho create-sale (~10× nhanh hơn)#255

Open
ecommeanblvd wants to merge 1 commit into
mainfrom
perf/create-sale-batch
Open

perf(warehouse): D — prefetch Shopify variant theo lô cho create-sale (~10× nhanh hơn)#255
ecommeanblvd wants to merge 1 commit into
mainfrom
perf/create-sale-batch

Conversation

@ecommeanblvd

Copy link
Copy Markdown
Owner

Vì sao

Engine C (create-sale.ts) đánh giá từng SKU bằng 2 query Shopify + 2 sleep (base + base-Sale) → ~2·N round-trip cho 504 SKU, dry-run/cron mất ~8 phút.

Thay đổi

  • Prefetch theo lô: gộp mọi term (base, base-Sale) thành query sku:'a' OR sku:'b' … (25 term/query), chạy 1 lần, bucket kết quả theo SKU (UPPERCASE) vào Map.
  • evaluateSku giờ thuần (sync), tra variantMap in-memory — không I/O trong vòng lặp.
  • Tách VARIANT_FIELDS fragment dùng chung cho query đơn + query lô (không lặp field).
  • Re-check trước khi ghi vẫn giữ query live ${base}-Sale (chống race giữa evaluate ↔ write) — chỉ chạy cho nhóm thực sự tạo.
  • Helper thuần mới có unit test: chunk(), bucketBySku().

Kiểm chứng

  • tsc --noEmit: 0 lỗi · vitest create-sale-logic: 27/27 pass · eslint: clean
  • Dry-run production (read-only): 47.1s (từ ~8 phút), 0 lỗi, tally y hệt bản cũ:
    ACTIVE 211 · not-found 185 · -Sale 86 · DRAFT 16 · customize 5 · mean 1 · eligible 0

Behavior-preserving thuần tối ưu tốc độ — không đổi quyết định tạo/skip.

🤖 Generated with Claude Code

Vòng đánh giá cũ query Shopify 2 lần/SKU (base + base-Sale) + sleep →
~2·N round-trip cho 504 SKU (~8 phút). Gộp thành prefetch theo lô
(sku:'a' OR sku:'b'…, 25 term/query) 1 lần, bucket theo SKU (UPPERCASE),
rồi evaluateSku tra map in-memory (thuần, không I/O).

Dry-run production: 47s (từ ~8 phút), 0 lỗi, tally y hệt bản cũ
(ACTIVE 211 / not-found 185 / -Sale 86 / DRAFT 16 / customize 5 / mean 1).

- create-sale-logic.ts: thêm chunk() + bucketBySku() (thuần, có test)
- create-sale.ts: VARIANT_FIELDS fragment dùng chung; prefetchVariantsBySku;
  evaluateSku → sync tra map; recheck trước khi ghi vẫn live (an toàn race)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant