فینورا پرو - سامانه جامع حسابداری و صدور فاکتور

feat(A08): add deterministic 10k-product 50k-invoice IndexedDB performance baseline

davoodmehraban89 committed Oct 14, 2026 at 23:19 UTC 5f6bd29ad8607afd6240da26a568ff00b27d7f0a
1 file changed +96
scripts/a08-benchmark.mjs new
+96
@@ -0,0 +1,96 @@
1 +import 'fake-indexeddb/auto';
2 +import { writeFileSync } from 'node:fs';
3 +import { pathToFileURL } from 'node:url';
4 +import { performance } from 'node:perf_hooks';
5 +import { StorageService } from '../src/core/StorageService.js';
6 +
7 +// Synthetic, deterministic and devoid of real customer data.
8 +export const COUNTS = Object.freeze({ products: 10_000, invoices: 50_000, companies: 5 });
9 +export function syntheticRecord(store, i) {
10 + const companyId = 'benchmark-c' + (i % COUNTS.companies);
11 + if (store === 'products') return {
12 + id: 'bench-p-' + i, companyId, ownerUserId: 'bench-admin',
13 + name: 'Synthetic product ' + i, code: 'SKU-' + i, sellPrice: 1000 + i
14 + };
15 + if (store === 'invoices') return {
16 + id: 'bench-i-' + i, companyId, ownerUserId: 'bench-admin',
17 + date: '1405/07/18', kind: 'sale', number: String(i + 1),
18 + grandTotal: 100000 + i, paidAmount: 0, remaining: 100000 + i,
19 + isPreInvoice: false
20 + };
21 + throw new Error('Unsupported synthetic store');
22 +}
23 +const rounded = n => Math.round(n * 100) / 100;
24 +const elapsed = async fn => {
25 + const start = performance.now();
26 + const result = await fn();
27 + return { ms: rounded(performance.now() - start), result };
28 +};
29 +const percentile = (values, q) => {
30 + const valuesSorted = [...values].sort((a, b) => a - b);
31 + return rounded(valuesSorted[Math.min(valuesSorted.length - 1, Math.ceil(q * valuesSorted.length) - 1)]);
32 +};
33 +async function seed(store, count, batchSize = 250) {
34 + const start = performance.now();
35 + for (let base = 0; base < count; base += batchSize) {
36 + const batch = Array.from({ length: Math.min(batchSize, count - base) },
37 + (_, index) => syntheticRecord(store, base + index));
38 + await StorageService.transaction(store, async tx => {
39 + await Promise.all(batch.map(row => tx.putRaw(store, row)));
40 + });
41 + }
42 + return rounded(performance.now() - start);
43 +}
44 +export async function benchmarkA08() {
45 + await StorageService.init();
46 + const seedProductsMs = await seed('products', COUNTS.products);
47 + const seedInvoicesMs = await seed('invoices', COUNTS.invoices);
48 +
49 + const checks = await Promise.all([
50 + StorageService.count('products'), StorageService.count('invoices')
51 + ]);
52 + if (checks[0] !== COUNTS.products || checks[1] !== COUNTS.invoices) {
53 + throw Error('Synthetic record counts do not match the target');
54 + }
55 +
56 + const queryTimes = [];
57 + let scopedCount = null;
58 + for (let i = 0; i < 10; i++) {
59 + const measured = await elapsed(() => StorageService.getByCompany('invoices', 'benchmark-c0'));
60 + queryTimes.push(measured.ms);
61 + scopedCount = measured.result.length;
62 + if (scopedCount !== 10_000) throw Error('Unexpected company filtering result');
63 + }
64 + const snapshot = await elapsed(() => StorageService.exportAll());
65 + const snapshotCounts = {
66 + products: snapshot.result.data.products.length,
67 + invoices: snapshot.result.data.invoices.length
68 + };
69 + if (snapshotCounts.products !== 10_000 || snapshotCounts.invoices !== 50_000) {
70 + throw Error('Backup snapshot counts do not match seed totals');
71 + }
72 + // No fixed millisecond budget until an actual machine baseline exists.
73 + const report = {
74 + benchmark: 'A08-synthetic-10000-products-50000-invoices-v1',
75 + timestamp: new Date().toISOString(),
76 + node: process.version,
77 + counts: { ...COUNTS, scopedInvoices: scopedCount },
78 + timingsMs: {
79 + seedProducts: seedProductsMs, seedInvoices: seedInvoicesMs,
80 + companyInvoiceRead: { p50: percentile(queryTimes, .5), p95: percentile(queryTimes, .95) },
81 + fullBackupExport: snapshot.ms
82 + },
83 + memoryMiB: {
84 + rss: rounded(process.memoryUsage().rss / 1024 / 1024),
85 + heapUsed: rounded(process.memoryUsage().heapUsed / 1024 / 1024)
86 + },
87 + note: 'Synthetic fake-indexeddb baseline; not a Windows disk crash or customer performance measurement.'
88 + };
89 + return report;
90 +}
91 +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
92 + benchmarkA08().then(report => {
93 + writeFileSync('a08-benchmark-results.json', JSON.stringify(report, null, 2) + '\n');
94 + console.log(JSON.stringify(report, null, 2));
95 + }).catch(error => { console.error(error); process.exitCode = 1; });
96 +}