file-mover
Version:
Script to move files and update imports automatically
140 lines • 6.06 kB
JavaScript
/**
* Performance timing utilities
*/
class PerformanceTrackerImpl {
metrics = {
totalTime: 0,
fileDiscovery: { time: 0, fileCount: 0 },
validation: { time: 0, moveCount: 0 },
importAnalysis: { time: 0, totalFiles: 0, filesWithImports: 0 },
fileOperations: { time: 0, moves: 0, updates: 0 },
cachePerformance: { astCacheHits: 0, fileCacheHits: 0, totalCacheLookups: 0 },
individualMoves: [],
};
verbose;
constructor(verbose = false) {
this.verbose = verbose;
}
start(label) {
const start = performance.now();
return {
end: () => {
const end = performance.now();
const duration = end - start;
if (this.verbose) {
console.log(`⏱️ ${label}: ${duration.toFixed(2)}ms`);
}
return duration;
}
};
}
/**
* Clear all caches to free memory
*/
clearCaches() {
// Clear AST cache from importUtils
if (typeof globalThis !== 'undefined' && globalThis.astCache) {
globalThis.astCache.clear();
}
// Clear file content cache from fileOps
if (typeof globalThis !== 'undefined' && globalThis.fileContentCache) {
globalThis.fileContentCache.clear();
}
if (this.verbose) {
console.log("🧹 Caches cleared");
}
}
/**
* Print detailed performance summary
*/
printSummary() {
console.log("\n📊 PERFORMANCE SUMMARY");
console.log("=".repeat(50));
console.log(`Total execution time: ${this.metrics.totalTime.toFixed(2)}ms`);
console.log(`\nBreakdown:`);
console.log(` File discovery: ${this.metrics.fileDiscovery.time.toFixed(2)}ms (${this.metrics.fileDiscovery.fileCount} files)`);
console.log(` Validation: ${this.metrics.validation.time.toFixed(2)}ms (${this.metrics.validation.moveCount} moves)`);
console.log(` File operations: ${this.metrics.fileOperations.time.toFixed(2)}ms (${this.metrics.fileOperations.moves} moves, ${this.metrics.fileOperations.updates} updates)`);
// Cache performance
if (this.metrics.cachePerformance.totalCacheLookups > 0) {
const astHitRate = (this.metrics.cachePerformance.astCacheHits / this.metrics.cachePerformance.totalCacheLookups * 100).toFixed(1);
const fileHitRate = (this.metrics.cachePerformance.fileCacheHits / this.metrics.cachePerformance.totalCacheLookups * 100).toFixed(1);
console.log(`\nCache Performance:`);
console.log(` AST cache hit rate: ${astHitRate}% (${this.metrics.cachePerformance.astCacheHits}/${this.metrics.cachePerformance.totalCacheLookups})`);
console.log(` File cache hit rate: ${fileHitRate}% (${this.metrics.cachePerformance.fileCacheHits}/${this.metrics.cachePerformance.totalCacheLookups})`);
}
if (this.metrics.individualMoves.length > 0) {
console.log(`\nIndividual move performance:`);
this.metrics.individualMoves.forEach((move, index) => {
const totalMoveTime = move.analysisTime + move.moveTime + move.updateTime;
console.log(` Move ${index + 1}: ${totalMoveTime.toFixed(2)}ms total`);
console.log(` Analysis: ${move.analysisTime.toFixed(2)}ms`);
console.log(` Physical move: ${move.moveTime.toFixed(2)}ms`);
console.log(` Import updates: ${move.updateTime.toFixed(2)}ms (${move.filesUpdated} files updated)`);
});
}
// Performance insights
console.log(`\n💡 Performance Insights:`);
const avgAnalysisTime = this.metrics.individualMoves.reduce((sum, move) => sum + move.analysisTime, 0) / this.metrics.individualMoves.length;
const avgUpdateTime = this.metrics.individualMoves.reduce((sum, move) => sum + move.updateTime, 0) / this.metrics.individualMoves.length;
console.log(` Average analysis time per move: ${avgAnalysisTime.toFixed(2)}ms`);
console.log(` Average update time per move: ${avgUpdateTime.toFixed(2)}ms`);
console.log(` File discovery efficiency: ${(this.metrics.fileDiscovery.fileCount / this.metrics.fileDiscovery.time * 1000).toFixed(1)} files/ms`);
// Clear caches to free memory
this.clearCaches();
}
/**
* Reset all metrics
*/
reset() {
this.metrics = {
totalTime: 0,
fileDiscovery: { time: 0, fileCount: 0 },
validation: { time: 0, moveCount: 0 },
importAnalysis: { time: 0, totalFiles: 0, filesWithImports: 0 },
fileOperations: { time: 0, moves: 0, updates: 0 },
cachePerformance: { astCacheHits: 0, fileCacheHits: 0, totalCacheLookups: 0 },
individualMoves: [],
};
}
}
// Global performance tracker instance
let globalPerformanceTracker = null;
/**
* Get or create the global performance tracker
*/
export function getPerformanceTracker(verbose = false) {
if (!globalPerformanceTracker) {
globalPerformanceTracker = new PerformanceTrackerImpl(verbose);
}
return globalPerformanceTracker;
}
/**
* Reset the global performance tracker
*/
export function resetPerformanceTracker() {
globalPerformanceTracker = null;
}
/**
* Track cache performance metrics
*/
export function trackCacheHit(type) {
if (globalPerformanceTracker) {
globalPerformanceTracker.metrics.cachePerformance.totalCacheLookups++;
if (type === 'ast') {
globalPerformanceTracker.metrics.cachePerformance.astCacheHits++;
}
else {
globalPerformanceTracker.metrics.cachePerformance.fileCacheHits++;
}
}
}
/**
* Track cache lookup (miss)
*/
export function trackCacheLookup() {
if (globalPerformanceTracker) {
globalPerformanceTracker.metrics.cachePerformance.totalCacheLookups++;
}
}
//# sourceMappingURL=performance.js.map