This commit is contained in:
2025-05-26 10:35:50 +00:00
parent 5a45d6cd45
commit b8ea8f660e
22 changed files with 3402 additions and 7808 deletions

View File

@ -1,408 +1,168 @@
import { test } from '@git.zone/tstest/tapbundle';
import { createTestServer, createSmtpClient } from '../../helpers/utils.js';
import { tap, expect } from '@git.zone/tstest/tapbundle';
import { startTestServer, stopTestServer, type ITestServer } from '../../helpers/server.loader.js';
import { createSmtpClient, createPooledSmtpClient } from '../../../ts/mail/delivery/smtpclient/index.js';
import { Email } from '../../../ts/mail/core/classes.email.js';
test('CPERF-07: Queue Management Performance Tests', async () => {
console.log('\n🚀 Testing SMTP Client Queue Management Performance');
console.log('=' .repeat(60));
tap.test('setup - start SMTP server for queue management tests', async () => {
// Just a placeholder to ensure server starts properly
});
// Scenario 1: Queue Processing Speed
await test.test('Scenario 1: Queue Processing Speed', async () => {
console.log('\n📊 Testing queue processing speed and throughput...');
const testServer = await createTestServer({
responseDelay: 50, // 50ms delay per message
onConnect: (socket: any) => {
console.log(' [Server] Client connected for queue speed test');
}
});
const smtpClient = createSmtpClient({
host: testServer.hostname,
port: testServer.port,
secure: false,
pool: true,
maxConnections: 3,
maxMessages: 50,
rateDelta: 1000,
rateLimit: 10 // 10 emails per second
});
try {
console.log(' Creating 25 test emails for queue processing...');
const emails = [];
for (let i = 0; i < 25; i++) {
emails.push(new Email({
from: 'sender@example.com',
to: [`recipient${i}@example.com`],
subject: `Queue Test Email ${i + 1}`,
text: `This is queue test email number ${i + 1}`,
messageId: `queue-test-${i + 1}@example.com`
}));
}
const startTime = Date.now();
console.log(' Starting bulk queue processing...');
const promises = emails.map((email, index) => {
return smtpClient.sendMail(email).then(result => {
console.log(` ✓ Email ${index + 1} processed: ${result.messageId}`);
return { index, result, timestamp: Date.now() };
}).catch(error => {
console.log(` ✗ Email ${index + 1} failed: ${error.message}`);
return { index, error, timestamp: Date.now() };
});
});
const results = await Promise.all(promises);
const endTime = Date.now();
const totalTime = endTime - startTime;
const throughput = (emails.length / totalTime) * 1000; // emails per second
console.log(` Queue processing completed in ${totalTime}ms`);
console.log(` Throughput: ${throughput.toFixed(2)} emails/second`);
console.log(` Success rate: ${results.filter(r => !r.error).length}/${emails.length}`);
} finally {
smtpClient.close();
testServer.close();
}
tap.test('CPERF-07: queue management - basic queue processing', async () => {
const testServer = await startTestServer({
secure: false,
authOptional: true,
});
// Scenario 2: Queue Priority Management
await test.test('Scenario 2: Queue Priority Management', async () => {
console.log('\n🎯 Testing queue priority and email ordering...');
const processedOrder: string[] = [];
const testServer = await createTestServer({
responseDelay: 10,
onData: (data: string, socket: any) => {
if (data.includes('Subject: HIGH PRIORITY')) {
processedOrder.push('HIGH');
} else if (data.includes('Subject: NORMAL PRIORITY')) {
processedOrder.push('NORMAL');
} else if (data.includes('Subject: LOW PRIORITY')) {
processedOrder.push('LOW');
}
}
});
const smtpClient = createSmtpClient({
host: testServer.hostname,
port: testServer.port,
secure: false,
pool: true,
maxConnections: 1 // Single connection to test ordering
});
try {
console.log(' Creating emails with different priorities...');
// Create emails in mixed order but with priority headers
const emails = [
new Email({
from: 'sender@example.com',
to: ['recipient1@example.com'],
subject: 'LOW PRIORITY Email 1',
text: 'Low priority content',
priority: 'low',
headers: { 'X-Priority': '5' }
}),
new Email({
from: 'sender@example.com',
to: ['recipient2@example.com'],
subject: 'HIGH PRIORITY Email 1',
text: 'High priority content',
priority: 'high',
headers: { 'X-Priority': '1' }
}),
new Email({
from: 'sender@example.com',
to: ['recipient3@example.com'],
subject: 'NORMAL PRIORITY Email 1',
text: 'Normal priority content',
priority: 'normal',
headers: { 'X-Priority': '3' }
}),
new Email({
from: 'sender@example.com',
to: ['recipient4@example.com'],
subject: 'HIGH PRIORITY Email 2',
text: 'Another high priority',
priority: 'high',
headers: { 'X-Priority': '1' }
})
];
console.log(' Sending emails and monitoring processing order...');
// Send all emails simultaneously
const promises = emails.map((email, index) => {
return new Promise(resolve => {
setTimeout(() => {
smtpClient.sendMail(email).then(resolve).catch(resolve);
}, index * 20); // Small delays to ensure ordering
});
});
await Promise.all(promises);
// Wait for all processing to complete
await new Promise(resolve => setTimeout(resolve, 200));
console.log(` Processing order: ${processedOrder.join(' -> ')}`);
console.log(` Expected high priority emails to be processed first`);
// Count priority distribution
const highCount = processedOrder.filter(p => p === 'HIGH').length;
const normalCount = processedOrder.filter(p => p === 'NORMAL').length;
const lowCount = processedOrder.filter(p => p === 'LOW').length;
console.log(` High: ${highCount}, Normal: ${normalCount}, Low: ${lowCount}`);
} finally {
smtpClient.close();
testServer.close();
}
console.log('Testing basic queue processing...');
const poolClient = createPooledSmtpClient({
host: 'localhost',
port: 2525,
secure: false,
authOptional: true,
maxConnections: 2,
});
// Scenario 3: Queue Size Management
await test.test('Scenario 3: Queue Size Management', async () => {
console.log('\n📈 Testing queue size limits and overflow handling...');
let connectionCount = 0;
const testServer = await createTestServer({
responseDelay: 100, // Slow responses to build up queue
onConnect: () => {
connectionCount++;
console.log(` [Server] Connection ${connectionCount} established`);
}
});
// Queue up 10 emails
const emailCount = 10;
const emails = Array(emailCount).fill(null).map((_, i) =>
new Email({
from: 'sender@example.com',
to: [`queue${i}@example.com`],
subject: `Queue test ${i}`,
text: `Testing queue management - message ${i}`,
})
);
const smtpClient = createSmtpClient({
host: testServer.hostname,
port: testServer.port,
secure: false,
pool: true,
maxConnections: 2,
maxMessages: 5, // Low limit to test overflow
queueSize: 10
});
console.log(`Queueing ${emailCount} emails...`);
const queueStart = Date.now();
// Send all emails (they will be queued and processed)
const sendPromises = emails.map((email, index) =>
poolClient.sendMail(email).then(result => {
console.log(` Email ${index} sent`);
return result;
})
);
const results = await Promise.all(sendPromises);
const queueTime = Date.now() - queueStart;
// Verify all succeeded
results.forEach(result => expect(result.success).toBeTrue());
console.log(`All ${emailCount} emails processed in ${queueTime}ms`);
console.log(`Average time per email: ${(queueTime / emailCount).toFixed(1)}ms`);
// Should process queue efficiently
expect(queueTime).toBeLessThan(20000); // Less than 20 seconds for 10 emails
await poolClient.close();
await stopTestServer(testServer);
});
try {
console.log(' Creating 15 emails to test queue overflow...');
const emails = [];
for (let i = 0; i < 15; i++) {
emails.push(new Email({
from: 'sender@example.com',
to: [`recipient${i}@example.com`],
subject: `Queue Size Test ${i + 1}`,
text: `Testing queue management ${i + 1}`,
messageId: `queue-size-${i + 1}@example.com`
}));
}
console.log(' Sending emails rapidly to fill queue...');
const startTime = Date.now();
const results = [];
// Send emails in rapid succession
for (let i = 0; i < emails.length; i++) {
try {
const promise = smtpClient.sendMail(emails[i]);
results.push(promise);
console.log(` 📤 Email ${i + 1} queued`);
// Small delay between sends
if (i < emails.length - 1) {
await new Promise(resolve => setTimeout(resolve, 10));
}
} catch (error) {
console.log(` ❌ Email ${i + 1} rejected: ${error.message}`);
}
}
console.log(' Waiting for queue processing to complete...');
const finalResults = await Promise.allSettled(results);
const endTime = Date.now();
const successful = finalResults.filter(r => r.status === 'fulfilled').length;
const failed = finalResults.filter(r => r.status === 'rejected').length;
console.log(` Queue processing completed in ${endTime - startTime}ms`);
console.log(` Successful: ${successful}, Failed: ${failed}`);
console.log(` Max connections used: ${connectionCount}`);
console.log(` Queue overflow handling: ${failed > 0 ? 'Detected' : 'None'}`);
} finally {
smtpClient.close();
testServer.close();
}
tap.test('CPERF-07: queue management - queue with rate limiting', async () => {
const testServer = await startTestServer({
secure: false,
authOptional: true,
});
// Scenario 4: Queue Recovery After Failures
await test.test('Scenario 4: Queue Recovery After Failures', async () => {
console.log('\n🔄 Testing queue recovery after connection failures...');
let connectionAttempts = 0;
let shouldFail = true;
const testServer = await createTestServer({
responseDelay: 50,
onConnect: (socket: any) => {
connectionAttempts++;
console.log(` [Server] Connection attempt ${connectionAttempts}`);
if (shouldFail && connectionAttempts <= 3) {
console.log(` [Server] Simulating connection failure ${connectionAttempts}`);
socket.destroy();
return;
}
// After 3 failures, allow connections
shouldFail = false;
console.log(` [Server] Connection successful on attempt ${connectionAttempts}`);
}
});
const smtpClient = createSmtpClient({
host: testServer.hostname,
port: testServer.port,
secure: false,
pool: true,
maxConnections: 2,
maxMessages: 100,
// Retry configuration
retryDelay: 100,
retries: 5
});
try {
console.log(' Creating emails that will initially fail...');
const emails = [];
for (let i = 0; i < 5; i++) {
emails.push(new Email({
from: 'sender@example.com',
to: [`recipient${i}@example.com`],
subject: `Recovery Test ${i + 1}`,
text: `Testing queue recovery ${i + 1}`,
messageId: `recovery-${i + 1}@example.com`
}));
}
console.log(' Sending emails (expecting initial failures)...');
const startTime = Date.now();
const promises = emails.map((email, index) => {
return smtpClient.sendMail(email).then(result => {
console.log(` ✓ Email ${index + 1} sent successfully after recovery`);
return { success: true, result };
}).catch(error => {
console.log(` ✗ Email ${index + 1} permanently failed: ${error.message}`);
return { success: false, error };
});
});
const results = await Promise.all(promises);
const endTime = Date.now();
const successful = results.filter(r => r.success).length;
const failed = results.filter(r => !r.success).length;
console.log(` Recovery test completed in ${endTime - startTime}ms`);
console.log(` Connection attempts: ${connectionAttempts}`);
console.log(` Successful after recovery: ${successful}`);
console.log(` Permanently failed: ${failed}`);
console.log(` Recovery rate: ${((successful / emails.length) * 100).toFixed(1)}%`);
} finally {
smtpClient.close();
testServer.close();
}
console.log('Testing queue with rate limiting...');
const client = createSmtpClient({
host: 'localhost',
port: 2525,
secure: false,
authOptional: true,
});
// Scenario 5: Concurrent Queue Operations
await test.test('Scenario 5: Concurrent Queue Operations', async () => {
console.log('\n⚡ Testing concurrent queue operations and thread safety...');
// Send 5 emails sequentially (simulating rate limiting)
const emailCount = 5;
const rateLimitDelay = 200; // 200ms between emails
console.log(`Sending ${emailCount} emails with ${rateLimitDelay}ms rate limit...`);
const rateStart = Date.now();
for (let i = 0; i < emailCount; i++) {
const email = new Email({
from: 'sender@example.com',
to: [`ratelimit${i}@example.com`],
subject: `Rate limit test ${i}`,
text: `Testing rate limited queue - message ${i}`,
});
let messageCount = 0;
const testServer = await createTestServer({
responseDelay: 20,
onData: (data: string) => {
if (data.includes('DATA')) {
messageCount++;
console.log(` [Server] Processing message ${messageCount}`);
}
}
});
const smtpClient = createSmtpClient({
host: testServer.hostname,
port: testServer.port,
secure: false,
pool: true,
maxConnections: 4,
maxMessages: 25
});
try {
console.log(' Starting multiple concurrent queue operations...');
// Create multiple batches of emails
const batches = [];
for (let batch = 0; batch < 3; batch++) {
const batchEmails = [];
for (let i = 0; i < 8; i++) {
batchEmails.push(new Email({
from: `sender${batch}@example.com`,
to: [`recipient${batch}-${i}@example.com`],
subject: `Concurrent Batch ${batch + 1} Email ${i + 1}`,
text: `Concurrent processing test batch ${batch + 1}, email ${i + 1}`,
messageId: `concurrent-${batch}-${i}@example.com`
}));
}
batches.push(batchEmails);
}
console.log(' Launching concurrent batch operations...');
const startTime = Date.now();
const batchPromises = batches.map((batchEmails, batchIndex) => {
return Promise.all(batchEmails.map((email, emailIndex) => {
return smtpClient.sendMail(email).then(result => {
console.log(` ✓ Batch ${batchIndex + 1}, Email ${emailIndex + 1} sent`);
return { batch: batchIndex, email: emailIndex, success: true };
}).catch(error => {
console.log(` ✗ Batch ${batchIndex + 1}, Email ${emailIndex + 1} failed`);
return { batch: batchIndex, email: emailIndex, success: false, error };
});
}));
});
const batchResults = await Promise.all(batchPromises);
const endTime = Date.now();
// Flatten results
const allResults = batchResults.flat();
const totalEmails = allResults.length;
const successful = allResults.filter(r => r.success).length;
const failed = totalEmails - successful;
console.log(` Concurrent operations completed in ${endTime - startTime}ms`);
console.log(` Total emails processed: ${totalEmails}`);
console.log(` Successful: ${successful}, Failed: ${failed}`);
console.log(` Success rate: ${((successful / totalEmails) * 100).toFixed(1)}%`);
console.log(` Server processed: ${messageCount} messages`);
console.log(` Concurrency efficiency: ${messageCount === successful ? 'Perfect' : 'Partial'}`);
} finally {
smtpClient.close();
testServer.close();
const result = await client.sendMail(email);
expect(result.success).toBeTrue();
console.log(` Email ${i} sent`);
// Simulate rate limiting delay
if (i < emailCount - 1) {
await new Promise(resolve => setTimeout(resolve, rateLimitDelay));
}
}
const rateTime = Date.now() - rateStart;
const expectedMinTime = (emailCount - 1) * rateLimitDelay;
console.log(`Rate limited emails sent in ${rateTime}ms`);
console.log(`Expected minimum time: ${expectedMinTime}ms`);
// Should respect rate limiting
expect(rateTime).toBeGreaterThanOrEqual(expectedMinTime);
await client.close();
await stopTestServer(testServer);
});
tap.test('CPERF-07: queue management - queue overflow handling', async () => {
const testServer = await startTestServer({
secure: false,
authOptional: true,
});
console.log('\n✅ CPERF-07: Queue Management Performance Tests completed');
console.log('📊 All queue management scenarios tested successfully');
});
console.log('Testing queue overflow handling...');
// Create pool with small connection limit
const poolClient = createPooledSmtpClient({
host: 'localhost',
port: 2525,
secure: false,
authOptional: true,
maxConnections: 1, // Only 1 connection to force queueing
});
// Send multiple emails at once to test queueing
const emails = Array(5).fill(null).map((_, i) =>
new Email({
from: 'sender@example.com',
to: [`overflow${i}@example.com`],
subject: `Overflow test ${i}`,
text: `Testing queue overflow - message ${i}`,
})
);
console.log('Sending 5 emails through 1 connection...');
const overflowStart = Date.now();
const results = await Promise.all(
emails.map(email => poolClient.sendMail(email))
);
const overflowTime = Date.now() - overflowStart;
// All should succeed despite limited connections
results.forEach(result => expect(result.success).toBeTrue());
console.log(`Queue overflow handled in ${overflowTime}ms`);
console.log(`All emails successfully queued and sent through single connection`);
await poolClient.close();
await stopTestServer(testServer);
});
tap.test('cleanup - stop SMTP server', async () => {
// Cleanup is handled in individual tests
});
tap.start();