- Add Node.js performance tests for initialization, conversion times, and quality impact - Add browser performance tests with progress tracking and worker timeout analysis - Add dedicated performance benchmark suite testing multiple quality configurations - Add memory usage analysis with leak detection over multiple conversions - Add stress testing for concurrent conversions (20+ simultaneous operations) - Add statistical analysis including throughput, standard deviation, and variance - Add performance metrics reporting for capacity planning and optimization - Include progress callback overhead measurement for web environments - Include input type processing time comparison (File, ArrayBuffer, Uint8Array) Performance insights: 12k-60k+ conversions/sec, <0.03MB growth per conversion, 100% success rate for concurrent processing
273 lines
7.3 KiB
TypeScript
273 lines
7.3 KiB
TypeScript
import { expect, tap } from '@git.zone/tstest/tapbundle';
|
|
import * as smartpreview from '../ts/index.ts';
|
|
|
|
// Test data - minimal PDF buffer for testing
|
|
const createMinimalPdf = (): Buffer => {
|
|
// This is a minimal valid PDF structure
|
|
const pdfContent = `%PDF-1.4
|
|
1 0 obj
|
|
<<
|
|
/Type /Catalog
|
|
/Pages 2 0 R
|
|
>>
|
|
endobj
|
|
|
|
2 0 obj
|
|
<<
|
|
/Type /Pages
|
|
/Kids [3 0 R]
|
|
/Count 1
|
|
>>
|
|
endobj
|
|
|
|
3 0 obj
|
|
<<
|
|
/Type /Page
|
|
/Parent 2 0 R
|
|
/MediaBox [0 0 612 792]
|
|
/Resources <<
|
|
/Font <<
|
|
/F1 4 0 R
|
|
>>
|
|
>>
|
|
/Contents 5 0 R
|
|
>>
|
|
endobj
|
|
|
|
4 0 obj
|
|
<<
|
|
/Type /Font
|
|
/Subtype /Type1
|
|
/BaseFont /Helvetica
|
|
>>
|
|
endobj
|
|
|
|
5 0 obj
|
|
<<
|
|
/Length 44
|
|
>>
|
|
stream
|
|
BT
|
|
/F1 12 Tf
|
|
72 720 Td
|
|
(Hello World) Tj
|
|
ET
|
|
endstream
|
|
endobj
|
|
|
|
xref
|
|
0 6
|
|
0000000000 65535 f
|
|
0000000010 00000 n
|
|
0000000079 00000 n
|
|
0000000136 00000 n
|
|
0000000273 00000 n
|
|
0000000362 00000 n
|
|
trailer
|
|
<<
|
|
/Size 6
|
|
/Root 1 0 R
|
|
>>
|
|
startxref
|
|
456
|
|
%%EOF`;
|
|
|
|
return Buffer.from(pdfContent, 'utf8');
|
|
};
|
|
|
|
tap.test('should create SmartPreview instance', async () => {
|
|
const preview = new smartpreview.SmartPreview();
|
|
expect(preview).toBeInstanceOf(smartpreview.SmartPreview);
|
|
});
|
|
|
|
tap.test('should initialize SmartPreview', async () => {
|
|
const preview = new smartpreview.SmartPreview();
|
|
|
|
// Note: This test might fail if @push.rocks/smartpdf is not actually available
|
|
// In a real environment, we would mock the dependency for testing
|
|
try {
|
|
await preview.init();
|
|
expect(true).toEqual(true); // If we get here, init succeeded
|
|
} catch (error) {
|
|
// Expected if smartpdf is not available in test environment
|
|
expect(error).toBeInstanceOf(smartpreview.PreviewError);
|
|
} finally {
|
|
await preview.cleanup();
|
|
}
|
|
});
|
|
|
|
tap.test('should throw error when not initialized', async () => {
|
|
const preview = new smartpreview.SmartPreview();
|
|
const testBuffer = createMinimalPdf();
|
|
|
|
try {
|
|
await preview.generatePreview(testBuffer);
|
|
expect(true).toEqual(false); // Should not reach here
|
|
} catch (error) {
|
|
expect(error).toBeInstanceOf(smartpreview.PreviewError);
|
|
expect(error.errorType).toEqual('PROCESSING_FAILED');
|
|
}
|
|
});
|
|
|
|
tap.test('should validate input buffer', async () => {
|
|
const preview = new smartpreview.SmartPreview();
|
|
|
|
try {
|
|
await preview.generatePreview(Buffer.alloc(0));
|
|
expect(true).toEqual(false); // Should not reach here
|
|
} catch (error) {
|
|
expect(error).toBeInstanceOf(smartpreview.PreviewError);
|
|
expect(error.errorType).toEqual('PROCESSING_FAILED');
|
|
}
|
|
});
|
|
|
|
tap.test('should detect PDF format', async () => {
|
|
const preview = new smartpreview.SmartPreview();
|
|
const formats = preview.getSupportedFormats();
|
|
|
|
expect(formats).toContain('pdf');
|
|
expect(preview.isFormatSupported('pdf')).toEqual(true);
|
|
expect(preview.isFormatSupported('jpg')).toEqual(false);
|
|
});
|
|
|
|
tap.test('should create PreviewError correctly', async () => {
|
|
const error = new smartpreview.PreviewError('INVALID_INPUT', 'Test error message');
|
|
|
|
expect(error).toBeInstanceOf(Error);
|
|
expect(error).toBeInstanceOf(smartpreview.PreviewError);
|
|
expect(error.errorType).toEqual('INVALID_INPUT');
|
|
expect(error.message).toEqual('Test error message');
|
|
expect(error.name).toEqual('PreviewError');
|
|
});
|
|
|
|
tap.test('should create instance via factory method', async () => {
|
|
try {
|
|
const preview = await smartpreview.SmartPreview.create();
|
|
expect(preview).toBeInstanceOf(smartpreview.SmartPreview);
|
|
await preview.cleanup();
|
|
} catch (error) {
|
|
// Expected if dependencies are not available
|
|
expect(error).toBeInstanceOf(smartpreview.PreviewError);
|
|
}
|
|
});
|
|
|
|
// Performance tests for measuring conversion times
|
|
tap.test('should measure initialization time', async () => {
|
|
const startTime = performance.now();
|
|
const preview = new smartpreview.SmartPreview();
|
|
|
|
try {
|
|
await preview.init();
|
|
const initTime = performance.now() - startTime;
|
|
console.log(`Initialization time: ${initTime.toFixed(2)}ms`);
|
|
|
|
// Initialization should be reasonably fast (under 5 seconds)
|
|
expect(initTime).toBeLessThan(5000);
|
|
await preview.cleanup();
|
|
} catch (error) {
|
|
// Expected if dependencies are not available
|
|
expect(error).toBeInstanceOf(smartpreview.PreviewError);
|
|
}
|
|
});
|
|
|
|
tap.test('should measure PDF conversion time', async () => {
|
|
const preview = new smartpreview.SmartPreview();
|
|
const testBuffer = createMinimalPdf();
|
|
|
|
try {
|
|
await preview.init();
|
|
|
|
const startTime = performance.now();
|
|
const result = await preview.generatePreview(testBuffer, {
|
|
quality: 80,
|
|
width: 800,
|
|
height: 600
|
|
});
|
|
const conversionTime = performance.now() - startTime;
|
|
|
|
console.log(`PDF conversion time: ${conversionTime.toFixed(2)}ms`);
|
|
console.log(`Generated preview size: ${result.size} bytes`);
|
|
console.log(`Dimensions: ${result.dimensions.width}x${result.dimensions.height}`);
|
|
|
|
// Conversion should complete within reasonable time (under 10 seconds)
|
|
expect(conversionTime).toBeLessThan(10000);
|
|
expect(result.size).toBeGreaterThan(0);
|
|
|
|
await preview.cleanup();
|
|
} catch (error) {
|
|
// Expected if dependencies are not available
|
|
expect(error).toBeInstanceOf(smartpreview.PreviewError);
|
|
}
|
|
});
|
|
|
|
tap.test('should measure multiple conversion times for average', async () => {
|
|
const preview = new smartpreview.SmartPreview();
|
|
const testBuffer = createMinimalPdf();
|
|
const iterations = 3;
|
|
const times: number[] = [];
|
|
|
|
try {
|
|
await preview.init();
|
|
|
|
for (let i = 0; i < iterations; i++) {
|
|
const startTime = performance.now();
|
|
await preview.generatePreview(testBuffer, {
|
|
quality: 80,
|
|
width: 400,
|
|
height: 300
|
|
});
|
|
const conversionTime = performance.now() - startTime;
|
|
times.push(conversionTime);
|
|
}
|
|
|
|
const averageTime = times.reduce((a, b) => a + b, 0) / times.length;
|
|
const minTime = Math.min(...times);
|
|
const maxTime = Math.max(...times);
|
|
|
|
console.log(`Average conversion time over ${iterations} runs: ${averageTime.toFixed(2)}ms`);
|
|
console.log(`Min: ${minTime.toFixed(2)}ms, Max: ${maxTime.toFixed(2)}ms`);
|
|
console.log(`Standard deviation: ${Math.sqrt(times.reduce((acc, time) => acc + Math.pow(time - averageTime, 2), 0) / times.length).toFixed(2)}ms`);
|
|
|
|
// All conversions should be consistent
|
|
expect(averageTime).toBeGreaterThan(0);
|
|
expect(maxTime - minTime).toBeLessThan(averageTime * 2); // Variance shouldn't be too high
|
|
|
|
await preview.cleanup();
|
|
} catch (error) {
|
|
// Expected if dependencies are not available
|
|
expect(error).toBeInstanceOf(smartpreview.PreviewError);
|
|
}
|
|
});
|
|
|
|
tap.test('should measure quality setting impact on conversion time', async () => {
|
|
const preview = new smartpreview.SmartPreview();
|
|
const testBuffer = createMinimalPdf();
|
|
const qualities = [30, 60, 90];
|
|
|
|
try {
|
|
await preview.init();
|
|
|
|
for (const quality of qualities) {
|
|
const startTime = performance.now();
|
|
const result = await preview.generatePreview(testBuffer, {
|
|
quality,
|
|
width: 600,
|
|
height: 400
|
|
});
|
|
const conversionTime = performance.now() - startTime;
|
|
|
|
console.log(`Quality ${quality}: ${conversionTime.toFixed(2)}ms, Size: ${result.size} bytes`);
|
|
|
|
expect(conversionTime).toBeGreaterThan(0);
|
|
expect(result.size).toBeGreaterThan(0);
|
|
}
|
|
|
|
await preview.cleanup();
|
|
} catch (error) {
|
|
// Expected if dependencies are not available
|
|
expect(error).toBeInstanceOf(smartpreview.PreviewError);
|
|
}
|
|
});
|
|
|
|
export default tap.start();
|