demo-optimization.cjsā¢4 kB
#!/usr/bin/env node
/**
* Test Optimization Demonstration
*
* Quick demonstration of the optimized test approach vs traditional approach.
* This script shows the key differences and benefits of the optimization strategy.
*/
const { exec } = require('child_process');
const path = require('path');
console.log('š Test Optimization Demonstration');
console.log('====================================\n');
console.log('š TRADITIONAL APPROACH PATTERN:');
console.log(' ā Each test method makes individual API calls');
console.log(' ā Duplicate data fetching across test cases');
console.log(' ā Higher API rate limit consumption');
console.log(' ā Slower test execution due to network overhead\n');
console.log('š OPTIMIZED APPROACH PATTERN:');
console.log(' ā
Single API call in beforeAll() shared across all tests');
console.log(' ā
Cached data reused for filtering and analysis');
console.log(' ā
Minimal API rate limit consumption');
console.log(' ā
Faster test execution with maintained coverage\n');
console.log('š Running optimized test suite demonstration...\n');
const testCommand = 'npm test -- tests/firewalla/client-device-optimized.test.ts --verbose';
exec(testCommand, { cwd: path.resolve(__dirname, '..') }, (error, stdout, stderr) => {
if (error) {
console.error('ā Test execution failed:', error.message);
return;
}
// Extract key metrics from the output
const output = stdout + stderr;
// Extract performance metrics
const apiCallsMatch = output.match(/Total API calls: (\d+)/);
const testTimeMatch = output.match(/Total test time: (\d+)ms/);
const testsPassedMatch = output.match(/Tests:\s+(\d+) passed/);
const apiCalls = apiCallsMatch ? parseInt(apiCallsMatch[1]) : 'N/A';
const testTime = testTimeMatch ? parseInt(testTimeMatch[1]) : 'N/A';
const testsPassed = testsPassedMatch ? parseInt(testsPassedMatch[1]) : 'N/A';
console.log('š OPTIMIZATION RESULTS:');
console.log('========================');
console.log(`ā
Tests Passed: ${testsPassed}`);
console.log(`ā” API Calls Used: ${apiCalls} (shared load)`);
console.log(`š Execution Time: ${testTime}ms`);
console.log(`š Test Coverage: Comprehensive device operations`);
console.log('\nšÆ KEY BENEFITS DEMONSTRATED:');
console.log(' ⢠Single shared API call eliminates redundancy');
console.log(' ⢠All test scenarios covered with cached data');
console.log(' ⢠Fast execution with minimal network overhead');
console.log(' ⢠Maintainable test isolation and reliability');
console.log('\nš” IMPLEMENTATION PATTERN:');
console.log('```typescript');
console.log('describe("Optimized Test Suite", () => {');
console.log(' let sharedData: any[];');
console.log(' ');
console.log(' beforeAll(async () => {');
console.log(' // SINGLE API CALL - shared across all tests');
console.log(' const cache = await TestDataCacheManager.loadSharedTestData(client);');
console.log(' sharedData = cache.devices;');
console.log(' });');
console.log(' ');
console.log(' it("test scenario 1", () => {');
console.log(' // Use cached data - NO additional API call');
console.log(' const filtered = TestDataUtils.filterDevices(sharedData, criteria);');
console.log(' expect(filtered).toHaveLength(expected);');
console.log(' });');
console.log('});');
console.log('```');
console.log('\nš OPTIMIZATION SUCCESS:');
console.log(' This approach can be applied to other test suites for similar benefits:');
console.log(' ⢠Alarm tests: Share alarm data across filtering scenarios');
console.log(' ⢠Flow tests: Cache flow data for analysis and reporting tests');
console.log(' ⢠Rule tests: Share rule configurations across validation tests');
console.log('\nš Ready for production use!');
console.log(' Use OPTIMIZE_TESTS=true to enable optimized patterns in CI/CD');
});
console.log('ā³ Running test suite...');