dcrouter/test/suite/smtpclient_performance/test.cperf-05.network-efficiency.ts
2025-05-26 10:35:50 +00:00

181 lines
5.0 KiB
TypeScript

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';
tap.test('setup - start SMTP server for network efficiency tests', async () => {
// Just a placeholder to ensure server starts properly
});
tap.test('CPERF-05: network efficiency - connection reuse', async () => {
const testServer = await startTestServer({
secure: false,
authOptional: true,
});
console.log('Testing connection reuse efficiency...');
// Test 1: Individual connections (2 messages)
console.log('Sending 2 messages with individual connections...');
const individualStart = Date.now();
for (let i = 0; i < 2; i++) {
const client = createSmtpClient({
host: 'localhost',
port: 2525,
secure: false,
authOptional: true,
});
const email = new Email({
from: 'sender@example.com',
to: [`recipient${i}@example.com`],
subject: `Test ${i}`,
text: `Message ${i}`,
});
const result = await client.sendMail(email);
expect(result.success).toBeTrue();
await client.close();
}
const individualTime = Date.now() - individualStart;
console.log(`Individual connections: 2 connections, ${individualTime}ms`);
// Test 2: Connection reuse (2 messages)
console.log('Sending 2 messages with connection reuse...');
const reuseStart = Date.now();
const reuseClient = createSmtpClient({
host: 'localhost',
port: 2525,
secure: false,
authOptional: true,
});
for (let i = 0; i < 2; i++) {
const email = new Email({
from: 'sender@example.com',
to: [`reuse${i}@example.com`],
subject: `Reuse ${i}`,
text: `Message ${i}`,
});
const result = await reuseClient.sendMail(email);
expect(result.success).toBeTrue();
}
await reuseClient.close();
const reuseTime = Date.now() - reuseStart;
console.log(`Connection reuse: 1 connection, ${reuseTime}ms`);
// Connection reuse should complete reasonably quickly
expect(reuseTime).toBeLessThan(5000); // Less than 5 seconds
await stopTestServer(testServer);
});
tap.test('CPERF-05: network efficiency - message throughput', async () => {
const testServer = await startTestServer({
secure: false,
authOptional: true,
});
console.log('Testing message throughput...');
const client = createSmtpClient({
host: 'localhost',
port: 2525,
secure: false,
authOptional: true,
});
// Test with different message sizes
const sizes = [1024, 10240]; // 1KB, 10KB
let totalBytes = 0;
const startTime = Date.now();
for (const size of sizes) {
const content = 'x'.repeat(size);
const email = new Email({
from: 'sender@example.com',
to: ['recipient@example.com'],
subject: `Test ${size} bytes`,
text: content,
});
const result = await client.sendMail(email);
expect(result.success).toBeTrue();
totalBytes += size;
}
const elapsed = Date.now() - startTime;
const throughput = (totalBytes / elapsed) * 1000; // bytes per second
console.log(`Total bytes sent: ${totalBytes}`);
console.log(`Time elapsed: ${elapsed}ms`);
console.log(`Throughput: ${(throughput / 1024).toFixed(1)} KB/s`);
// Should achieve reasonable throughput
expect(throughput).toBeGreaterThan(512); // At least 512 bytes/s
await client.close();
await stopTestServer(testServer);
});
tap.test('CPERF-05: network efficiency - concurrent connections', async () => {
const testServer = await startTestServer({
secure: false,
authOptional: true,
});
console.log('Testing concurrent connections...');
// Create pooled client
const poolClient = createPooledSmtpClient({
host: 'localhost',
port: 2525,
secure: false,
authOptional: true,
maxConnections: 2,
});
// Send 4 emails concurrently
const emails = Array(4).fill(null).map((_, i) =>
new Email({
from: 'sender@example.com',
to: [`concurrent${i}@example.com`],
subject: `Concurrent ${i}`,
text: `Testing concurrent connections - message ${i}`,
})
);
console.log('Sending 4 emails through connection pool...');
const poolStart = Date.now();
// Send emails concurrently
const results = await Promise.all(
emails.map(email => poolClient.sendMail(email))
);
results.forEach(result => expect(result.success).toBeTrue());
const poolTime = Date.now() - poolStart;
console.log(`Emails sent: 4`);
console.log(`Total time: ${poolTime}ms`);
console.log(`Average time per email: ${(poolTime / 4).toFixed(1)}ms`);
// Pool should handle multiple emails efficiently
expect(poolTime).toBeLessThan(10000); // Less than 10 seconds total
await poolClient.close();
await stopTestServer(testServer);
});
tap.test('cleanup - stop SMTP server', async () => {
// Cleanup is handled in individual tests
});
tap.start();