qnce-engine
Version: 
Core QNCE (Quantum Narrative Convergence Engine) - Framework agnostic narrative engine with performance optimization
220 lines (215 loc) โข 8.67 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.displayDashboard = displayDashboard;
exports.startLiveMonitor = startLiveMonitor;
exports.exportSummary = exportSummary;
exports.resetCounters = resetCounters;
const PerfReporter_js_1 = require("../performance/PerfReporter.js");
const ThreadPool_js_1 = require("../performance/ThreadPool.js");
const DEFAULT_THRESHOLDS = {
    maxCacheHitTime: 50, // 50ms max for cache operations
    minCacheHitRate: 80, // 80% minimum cache hit rate
    maxHotReloadTime: 2, // 2ms max for hot-reload (S2-T3 target)
    maxStateTransitionTime: 10, // 10ms max for state transitions
    maxEventBacklog: 1000 // Max 1000 events in backlog
};
/**
 * Display comprehensive performance dashboard
 */
function displayDashboard(thresholds = DEFAULT_THRESHOLDS) {
    console.log('\n๐ QNCE Performance Dashboard');
    console.log('=====================================');
    const summary = PerfReporter_js_1.perf.summary();
    const threadPool = (0, ThreadPool_js_1.getThreadPool)();
    // Header with time range
    const durationSeconds = (summary.timeRange.duration / 1000).toFixed(2);
    console.log(`๐ Session Duration: ${durationSeconds}s`);
    console.log(`๐ข Total Events: ${summary.totalEvents}`);
    console.log('');
    // Event breakdown
    console.log('๐ Event Breakdown:');
    for (const [type, count] of Object.entries(summary.eventsByType)) {
        const avg = summary.avgDurations[type]?.toFixed(2) || 'N/A';
        const max = summary.maxDurations[type]?.toFixed(2) || 'N/A';
        console.log(`   ${type.padEnd(20)} ${count.toString().padStart(6)} events (avg: ${avg}ms, max: ${max}ms)`);
    }
    console.log('');
    // Cache Performance
    console.log('๐พ Cache Performance:');
    const cacheStatus = summary.cacheHitRate >= thresholds.minCacheHitRate ? 'โ
' : 'โ ๏ธ';
    console.log(`   ${cacheStatus} Hit Rate: ${summary.cacheHitRate.toFixed(1)}% (threshold: ${thresholds.minCacheHitRate}%)`);
    const cacheTime = summary.avgDurations['cache-hit'] || 0;
    const cacheTimeStatus = cacheTime <= thresholds.maxCacheHitTime ? 'โ
' : 'โ ๏ธ';
    console.log(`   ${cacheTimeStatus} Avg Cache Time: ${cacheTime.toFixed(2)}ms (threshold: ${thresholds.maxCacheHitTime}ms)`);
    console.log('');
    // Hot-Reload Performance (S2-T3 metrics)
    console.log('๐ฅ Hot-Reload Performance:');
    const { avgTime, maxTime, totalReloads } = summary.hotReloadPerformance;
    const hotReloadStatus = avgTime <= thresholds.maxHotReloadTime ? 'โ
' : 'โ ๏ธ';
    console.log(`   ${hotReloadStatus} Avg Time: ${avgTime.toFixed(2)}ms (threshold: ${thresholds.maxHotReloadTime}ms)`);
    console.log(`   ๐ Max Time: ${maxTime.toFixed(2)}ms`);
    console.log(`   ๐ Total Reloads: ${totalReloads}`);
    console.log('');
    // State Transition Performance
    console.log('๐ State Transitions:');
    const stateTime = summary.avgDurations['state-transition'] || 0;
    const stateStatus = stateTime <= thresholds.maxStateTransitionTime ? 'โ
' : 'โ ๏ธ';
    console.log(`   ${stateStatus} Avg Time: ${stateTime.toFixed(2)}ms (threshold: ${thresholds.maxStateTransitionTime}ms)`);
    console.log('');
    // ThreadPool Status (S2-T2 metrics)
    console.log('๐งต ThreadPool Status:');
    const stats = threadPool.getStats();
    console.log(`   ๐ Completed Jobs: ${stats.completedJobs}`);
    console.log(`   โณ Queued Jobs: ${stats.queuedJobs}`);
    console.log(`   ๐ Active Workers: ${stats.activeWorkers}`);
    console.log(`   ๐ Avg Execution Time: ${stats.avgExecutionTime.toFixed(2)}ms`);
    console.log(`   ๐ Worker Utilization: ${stats.workerUtilization.toFixed(1)}%`);
    console.log('');
    // Performance Alerts
    displayAlerts(summary, thresholds, stats);
}
/**
 * Display performance threshold alerts
 */
function displayAlerts(summary, thresholds, threadStats) {
    console.log('๐จ Performance Alerts:');
    const alerts = [];
    // Cache alerts
    if (summary.cacheHitRate < thresholds.minCacheHitRate) {
        alerts.push(`Low cache hit rate: ${summary.cacheHitRate.toFixed(1)}% < ${thresholds.minCacheHitRate}%`);
    }
    const cacheTime = summary.avgDurations['cache-hit'] || 0;
    if (cacheTime > thresholds.maxCacheHitTime) {
        alerts.push(`Slow cache operations: ${cacheTime.toFixed(2)}ms > ${thresholds.maxCacheHitTime}ms`);
    }
    // Hot-reload alerts (S2-T3 compliance)
    if (summary.hotReloadPerformance.avgTime > thresholds.maxHotReloadTime) {
        alerts.push(`Hot-reload exceeds target: ${summary.hotReloadPerformance.avgTime.toFixed(2)}ms > ${thresholds.maxHotReloadTime}ms`);
    }
    // State transition alerts
    const stateTime = summary.avgDurations['state-transition'] || 0;
    if (stateTime > thresholds.maxStateTransitionTime) {
        alerts.push(`Slow state transitions: ${stateTime.toFixed(2)}ms > ${thresholds.maxStateTransitionTime}ms`);
    }
    // Event backlog alerts
    if (summary.totalEvents > thresholds.maxEventBacklog) {
        alerts.push(`High event backlog: ${summary.totalEvents} > ${thresholds.maxEventBacklog}`);
    }
    // ThreadPool alerts
    if (threadStats.queuedJobs > 10) {
        alerts.push(`High queued job count: ${threadStats.queuedJobs} jobs`);
    }
    if (alerts.length === 0) {
        console.log('   โ
 All systems performing within thresholds');
    }
    else {
        alerts.forEach(alert => console.log(`   โ ๏ธ  ${alert}`));
    }
    console.log('');
}
/**
 * Live monitor mode - continuously update performance metrics
 */
function startLiveMonitor(intervalMs = 2000, thresholds = DEFAULT_THRESHOLDS) {
    console.log(`๐ด Starting live performance monitor (${intervalMs}ms interval)`);
    console.log('Press Ctrl+C to stop');
    const interval = setInterval(() => {
        // Clear screen
        console.clear();
        displayDashboard(thresholds);
        // Show live timestamp
        console.log(`๐ Last updated: ${new Date().toLocaleTimeString()}`);
    }, intervalMs);
    // Handle graceful shutdown
    process.on('SIGINT', () => {
        clearInterval(interval);
        console.log('\n๐ Live monitor stopped');
        process.exit(0);
    });
}
/**
 * Export performance summary as JSON
 */
function exportSummary() {
    const summary = PerfReporter_js_1.perf.summary();
    const threadStats = (0, ThreadPool_js_1.getThreadPool)().getStats();
    const report = {
        timestamp: new Date().toISOString(),
        performanceSummary: summary,
        threadPoolStats: threadStats,
        thresholds: DEFAULT_THRESHOLDS
    };
    console.log(JSON.stringify(report, null, 2));
}
/**
 * Reset performance counters
 */
function resetCounters() {
    (0, PerfReporter_js_1.getPerfReporter)().clear();
    console.log('โ
 Performance counters reset');
}
/**
 * Main CLI entry point
 */
function main() {
    const args = process.argv.slice(2);
    const command = args[0] || 'dashboard';
    switch (command) {
        case 'dashboard':
        case 'dash':
            displayDashboard();
            break;
        case 'live':
        case 'monitor': {
            const interval = parseInt(args[1]) || 2000;
            startLiveMonitor(interval);
            break;
        }
        case 'export':
        case 'json':
            exportSummary();
            break;
        case 'reset':
        case 'clear':
            resetCounters();
            break;
        case 'help':
        case '--help':
        case '-h':
            console.log(`
๐ QNCE Performance CLI
Usage: qnce-perf [command] [options]
Commands:
  dashboard, dash     Show performance dashboard (default)
  live, monitor       Start live monitoring mode
  export, json        Export performance data as JSON
  reset, clear        Reset performance counters
  help               Show this help
Options:
  --interval <ms>     Monitoring interval for live mode (default: 2000ms)
Examples:
  qnce-perf dashboard
  qnce-perf live 1000
  qnce-perf export > performance-report.json
  qnce-perf reset
Performance Thresholds:
  Cache Hit Time:      < 50ms
  Cache Hit Rate:      > 80%
  Hot-Reload Time:     < 2ms  (S2-T3 target)
  State Transition:    < 10ms
  Event Backlog:       < 1000 events
      `);
            break;
        default:
            console.error(`โ Unknown command: ${command}`);
            console.log('Run "qnce-perf help" for usage information');
            process.exit(1);
    }
}
// Run if called directly (ES module compatible)
const isMainModule = require.main === module;
if (isMainModule) {
    main();
}
//# sourceMappingURL=perf.js.map