stack-performance
Version:
A comprehensive application stack analyzer that evaluates MEAN, MERN, and other Node.js-based applications across 15 performance criteria
91 lines (73 loc) • 3 kB
JavaScript
const BaseAnalyzer = require('./BaseAnalyzer');
/**
* API Response Time Analyzer with algorithmic scoring
*/
class ApiResponseTimeAnalyzer extends BaseAnalyzer {
constructor(stackInfo, projectPath) {
super(stackInfo, projectPath, 'API Response Time');
}
async analyze() {
const factors = [];
// Factor 1: Framework Efficiency (35%)
const frameworkScore = this.analyzeFrameworkEfficiency();
factors.push({ score: frameworkScore, weight: 0.35 });
// Factor 2: Database Query Optimization (25%)
const dbOptScore = this.analyzeDatabaseOptimization();
factors.push({ score: dbOptScore, weight: 0.25 });
// Factor 3: Middleware Overhead (25%)
const middlewareScore = this.analyzeMiddlewareEfficiency();
factors.push({ score: middlewareScore, weight: 0.25 });
// Factor 4: Response Compression (15%)
const compressionScore = this.analyzeCompressionUsage();
factors.push({ score: compressionScore, weight: 0.15 });
const finalScore = this.calculateWeightedScore(factors);
const details = {
frameworkEfficiency: frameworkScore,
databaseOptimization: dbOptScore,
middlewareEfficiency: middlewareScore,
compressionUsage: compressionScore,
stackType: this.stackInfo.type
};
return this.createResult(finalScore, details, this.generateRecommendations(finalScore));
}
analyzeFrameworkEfficiency() {
const { technologies } = this.stackInfo;
let score = 60;
if (technologies.includes('Fastify')) score += 25; // Highly optimized
if (technologies.includes('Express.js')) score += 20; // Good performance
if (technologies.includes('Koa')) score += 18; // Lightweight
return Math.min(100, score);
}
analyzeDatabaseOptimization() {
let score = 60;
if (this.hasDependency('mongoose')) {
score += 15;
// Check for connection pooling indicators
if (this.findFiles('**/config/**/*.js').length > 0) score += 10;
}
if (this.hasDependency('mongodb')) score += 12;
return Math.min(100, score);
}
analyzeMiddlewareEfficiency() {
let score = 70;
if (this.hasDependency('helmet')) score += 5; // Security without major overhead
if (this.hasDependency('cors')) score += 3;
if (this.hasDependency('compression')) score += 8; // Actually improves response time
return Math.min(100, score);
}
analyzeCompressionUsage() {
let score = 50;
if (this.hasDependency('compression')) score += 30;
if (this.hasDependency('gzip')) score += 25;
return Math.min(100, score);
}
generateRecommendations(score) {
const recommendations = [];
if (score < 70) {
recommendations.push('Consider using Fastify for better performance');
recommendations.push('Implement response compression middleware');
}
return recommendations;
}
}
module.exports = ApiResponseTimeAnalyzer;