Repository navigation
Expand file tree
/
Copy pathbenchmark.mjs
More file actions
169 lines (138 loc) · 6.47 KB
/
Copy pathbenchmark.mjs
File metadata and controls
169 lines (138 loc) · 6.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env node
/**
* LibreOffice WASM Benchmark Script
*
* Measures:
* - First initialization time
* - DOCX → PDF conversion (multiple runs for average)
* - XLSX → PDF conversion
* - PPTX → PDF conversion
*/
import { createConverter } from './dist/index.js';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
// Configuration
const ITERATIONS = 5; // Number of conversion iterations per format
// Test files
const TEST_FILES = {
docx: 'tests/sample_2_page.docx', // Smaller DOCX for consistent benchmarks
xlsx: 'tests/sample_test_5.xlsx',
pptx: 'tests/sample_test_1.pptx',
};
// Track all results
const results = {
initialization: null,
docx: [],
xlsx: [],
pptx: [],
};
function formatTime(ms) {
if (ms < 1000) return `${ms.toFixed(0)}ms`;
return `${(ms / 1000).toFixed(2)}s`;
}
function calculateStats(times) {
if (times.length === 0) return null;
const sorted = [...times].sort((a, b) => a - b);
const min = sorted[0];
const max = sorted[sorted.length - 1];
const avg = times.reduce((a, b) => a + b, 0) / times.length;
const median = sorted[Math.floor(sorted.length / 2)];
return { min, max, avg, median };
}
console.log('╔════════════════════════════════════════════════════════════════╗');
console.log('║ LibreOffice WASM Benchmark ║');
console.log('╚════════════════════════════════════════════════════════════════╝');
console.log('');
console.log(`Configuration: ${ITERATIONS} iterations per format`);
console.log('');
// Phase 1: Initialization
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
console.log('Phase 1: Initialization');
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
const initStart = Date.now();
const converter = await createConverter({
wasmPath: path.join(__dirname, 'wasm'),
verbose: false,
onProgress: (info) => {
process.stdout.write(`\r [${info.phase}] ${info.percent}% - ${info.message}`.padEnd(80));
},
});
results.initialization = Date.now() - initStart;
console.log(`\n Initialization complete: ${formatTime(results.initialization)}`);
console.log('');
// Helper function to run conversion benchmark
async function benchmarkConversion(name, filePath, iterations) {
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
console.log(`Phase: ${name.toUpperCase()} → PDF`);
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
const fullPath = path.join(__dirname, filePath);
if (!fs.existsSync(fullPath)) {
console.log(` ⚠️ File not found: ${filePath}`);
return [];
}
const inputData = fs.readFileSync(fullPath);
const inputName = path.basename(filePath);
console.log(` Input: ${inputName} (${(inputData.length / 1024).toFixed(1)} KB)`);
console.log('');
const times = [];
for (let i = 0; i < iterations; i++) {
process.stdout.write(` Run ${i + 1}/${iterations}... `);
try {
const start = Date.now();
const result = await converter.convert(inputData, {
outputFormat: 'pdf',
}, inputName);
const elapsed = Date.now() - start;
times.push(elapsed);
console.log(`${formatTime(elapsed)} (output: ${(result.data.length / 1024).toFixed(1)} KB)`);
} catch (error) {
console.log(`ERROR: ${error.message}`);
}
}
if (times.length > 0) {
const stats = calculateStats(times);
console.log('');
console.log(` Results: min=${formatTime(stats.min)}, max=${formatTime(stats.max)}, avg=${formatTime(stats.avg)}, median=${formatTime(stats.median)}`);
}
console.log('');
return times;
}
// Phase 2: DOCX → PDF
results.docx = await benchmarkConversion('docx', TEST_FILES.docx, ITERATIONS);
// Phase 3: XLSX → PDF
results.xlsx = await benchmarkConversion('xlsx', TEST_FILES.xlsx, ITERATIONS);
// Phase 4: PPTX → PDF
results.pptx = await benchmarkConversion('pptx', TEST_FILES.pptx, ITERATIONS);
// Final Summary
console.log('╔════════════════════════════════════════════════════════════════╗');
console.log('║ BENCHMARK RESULTS ║');
console.log('╚════════════════════════════════════════════════════════════════╝');
console.log('');
console.log('| Operation | Min | Max | Avg | Median |');
console.log('|-----------|-----|-----|-----|--------|');
console.log(`| Initialization | - | - | ${formatTime(results.initialization)} | - |`);
for (const [name, times] of Object.entries({ docx: results.docx, xlsx: results.xlsx, pptx: results.pptx })) {
if (times.length > 0) {
const stats = calculateStats(times);
console.log(`| ${name.toUpperCase()} → PDF | ${formatTime(stats.min)} | ${formatTime(stats.max)} | ${formatTime(stats.avg)} | ${formatTime(stats.median)} |`);
}
}
console.log('');
console.log('README-ready format:');
console.log('');
console.log('| Operation | Time |');
console.log('|-----------|------|');
console.log(`| First initialization | ~${formatTime(results.initialization)} |`);
for (const [name, times] of Object.entries({ docx: results.docx, xlsx: results.xlsx, pptx: results.pptx })) {
if (times.length > 0) {
const stats = calculateStats(times);
const label = name === 'docx' ? 'DOCX → PDF' : name === 'xlsx' ? 'XLSX → PDF' : 'PPTX → PDF';
console.log(`| ${label} | ~${formatTime(stats.avg)} |`);
}
}
console.log('');
// Cleanup
await converter.destroy();
process.exit(0);