einvoice/test/suite/einvoice_performance/test.perf-01.detection-speed.ts

264 lines
8.7 KiB
TypeScript
Raw Normal View History

2025-05-25 19:45:37 +00:00
/**
* @file test.perf-01.detection-speed.ts
* @description Performance tests for format detection speed
*/
2025-05-28 19:37:00 +00:00
import { expect, tap } from '@git.zone/tstest/tapbundle';
import { FormatDetector } from '../../../ts/formats/utils/format.detector.js';
import { InvoiceFormat } from '../../../ts/interfaces/common.js';
2025-05-25 19:45:37 +00:00
2025-05-28 19:37:00 +00:00
// Simple performance tracking
class SimplePerformanceTracker {
private measurements: Map<string, number[]> = new Map();
private name: string;
2025-05-25 19:45:37 +00:00
2025-05-28 19:37:00 +00:00
constructor(name: string) {
this.name = name;
}
addMeasurement(key: string, time: number): void {
if (!this.measurements.has(key)) {
this.measurements.set(key, []);
2025-05-25 19:45:37 +00:00
}
2025-05-28 19:37:00 +00:00
this.measurements.get(key)!.push(time);
}
getStats(key: string) {
const times = this.measurements.get(key) || [];
if (times.length === 0) return null;
const sorted = [...times].sort((a, b) => a - b);
return {
avg: times.reduce((a, b) => a + b, 0) / times.length,
min: sorted[0],
max: sorted[sorted.length - 1],
p95: sorted[Math.floor(sorted.length * 0.95)]
};
}
printSummary(): void {
console.log(`\n${this.name} - Performance Summary:`);
for (const [key, times] of this.measurements) {
const stats = this.getStats(key);
if (stats) {
console.log(` ${key}: avg=${stats.avg.toFixed(2)}ms, min=${stats.min.toFixed(2)}ms, max=${stats.max.toFixed(2)}ms, p95=${stats.p95.toFixed(2)}ms`);
2025-05-25 19:45:37 +00:00
}
}
2025-05-28 19:37:00 +00:00
}
}
const performanceTracker = new SimplePerformanceTracker('PERF-01: Format Detection Speed');
tap.test('PERF-01: Single file detection benchmarks', async () => {
const testCases = [
{
name: 'UBL Invoice',
content: `<?xml version="1.0" encoding="UTF-8"?>
<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2">
<ID>123</ID>
<IssueDate>2025-01-25</IssueDate>
</Invoice>`,
expectedFormat: InvoiceFormat.UBL
},
{
name: 'CII Invoice',
content: `<?xml version="1.0" encoding="UTF-8"?>
<rsm:CrossIndustryInvoice xmlns:rsm="urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100"
xmlns:ram="urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100">
<rsm:ExchangedDocument>
<ram:ID>123</ram:ID>
</rsm:ExchangedDocument>
</rsm:CrossIndustryInvoice>`,
expectedFormat: InvoiceFormat.CII
},
{
name: 'Factur-X',
content: `<?xml version="1.0" encoding="UTF-8"?>
<rsm:CrossIndustryInvoice xmlns:rsm="urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100"
xmlns:ram="urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100"
xmlns:qdt="urn:un:unece:uncefact:data:standard:QualifiedDataType:100">
<rsm:ExchangedDocumentContext>
<ram:GuidelineSpecifiedDocumentContextParameter>
<ram:ID>urn:factur-x.eu:1p0:minimum</ram:ID>
</ram:GuidelineSpecifiedDocumentContextParameter>
</rsm:ExchangedDocumentContext>
</rsm:CrossIndustryInvoice>`,
expectedFormat: InvoiceFormat.FACTURX
}
];
const iterations = 100;
2025-05-25 19:45:37 +00:00
2025-05-28 19:37:00 +00:00
for (const testCase of testCases) {
const times: number[] = [];
for (let i = 0; i < iterations; i++) {
const startTime = performance.now();
const format = FormatDetector.detectFormat(testCase.content);
const endTime = performance.now();
2025-05-25 19:45:37 +00:00
2025-05-28 19:37:00 +00:00
const duration = endTime - startTime;
times.push(duration);
performanceTracker.addMeasurement(`detect-${testCase.name}`, duration);
2025-05-25 19:45:37 +00:00
2025-05-28 19:37:00 +00:00
if (i === 0) {
expect(format).toEqual(testCase.expectedFormat);
2025-05-25 19:45:37 +00:00
}
}
2025-05-28 19:37:00 +00:00
// Calculate statistics
times.sort((a, b) => a - b);
const avg = times.reduce((a, b) => a + b, 0) / times.length;
const p95 = times[Math.floor(times.length * 0.95)];
console.log(`${testCase.name}: avg=${avg.toFixed(3)}ms, p95=${p95.toFixed(3)}ms`);
// Performance assertions
expect(avg).toBeLessThan(5); // Average should be less than 5ms
expect(p95).toBeLessThan(10); // 95th percentile should be less than 10ms
}
});
tap.test('PERF-01: Quick detection performance', async () => {
// Test the quick string-based detection performance
const largeInvoice = `<?xml version="1.0" encoding="UTF-8"?>
<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">
<cbc:ID>LARGE-TEST-001</cbc:ID>
<cbc:IssueDate>2025-01-25</cbc:IssueDate>
${Array(1000).fill('<cac:InvoiceLine><cbc:ID>1</cbc:ID></cac:InvoiceLine>').join('')}
</Invoice>`;
const iterations = 50;
const times: number[] = [];
2025-05-25 19:45:37 +00:00
2025-05-28 19:37:00 +00:00
for (let i = 0; i < iterations; i++) {
const startTime = performance.now();
const format = FormatDetector.detectFormat(largeInvoice);
const endTime = performance.now();
const duration = endTime - startTime;
times.push(duration);
performanceTracker.addMeasurement('large-invoice-detection', duration);
}
const avg = times.reduce((a, b) => a + b, 0) / times.length;
console.log(`Large invoice detection: avg=${avg.toFixed(3)}ms`);
// Even large invoices should be detected quickly due to quick string check
expect(avg).toBeLessThan(10);
});
tap.test('PERF-01: Edge cases detection performance', async () => {
const edgeCases = [
{
name: 'Empty string',
content: '',
expectedFormat: InvoiceFormat.UNKNOWN
},
{
name: 'Invalid XML',
content: '<not-closed',
expectedFormat: InvoiceFormat.UNKNOWN
},
{
name: 'Non-invoice XML',
content: '<?xml version="1.0"?><root><data>test</data></root>',
expectedFormat: InvoiceFormat.UNKNOWN
2025-05-25 19:45:37 +00:00
}
2025-05-28 19:37:00 +00:00
];
2025-05-25 19:45:37 +00:00
2025-05-28 19:37:00 +00:00
for (const testCase of edgeCases) {
const times: number[] = [];
for (let i = 0; i < 100; i++) {
const startTime = performance.now();
const format = FormatDetector.detectFormat(testCase.content);
const endTime = performance.now();
2025-05-25 19:45:37 +00:00
2025-05-28 19:37:00 +00:00
times.push(endTime - startTime);
2025-05-25 19:45:37 +00:00
2025-05-28 19:37:00 +00:00
if (i === 0) {
expect(format).toEqual(testCase.expectedFormat);
2025-05-25 19:45:37 +00:00
}
}
2025-05-28 19:37:00 +00:00
const avg = times.reduce((a, b) => a + b, 0) / times.length;
console.log(`${testCase.name}: avg=${avg.toFixed(3)}ms`);
// Edge cases should be detected very quickly
expect(avg).toBeLessThan(1);
}
});
2025-05-25 19:45:37 +00:00
2025-05-28 19:37:00 +00:00
tap.test('PERF-01: Concurrent detection performance', async () => {
const xmlContent = `<?xml version="1.0" encoding="UTF-8"?>
<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2">
<ID>CONCURRENT-TEST</ID>
</Invoice>`;
2025-05-25 19:45:37 +00:00
2025-05-28 19:37:00 +00:00
const concurrentCount = 10;
const iterations = 5;
2025-05-25 19:45:37 +00:00
2025-05-28 19:37:00 +00:00
for (let iter = 0; iter < iterations; iter++) {
const startTime = performance.now();
// Run multiple detections concurrently
const promises = Array(concurrentCount).fill(null).map(() =>
Promise.resolve(FormatDetector.detectFormat(xmlContent))
);
const results = await Promise.all(promises);
const endTime = performance.now();
const duration = endTime - startTime;
performanceTracker.addMeasurement('concurrent-detection', duration);
// All should detect the same format
expect(results.every(r => r === InvoiceFormat.UBL)).toEqual(true);
console.log(`Concurrent detection (${concurrentCount} parallel): ${duration.toFixed(3)}ms`);
}
2025-05-25 19:45:37 +00:00
2025-05-28 19:37:00 +00:00
const stats = performanceTracker.getStats('concurrent-detection');
if (stats) {
// Concurrent detection should still be fast
expect(stats.avg).toBeLessThan(50);
}
});
tap.test('PERF-01: Memory usage during detection', async () => {
const initialMemory = process.memoryUsage();
2025-05-25 19:45:37 +00:00
2025-05-28 19:37:00 +00:00
// Create a reasonably large test set
const testXmls = Array(1000).fill(null).map((_, i) => `<?xml version="1.0"?>
<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2">
<ID>MEM-TEST-${i}</ID>
<IssueDate>2025-01-25</IssueDate>
</Invoice>`);
2025-05-25 19:45:37 +00:00
2025-05-28 19:37:00 +00:00
// Detect all formats
const startTime = performance.now();
const formats = testXmls.map(xml => FormatDetector.detectFormat(xml));
const endTime = performance.now();
2025-05-25 19:45:37 +00:00
2025-05-28 19:37:00 +00:00
const afterMemory = process.memoryUsage();
const memoryIncrease = (afterMemory.heapUsed - initialMemory.heapUsed) / 1024 / 1024;
2025-05-25 19:45:37 +00:00
2025-05-28 19:37:00 +00:00
console.log(`Detected ${formats.length} formats in ${(endTime - startTime).toFixed(2)}ms`);
console.log(`Memory increase: ${memoryIncrease.toFixed(2)} MB`);
// Memory increase should be reasonable
expect(memoryIncrease).toBeLessThan(50); // Less than 50MB for 1000 detections
2025-05-25 19:45:37 +00:00
2025-05-28 19:37:00 +00:00
// All should be detected as UBL
expect(formats.every(f => f === InvoiceFormat.UBL)).toEqual(true);
});
2025-05-25 19:45:37 +00:00
2025-05-28 19:37:00 +00:00
tap.test('PERF-01: Performance Summary', async () => {
performanceTracker.printSummary();
console.log('\nFormat detection performance tests completed successfully');
2025-05-25 19:45:37 +00:00
});
tap.start();