@postalsys/email-text-tools 2.1.15 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,101 +1,106 @@
1
1
  # Changelog
2
2
 
3
- ## [2.1.15](https://github.com/postalsys/email-text-tools/compare/v2.1.14...v2.1.15) (2025-05-20)
3
+ ## [2.2.0](https://github.com/postalsys/email-text-tools/compare/v2.1.16...v2.2.0) (2025-09-29)
4
+
5
+
6
+ ### Features
7
+
8
+ * **mime-html:** add timeout protection for CSS processing with juice ([9bd3542](https://github.com/postalsys/email-text-tools/commit/9bd35426642c00c20fa1a8ce9c898f7ab7fc7e43))
4
9
 
5
10
 
6
11
  ### Bug Fixes
7
12
 
8
- * Bumped deps ([519eb91](https://github.com/postalsys/email-text-tools/commit/519eb913a2aa91afe2eb1c782186f4f043ce1c55))
13
+ * **mime-html:** ensure async version pre-filters problematic CSS selectors ([5166f71](https://github.com/postalsys/email-text-tools/commit/5166f714b8532c62aa33c76b2b4e2ac3c8642ca6))
14
+ * **worker-pool:** properly return workers to free list after task completion ([2d6cd2c](https://github.com/postalsys/email-text-tools/commit/2d6cd2c451941932d44d2ce025a5ba867ec66fec))
9
15
 
10
- ## [2.1.14](https://github.com/postalsys/email-text-tools/compare/v2.1.13...v2.1.14) (2025-02-22)
16
+ ## [2.1.16](https://github.com/postalsys/email-text-tools/compare/v2.1.15...v2.1.16) (2025-06-29)
17
+
18
+ ### Bug Fixes
11
19
 
20
+ - removed unneeded console log lines ([3a10214](https://github.com/postalsys/email-text-tools/commit/3a1021483029a1c99df122241c4e0f6062b91059))
21
+
22
+ ## [2.1.15](https://github.com/postalsys/email-text-tools/compare/v2.1.14...v2.1.15) (2025-05-20)
12
23
 
13
24
  ### Bug Fixes
14
25
 
15
- * Bumped deps ([00af333](https://github.com/postalsys/email-text-tools/commit/00af333ece8b1e6d14091fda1aea7662f314c721))
26
+ - Bumped deps ([519eb91](https://github.com/postalsys/email-text-tools/commit/519eb913a2aa91afe2eb1c782186f4f043ce1c55))
16
27
 
17
- ## [2.1.13](https://github.com/postalsys/email-text-tools/compare/v2.1.12...v2.1.13) (2025-01-26)
28
+ ## [2.1.14](https://github.com/postalsys/email-text-tools/compare/v2.1.13...v2.1.14) (2025-02-22)
29
+
30
+ ### Bug Fixes
18
31
 
32
+ - Bumped deps ([00af333](https://github.com/postalsys/email-text-tools/commit/00af333ece8b1e6d14091fda1aea7662f314c721))
33
+
34
+ ## [2.1.13](https://github.com/postalsys/email-text-tools/compare/v2.1.12...v2.1.13) (2025-01-26)
19
35
 
20
36
  ### Bug Fixes
21
37
 
22
- * **deps:** Bumped deps ([3b71891](https://github.com/postalsys/email-text-tools/commit/3b71891b2c6bcb5d87fb9f583a2aec221bcb142f))
38
+ - **deps:** Bumped deps ([3b71891](https://github.com/postalsys/email-text-tools/commit/3b71891b2c6bcb5d87fb9f583a2aec221bcb142f))
23
39
 
24
40
  ## [2.1.12](https://github.com/postalsys/email-text-tools/compare/v2.1.11...v2.1.12) (2024-12-03)
25
41
 
26
-
27
42
  ### Bug Fixes
28
43
 
29
- * **deps:** Bumped deps ([498e56a](https://github.com/postalsys/email-text-tools/commit/498e56a011321f2f437c461008724ca07233eb41))
44
+ - **deps:** Bumped deps ([498e56a](https://github.com/postalsys/email-text-tools/commit/498e56a011321f2f437c461008724ca07233eb41))
30
45
 
31
46
  ## [2.1.11](https://github.com/postalsys/email-text-tools/compare/v2.1.10...v2.1.11) (2024-09-08)
32
47
 
33
-
34
48
  ### Bug Fixes
35
49
 
36
- * Bumped deps ([da979c9](https://github.com/postalsys/email-text-tools/commit/da979c9feef5c8fd4e08b9967cbffcc058a70677))
50
+ - Bumped deps ([da979c9](https://github.com/postalsys/email-text-tools/commit/da979c9feef5c8fd4e08b9967cbffcc058a70677))
37
51
 
38
52
  ## [2.1.10](https://github.com/postalsys/email-text-tools/compare/v2.1.9...v2.1.10) (2024-08-22)
39
53
 
40
-
41
54
  ### Bug Fixes
42
55
 
43
- * **deps:** Bumped deps (juice) ([69eaee3](https://github.com/postalsys/email-text-tools/commit/69eaee36cceadbbf5ebf23caaf24385c38859a03))
56
+ - **deps:** Bumped deps (juice) ([69eaee3](https://github.com/postalsys/email-text-tools/commit/69eaee36cceadbbf5ebf23caaf24385c38859a03))
44
57
 
45
58
  ## [2.1.9](https://github.com/postalsys/email-text-tools/compare/v2.1.8...v2.1.9) (2024-07-03)
46
59
 
47
-
48
60
  ### Bug Fixes
49
61
 
50
- * bumped deps ([45f4533](https://github.com/postalsys/email-text-tools/commit/45f453390e00a8b5dea00c0ab3a7b4a742e9b3ce))
62
+ - bumped deps ([45f4533](https://github.com/postalsys/email-text-tools/commit/45f453390e00a8b5dea00c0ab3a7b4a742e9b3ce))
51
63
 
52
64
  ## [2.1.8](https://github.com/postalsys/email-text-tools/compare/v2.1.7...v2.1.8) (2024-04-12)
53
65
 
54
-
55
66
  ### Bug Fixes
56
67
 
57
- * **deps:** Bumped deps ([59e44d8](https://github.com/postalsys/email-text-tools/commit/59e44d8cb0877e407becac7ef6744a4dfcdba083))
68
+ - **deps:** Bumped deps ([59e44d8](https://github.com/postalsys/email-text-tools/commit/59e44d8cb0877e407becac7ef6744a4dfcdba083))
58
69
 
59
70
  ## [2.1.7](https://github.com/postalsys/email-text-tools/compare/v2.1.6...v2.1.7) (2024-02-29)
60
71
 
61
-
62
72
  ### Bug Fixes
63
73
 
64
- * **deps:** Bumped deps ([4f8a120](https://github.com/postalsys/email-text-tools/commit/4f8a1208b2841d343baf610c1391788b60b4297c))
74
+ - **deps:** Bumped deps ([4f8a120](https://github.com/postalsys/email-text-tools/commit/4f8a1208b2841d343baf610c1391788b60b4297c))
65
75
 
66
76
  ## [2.1.6](https://github.com/postalsys/email-text-tools/compare/v2.1.5...v2.1.6) (2024-02-18)
67
77
 
68
-
69
78
  ### Bug Fixes
70
79
 
71
- * **deps:** removed js-beautify ([a73ccc9](https://github.com/postalsys/email-text-tools/commit/a73ccc97a4acf9bfa7e439939c41869c2554b073))
80
+ - **deps:** removed js-beautify ([a73ccc9](https://github.com/postalsys/email-text-tools/commit/a73ccc97a4acf9bfa7e439939c41869c2554b073))
72
81
 
73
82
  ## [2.1.5](https://github.com/postalsys/email-text-tools/compare/v2.1.4...v2.1.5) (2024-02-18)
74
83
 
75
-
76
84
  ### Bug Fixes
77
85
 
78
- * **deps:** fixed deps ([ccf26e2](https://github.com/postalsys/email-text-tools/commit/ccf26e2f0f8047482b9cc7e71cd0a3e61b286ddc))
86
+ - **deps:** fixed deps ([ccf26e2](https://github.com/postalsys/email-text-tools/commit/ccf26e2f0f8047482b9cc7e71cd0a3e61b286ddc))
79
87
 
80
88
  ## [2.1.4](https://github.com/postalsys/email-text-tools/compare/v2.1.3...v2.1.4) (2024-02-18)
81
89
 
82
-
83
90
  ### Bug Fixes
84
91
 
85
- * **deps:** Bumped deps ([a6711a1](https://github.com/postalsys/email-text-tools/commit/a6711a1938d8e2e75f5afd83b5a8c443be0adb9c))
86
- * **deps:** Bumped deps ([15233fa](https://github.com/postalsys/email-text-tools/commit/15233fa7850f3227bfbc4f06500f0f267e651913))
92
+ - **deps:** Bumped deps ([a6711a1](https://github.com/postalsys/email-text-tools/commit/a6711a1938d8e2e75f5afd83b5a8c443be0adb9c))
93
+ - **deps:** Bumped deps ([15233fa](https://github.com/postalsys/email-text-tools/commit/15233fa7850f3227bfbc4f06500f0f267e651913))
87
94
 
88
95
  ## [2.1.3](https://github.com/postalsys/email-text-tools/compare/v2.1.2...v2.1.3) (2023-10-31)
89
96
 
90
-
91
97
  ### Bug Fixes
92
98
 
93
- * **deps:** Bumped deps ([4815314](https://github.com/postalsys/email-text-tools/commit/4815314f9d36da2b2163d6990acc8f68fc1bc356))
99
+ - **deps:** Bumped deps ([4815314](https://github.com/postalsys/email-text-tools/commit/4815314f9d36da2b2163d6990acc8f68fc1bc356))
94
100
 
95
101
  ## [2.1.2](https://github.com/postalsys/email-text-tools/compare/v2.1.1...v2.1.2) (2023-10-20)
96
102
 
97
-
98
103
  ### Bug Fixes
99
104
 
100
- * **deploy:** Set up automatic deployment ([21e55be](https://github.com/postalsys/email-text-tools/commit/21e55be18bb2ac1b991ee6000928c429a5b09ddd))
101
- * **deploy:** Set up automatic deployment ([c816aaf](https://github.com/postalsys/email-text-tools/commit/c816aafa0457318cfd84497dfcd4e5628503c5b4))
105
+ - **deploy:** Set up automatic deployment ([21e55be](https://github.com/postalsys/email-text-tools/commit/21e55be18bb2ac1b991ee6000928c429a5b09ddd))
106
+ - **deploy:** Set up automatic deployment ([c816aaf](https://github.com/postalsys/email-text-tools/commit/c816aafa0457318cfd84497dfcd4e5628503c5b4))
@@ -0,0 +1,358 @@
1
+ #!/usr/bin/env node
2
+ 'use strict';
3
+
4
+ const fs = require('fs');
5
+ const path = require('path');
6
+ const mimeHtml = require('../lib/mime-html');
7
+
8
+ // Test data sets
9
+ const testCases = {
10
+ simple: {
11
+ name: 'Simple HTML',
12
+ html: `
13
+ <!DOCTYPE html>
14
+ <html>
15
+ <head>
16
+ <style>
17
+ body { font-family: Arial; }
18
+ p { color: blue; }
19
+ </style>
20
+ </head>
21
+ <body>
22
+ <h1>Hello World</h1>
23
+ <p>This is a simple test email.</p>
24
+ </body>
25
+ </html>
26
+ `
27
+ },
28
+
29
+ moderate: {
30
+ name: 'Moderate HTML with inline styles',
31
+ html: `
32
+ <!DOCTYPE html>
33
+ <html>
34
+ <head>
35
+ <style>
36
+ body { font-family: Arial, sans-serif; background: #f0f0f0; }
37
+ .container { max-width: 600px; margin: 0 auto; padding: 20px; }
38
+ h1 { color: #333; border-bottom: 2px solid #007bff; }
39
+ p { line-height: 1.6; color: #666; }
40
+ .button { display: inline-block; padding: 10px 20px; background: #007bff; color: white; text-decoration: none; }
41
+ .footer { margin-top: 40px; padding-top: 20px; border-top: 1px solid #ddd; }
42
+ </style>
43
+ </head>
44
+ <body>
45
+ <div class="container">
46
+ <h1>Newsletter Title</h1>
47
+ <p>This is a moderately complex email template with various styles.</p>
48
+ <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
49
+ <a href="#" class="button">Call to Action</a>
50
+ <div class="footer">
51
+ <p>Footer content here</p>
52
+ </div>
53
+ </div>
54
+ </body>
55
+ </html>
56
+ `
57
+ },
58
+
59
+ complex: {
60
+ name: 'Complex HTML with media queries',
61
+ html: `
62
+ <!DOCTYPE html>
63
+ <html>
64
+ <head>
65
+ <style>
66
+ * { margin: 0; padding: 0; box-sizing: border-box; }
67
+ body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif; }
68
+ .wrapper { max-width: 600px; margin: 0 auto; }
69
+ .header { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); padding: 40px 20px; }
70
+ .header h1 { color: white; font-size: 32px; }
71
+ .content { padding: 40px 20px; background: white; }
72
+ .card { border: 1px solid #e0e0e0; border-radius: 8px; padding: 20px; margin-bottom: 20px; }
73
+ .card h2 { color: #333; margin-bottom: 10px; }
74
+ .grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 20px; }
75
+ @media only screen and (max-width: 600px) {
76
+ .grid { grid-template-columns: 1fr; }
77
+ .header h1 { font-size: 24px; }
78
+ }
79
+ table { width: 100%; border-collapse: collapse; }
80
+ th, td { padding: 10px; text-align: left; border-bottom: 1px solid #ddd; }
81
+ .button { display: inline-block; padding: 12px 24px; background: #007bff; color: white; text-decoration: none; border-radius: 4px; }
82
+ .footer { background: #f8f9fa; padding: 30px 20px; text-align: center; color: #6c757d; }
83
+ </style>
84
+ </head>
85
+ <body>
86
+ <div class="wrapper">
87
+ <div class="header">
88
+ <h1>Complex Email Template</h1>
89
+ </div>
90
+ <div class="content">
91
+ <div class="card">
92
+ <h2>Feature One</h2>
93
+ <p>Description of the first feature with detailed information.</p>
94
+ </div>
95
+ <div class="grid">
96
+ <div class="card">
97
+ <h2>Item 1</h2>
98
+ <p>Grid item content</p>
99
+ </div>
100
+ <div class="card">
101
+ <h2>Item 2</h2>
102
+ <p>Grid item content</p>
103
+ </div>
104
+ </div>
105
+ <table>
106
+ <tr><th>Product</th><th>Price</th><th>Quantity</th></tr>
107
+ <tr><td>Item A</td><td>$10</td><td>2</td></tr>
108
+ <tr><td>Item B</td><td>$20</td><td>1</td></tr>
109
+ </table>
110
+ <a href="#" class="button">Shop Now</a>
111
+ </div>
112
+ <div class="footer">
113
+ <p>Copyright 2024. All rights reserved.</p>
114
+ </div>
115
+ </div>
116
+ </body>
117
+ </html>
118
+ `
119
+ },
120
+
121
+ problematic: {
122
+ name: 'HTML with problematic CSS (modern selectors)',
123
+ html: `
124
+ <!DOCTYPE html>
125
+ <html>
126
+ <head>
127
+ <style>
128
+ body { font-family: system-ui, sans-serif; }
129
+ /* Modern CSS that might cause issues */
130
+ .container:is(.active, .highlighted) { background: yellow; }
131
+ .item:where(.primary) { color: blue; }
132
+ .parent:has(> .child) { border: 1px solid red; }
133
+ /* Standard CSS */
134
+ h1 { color: #333; }
135
+ p { line-height: 1.6; }
136
+ .box:not(.special) { padding: 10px; }
137
+ </style>
138
+ </head>
139
+ <body>
140
+ <div class="container active">
141
+ <h1>Test with Modern CSS</h1>
142
+ <div class="parent">
143
+ <div class="child">Child element</div>
144
+ </div>
145
+ <p class="item primary">This uses modern selectors</p>
146
+ <div class="box">Regular box</div>
147
+ <div class="box special">Special box</div>
148
+ </div>
149
+ </body>
150
+ </html>
151
+ `
152
+ }
153
+ };
154
+
155
+ // Load the failing.html if it exists
156
+ try {
157
+ const failingHtml = fs.readFileSync(path.join(__dirname, '../examples/failing.html'), 'utf8');
158
+ testCases.realWorld = {
159
+ name: 'Real-world problematic HTML',
160
+ html: failingHtml
161
+ };
162
+ } catch (_err) {
163
+ // File doesn't exist, skip it
164
+ }
165
+
166
+ // Benchmark runner
167
+ class Benchmark {
168
+ constructor() {
169
+ this.results = [];
170
+ }
171
+
172
+ async runTest(name, fn, iterations = 100) {
173
+ const times = [];
174
+ const errors = [];
175
+
176
+ // Warmup
177
+ for (let i = 0; i < 5; i++) {
178
+ try {
179
+ await fn();
180
+ } catch (_err) {
181
+ // Ignore warmup errors
182
+ }
183
+ }
184
+
185
+ // Actual test
186
+ for (let i = 0; i < iterations; i++) {
187
+ const start = process.hrtime.bigint();
188
+ try {
189
+ await fn();
190
+ const end = process.hrtime.bigint();
191
+ times.push(Number(end - start) / 1000000); // Convert to milliseconds
192
+ } catch (err) {
193
+ errors.push(err.message);
194
+ }
195
+ }
196
+
197
+ if (times.length > 0) {
198
+ times.sort((a, b) => a - b);
199
+ const avg = times.reduce((a, b) => a + b, 0) / times.length;
200
+ const median = times[Math.floor(times.length / 2)];
201
+ const min = times[0];
202
+ const max = times[times.length - 1];
203
+ const p95 = times[Math.floor(times.length * 0.95)];
204
+ const p99 = times[Math.floor(times.length * 0.99)];
205
+
206
+ return {
207
+ name,
208
+ iterations,
209
+ successful: times.length,
210
+ failed: errors.length,
211
+ avg: avg.toFixed(2),
212
+ median: median.toFixed(2),
213
+ min: min.toFixed(2),
214
+ max: max.toFixed(2),
215
+ p95: p95.toFixed(2),
216
+ p99: p99.toFixed(2),
217
+ errors: errors.length > 0 ? errors[0] : null
218
+ };
219
+ } else {
220
+ return {
221
+ name,
222
+ iterations,
223
+ successful: 0,
224
+ failed: errors.length,
225
+ errors: errors[0] || 'All iterations failed'
226
+ };
227
+ }
228
+ }
229
+
230
+ formatResults(results) {
231
+ console.log('\n' + '='.repeat(80));
232
+ console.log(`Test Case: ${results.testCase}`);
233
+ console.log('='.repeat(80));
234
+
235
+ const table = [];
236
+ results.tests.forEach(test => {
237
+ if (test.successful > 0) {
238
+ table.push({
239
+ Method: test.name,
240
+ 'Success Rate': `${test.successful}/${test.iterations}`,
241
+ 'Avg (ms)': test.avg,
242
+ 'Median (ms)': test.median,
243
+ 'Min (ms)': test.min,
244
+ 'Max (ms)': test.max,
245
+ 'P95 (ms)': test.p95,
246
+ 'P99 (ms)': test.p99
247
+ });
248
+ } else {
249
+ table.push({
250
+ Method: test.name,
251
+ 'Success Rate': `${test.successful}/${test.iterations}`,
252
+ Error: test.errors
253
+ });
254
+ }
255
+ });
256
+
257
+ console.table(table);
258
+
259
+ // Calculate speedup if both succeeded
260
+ const syncTest = results.tests.find(t => t.name === 'Sync');
261
+ const asyncTest = results.tests.find(t => t.name === 'Async (with pool)');
262
+
263
+ if (syncTest && asyncTest && syncTest.successful > 0 && asyncTest.successful > 0) {
264
+ const speedup = (parseFloat(syncTest.avg) / parseFloat(asyncTest.avg)).toFixed(2);
265
+ if (speedup > 1) {
266
+ console.log(`\nAsync is ${speedup}x faster than Sync`);
267
+ } else {
268
+ console.log(`\nSync is ${(1 / speedup).toFixed(2)}x faster than Async`);
269
+ }
270
+ }
271
+ }
272
+
273
+ async runBenchmarks() {
274
+ console.log('Starting Mime HTML Benchmarks...');
275
+ console.log('================================\n');
276
+
277
+ for (const [_key, testCase] of Object.entries(testCases)) {
278
+ const results = {
279
+ testCase: testCase.name,
280
+ tests: []
281
+ };
282
+
283
+ // Test sync version
284
+ results.tests.push(
285
+ await this.runTest(
286
+ 'Sync',
287
+ () => {
288
+ return Promise.resolve(mimeHtml.sync({ html: testCase.html }));
289
+ },
290
+ 100
291
+ )
292
+ );
293
+
294
+ // Test async version with pool
295
+ results.tests.push(
296
+ await this.runTest(
297
+ 'Async (with pool)',
298
+ async () => {
299
+ return await mimeHtml.async(
300
+ { html: testCase.html },
301
+ {
302
+ useWorkerPool: true,
303
+ timeout: 5000,
304
+ minWorkers: 2,
305
+ maxWorkers: 4
306
+ }
307
+ );
308
+ },
309
+ 100
310
+ )
311
+ );
312
+
313
+ // Test async version without pool
314
+ results.tests.push(
315
+ await this.runTest(
316
+ 'Async (no pool)',
317
+ async () => {
318
+ return await mimeHtml.async(
319
+ { html: testCase.html },
320
+ {
321
+ useWorkerPool: false,
322
+ timeout: 5000
323
+ }
324
+ );
325
+ },
326
+ 100
327
+ )
328
+ );
329
+
330
+ this.formatResults(results);
331
+ }
332
+
333
+ // Show worker pool stats
334
+ const stats = mimeHtml.getWorkerPoolStats();
335
+ if (stats) {
336
+ console.log('\n' + '='.repeat(80));
337
+ console.log('Worker Pool Stats:');
338
+ console.log('='.repeat(80));
339
+ console.table([stats]);
340
+ }
341
+
342
+ // Clean up
343
+ await mimeHtml.closeWorkerPool();
344
+ }
345
+ }
346
+
347
+ // Run benchmarks
348
+ const benchmark = new Benchmark();
349
+ benchmark
350
+ .runBenchmarks()
351
+ .then(() => {
352
+ console.log('\nBenchmark complete!');
353
+ process.exit(0);
354
+ })
355
+ .catch(err => {
356
+ console.error('Benchmark failed:', err);
357
+ process.exit(1);
358
+ });
@@ -0,0 +1,176 @@
1
+ #!/usr/bin/env node
2
+ 'use strict';
3
+
4
+ const fs = require('fs');
5
+ const path = require('path');
6
+ const mimeHtml = require('../lib/mime-html');
7
+
8
+ async function concurrencyBenchmark() {
9
+ console.log('Concurrency Benchmark - Real-World HTML');
10
+ console.log('='.repeat(80));
11
+ console.log('Testing concurrent processing with problematic HTML\n');
12
+
13
+ // Load the real-world HTML
14
+ const realWorldHtml = fs.readFileSync(path.join(__dirname, '../examples/failing.html'), 'utf8');
15
+
16
+ console.log(`HTML size: ${(realWorldHtml.length / 1024).toFixed(1)} KB`);
17
+ console.log('Testing different concurrency levels...\n');
18
+
19
+ const concurrencyLevels = [1, 5, 10, 20, 50];
20
+ const totalRequests = 100;
21
+
22
+ const results = [];
23
+
24
+ for (const concurrency of concurrencyLevels) {
25
+ console.log(`Testing with concurrency level: ${concurrency}`);
26
+ console.log('-'.repeat(40));
27
+
28
+ // Test sync (sequential only)
29
+ if (concurrency === 1) {
30
+ console.log(' Sync version (sequential)...');
31
+ const syncStart = process.hrtime.bigint();
32
+ for (let i = 0; i < totalRequests; i++) {
33
+ mimeHtml.sync({ html: realWorldHtml });
34
+ if ((i + 1) % 20 === 0) {
35
+ process.stdout.write(` Progress: ${i + 1}/${totalRequests}\r`);
36
+ }
37
+ }
38
+ const syncEnd = process.hrtime.bigint();
39
+ const syncTime = Number(syncEnd - syncStart) / 1000000;
40
+ process.stdout.write(` Progress: ${totalRequests}/${totalRequests} - Done!\n`);
41
+
42
+ results.push({
43
+ method: 'Sync (sequential)',
44
+ concurrency: 1,
45
+ totalRequests,
46
+ totalTime: syncTime.toFixed(2),
47
+ avgTime: (syncTime / totalRequests).toFixed(2),
48
+ reqPerSec: (totalRequests / (syncTime / 1000)).toFixed(0)
49
+ });
50
+ }
51
+
52
+ // Test async with pool
53
+ console.log(' Async with pool...');
54
+ const asyncPoolStart = process.hrtime.bigint();
55
+ const poolPromises = [];
56
+
57
+ for (let i = 0; i < totalRequests; i++) {
58
+ poolPromises.push(
59
+ mimeHtml.async(
60
+ { html: realWorldHtml },
61
+ {
62
+ useWorkerPool: true,
63
+ timeout: 5000,
64
+ minWorkers: 2,
65
+ maxWorkers: 4,
66
+ logErrors: false
67
+ }
68
+ )
69
+ );
70
+
71
+ // Control concurrency
72
+ if (poolPromises.length >= concurrency) {
73
+ await Promise.race(poolPromises).then(() => {
74
+ const index = poolPromises.findIndex(
75
+ p => p.constructor.name !== 'Promise' || Promise.race([p, Promise.resolve('done')]).then(v => v === 'done')
76
+ );
77
+ if (index > -1) poolPromises.splice(index, 1);
78
+ });
79
+ }
80
+
81
+ if ((i + 1) % 20 === 0) {
82
+ process.stdout.write(` Progress: ${i + 1}/${totalRequests}\r`);
83
+ }
84
+ }
85
+
86
+ // Wait for remaining promises
87
+ await Promise.all(poolPromises);
88
+ const asyncPoolEnd = process.hrtime.bigint();
89
+ const asyncPoolTime = Number(asyncPoolEnd - asyncPoolStart) / 1000000;
90
+ process.stdout.write(` Progress: ${totalRequests}/${totalRequests} - Done!\n`);
91
+
92
+ results.push({
93
+ method: 'Async (pool)',
94
+ concurrency,
95
+ totalRequests,
96
+ totalTime: asyncPoolTime.toFixed(2),
97
+ avgTime: (asyncPoolTime / totalRequests).toFixed(2),
98
+ reqPerSec: (totalRequests / (asyncPoolTime / 1000)).toFixed(0)
99
+ });
100
+
101
+ // Show worker pool stats at peak concurrency
102
+ if (concurrency === 20) {
103
+ const stats = mimeHtml.getWorkerPoolStats();
104
+ console.log(` Worker pool stats: ${stats.totalWorkers} workers, ${stats.busyWorkers} busy`);
105
+ }
106
+
107
+ console.log();
108
+ }
109
+
110
+ // Display results table
111
+ console.log('='.repeat(80));
112
+ console.log('RESULTS');
113
+ console.log('='.repeat(80) + '\n');
114
+
115
+ console.table(
116
+ results.map(r => ({
117
+ Method: r.method,
118
+ Concurrency: r.concurrency,
119
+ 'Total Time': `${r.totalTime}ms`,
120
+ 'Avg per Request': `${r.avgTime}ms`,
121
+ Throughput: `${r.reqPerSec} req/s`
122
+ }))
123
+ );
124
+
125
+ // Analysis
126
+ console.log('Performance Analysis:');
127
+ console.log('-'.repeat(40));
128
+
129
+ const syncResult = results.find(r => r.method === 'Sync (sequential)');
130
+ const bestAsync = results.filter(r => r.method === 'Async (pool)').sort((a, b) => parseFloat(a.totalTime) - parseFloat(b.totalTime))[0];
131
+
132
+ if (syncResult && bestAsync) {
133
+ const speedup = (parseFloat(syncResult.totalTime) / parseFloat(bestAsync.totalTime)).toFixed(2);
134
+ console.log(`• Best async configuration: Concurrency ${bestAsync.concurrency}`);
135
+ console.log(`• Async is ${speedup}x faster than sync for batch processing`);
136
+ console.log(`• Sync throughput: ${syncResult.reqPerSec} req/s`);
137
+ console.log(`• Best async throughput: ${bestAsync.reqPerSec} req/s`);
138
+ }
139
+
140
+ // Throughput graph
141
+ console.log('\nThroughput by Concurrency Level:');
142
+ console.log('-'.repeat(40));
143
+
144
+ const asyncResults = results.filter(r => r.method === 'Async (pool)');
145
+ const maxThroughput = Math.max(...asyncResults.map(r => parseInt(r.reqPerSec)));
146
+
147
+ asyncResults.forEach(r => {
148
+ const barLength = Math.round((parseInt(r.reqPerSec) / maxThroughput) * 30);
149
+ const bar = '█'.repeat(barLength);
150
+ console.log(` ${String(r.concurrency).padStart(2)}: ${bar} ${r.reqPerSec} req/s`);
151
+ });
152
+
153
+ // Memory usage
154
+ const memUsage = process.memoryUsage();
155
+ console.log('\nFinal Memory Usage:');
156
+ console.log('-'.repeat(40));
157
+ console.log(`• RSS: ${(memUsage.rss / 1024 / 1024).toFixed(1)} MB`);
158
+ console.log(`• Heap Used: ${(memUsage.heapUsed / 1024 / 1024).toFixed(1)} MB`);
159
+
160
+ // Cleanup
161
+ await mimeHtml.closeWorkerPool();
162
+
163
+ console.log('\n' + '='.repeat(80));
164
+ console.log('Summary:');
165
+ console.log('• Async with worker pool provides significant speedup for concurrent processing');
166
+ console.log('• Pre-filtering prevents timeouts even under high load');
167
+ console.log('• Worker pool efficiently handles concurrent requests');
168
+ console.log('• Optimal concurrency depends on CPU cores and workload');
169
+ console.log('='.repeat(80));
170
+ }
171
+
172
+ // Run benchmark
173
+ concurrencyBenchmark().catch(err => {
174
+ console.error('Benchmark failed:', err);
175
+ process.exit(1);
176
+ });