UNPKG

ctrlshiftleft

Version:

AI-powered toolkit for embedding QA and security testing into development workflows

154 lines (125 loc) โ€ข 5.27 kB
#!/usr/bin/env node /** * Performance Analysis Script * * This script demonstrates the performance metrics tracking capabilities * introduced in v1.4.0. It runs a test generation and execution workflow * while tracking performance metrics at each step. */ const fs = require('fs'); const path = require('path'); const { execSync } = require('child_process'); // Use JavaScript wrappers instead of TypeScript modules const { PerformanceTracker } = require('./utils/performanceTracker'); const { PathUtils } = require('./utils/platformUtils'); const { errorHandler } = require('./utils/errorHandler'); // Parse command line arguments const args = process.argv.slice(2); const targetFile = args[0]; const outputDir = args[1] || path.join(process.cwd(), 'performance-reports'); if (!targetFile) { console.error('Usage: analyze-performance.js <target-file> [output-dir]'); process.exit(1); } // Create performance tracker const tracker = new PerformanceTracker({ projectName: path.basename(process.cwd()), version: require('../package.json').version, outputDir, enabled: true }); // Main function async function runPerformanceAnalysis() { console.log('๐Ÿ“Š Running performance analysis...'); try { // Ensure the target file exists if (!fs.existsSync(targetFile)) { throw new Error(`Target file not found: ${targetFile}`); } // Step 1: Generate tests with performance tracking console.log(`\n๐Ÿงช Generating tests for ${path.basename(targetFile)}...`); const generationTimer = tracker.startTimer('test-generation', targetFile); try { // Generate tests using the ctrl.shift.left CLI const testOutputDir = path.join(process.cwd(), 'tests'); PathUtils.ensureDir(testOutputDir); execSync(`node ./bin/ctrlshiftleft gen ${targetFile} --output ${testOutputDir}`, { stdio: 'inherit' }); const generationDuration = tracker.stopTimer(generationTimer); console.log(`โœ… Test generation completed in ${generationDuration}ms`); } catch (error) { tracker.stopTimer(generationTimer, { success: false, error: error.message }); throw error; } // Step 2: Run security analysis with performance tracking console.log('\n๐Ÿ”’ Running security analysis...'); const securityTimer = tracker.startTimer('security-analysis', targetFile); try { // Run security analysis using the ctrl.shift.left CLI const securityOutputDir = path.join(process.cwd(), 'security-reports'); PathUtils.ensureDir(securityOutputDir); execSync(`node ./bin/ctrlshiftleft secure ${targetFile} --output ${securityOutputDir}`, { stdio: 'inherit' }); const securityDuration = tracker.stopTimer(securityTimer); console.log(`โœ… Security analysis completed in ${securityDuration}ms`); } catch (error) { tracker.stopTimer(securityTimer, { success: false, error: error.message }); throw error; } // Step 3: Run tests with performance tracking console.log('\n๐Ÿš€ Running generated tests...'); const testExecutionTimer = tracker.startTimer('test-execution', targetFile); try { // Run tests using the ctrl.shift.left CLI execSync(`node ./bin/ctrlshiftleft run ${targetFile}`, { stdio: 'inherit' }); const executionDuration = tracker.stopTimer(testExecutionTimer); console.log(`โœ… Test execution completed in ${executionDuration}ms`); } catch (error) { tracker.stopTimer(testExecutionTimer, { success: false, error: error.message }); throw error; } // Generate and save performance report console.log('\n๐Ÿ“ Generating performance report...'); const report = tracker.generateReport(`analysis-${path.basename(targetFile)}`); // Save as JSON and Markdown const jsonPath = tracker.saveReport('json'); const markdownPath = tracker.saveReport('markdown'); console.log(`\nโœ… Performance analysis complete!`); console.log(`๐Ÿ“Š JSON Report: ${jsonPath}`); console.log(`๐Ÿ“‘ Markdown Report: ${markdownPath}`); // Performance summary console.log('\n๐Ÿ“ˆ Performance Summary:'); console.log(`Total Duration: ${report.summary.totalDuration}ms`); console.log(`Average Duration: ${report.summary.averageDuration}ms`); console.log(`Success Rate: ${report.summary.successRate}%`); // Log operation types console.log('\nโš™๏ธ Operation Types:'); Object.entries(report.summary.byType).forEach(([type, stats]) => { console.log(`- ${type}: ${stats.count} operations, avg ${stats.averageDuration}ms`); }); return { success: true, reportPath: markdownPath }; } catch (error) { const enhancedError = errorHandler.handleError(error, 'performance-analysis', { targetFile, outputDir }); errorHandler.displayError(enhancedError); errorHandler.logError(enhancedError); return { success: false, error: enhancedError }; } } // Run the analysis runPerformanceAnalysis() .then(result => { if (!result.success) { process.exit(1); } }) .catch(error => { console.error('Unhandled error:', error); process.exit(1); });