ms365-mcp-server
Version:
Microsoft 365 MCP Server for managing Microsoft 365 email through natural language interactions with full OAuth2 authentication support
107 lines (106 loc) • 4.62 kB
JavaScript
import { logger } from './api.js';
export class BatchPerformanceMonitor {
constructor() {
this.metrics = [];
}
/**
* Start tracking a performance operation
*/
startOperation(operationType, requestCount, method) {
const operationId = `${operationType}_${method}_${Date.now()}`;
const startTime = Date.now();
logger.log(`📊 PERFORMANCE START: ${operationType} (${method}) - ${requestCount} requests`);
return operationId;
}
/**
* End tracking a performance operation
*/
endOperation(operationId, operationType, requestCount, method, startTime, httpCalls, success = true, errorCount = 0) {
const endTime = Date.now();
const duration = endTime - startTime;
const averagePerRequest = duration / requestCount;
const metrics = {
operationType,
requestCount,
startTime,
endTime,
duration,
averagePerRequest,
success,
errorCount,
httpCalls,
method
};
this.metrics.push(metrics);
logger.log(`📊 PERFORMANCE END: ${operationType} (${method})`);
logger.log(` Duration: ${duration}ms`);
logger.log(` HTTP Calls: ${httpCalls}`);
logger.log(` Avg per request: ${averagePerRequest.toFixed(2)}ms`);
logger.log(` Success: ${success} (${errorCount} errors)`);
return metrics;
}
/**
* Compare traditional vs batched performance
*/
comparePerformance(operationType) {
const traditionalMetrics = this.metrics.find(m => m.operationType === operationType && m.method === 'traditional');
const batchedMetrics = this.metrics.find(m => m.operationType === operationType && m.method === 'batched');
if (!traditionalMetrics || !batchedMetrics) {
return null;
}
const durationReduction = ((traditionalMetrics.duration - batchedMetrics.duration) / traditionalMetrics.duration) * 100;
const httpCallReduction = ((traditionalMetrics.httpCalls - batchedMetrics.httpCalls) / traditionalMetrics.httpCalls) * 100;
const efficiencyGain = (traditionalMetrics.averagePerRequest - batchedMetrics.averagePerRequest) / traditionalMetrics.averagePerRequest * 100;
const result = {
traditional: traditionalMetrics,
batched: batchedMetrics,
improvement: {
durationReduction,
httpCallReduction,
efficiencyGain
}
};
logger.log(`🏆 PERFORMANCE COMPARISON: ${operationType}`);
logger.log(` Duration: ${traditionalMetrics.duration}ms → ${batchedMetrics.duration}ms (${durationReduction.toFixed(1)}% faster)`);
logger.log(` HTTP Calls: ${traditionalMetrics.httpCalls} → ${batchedMetrics.httpCalls} (${httpCallReduction.toFixed(1)}% reduction)`);
logger.log(` Efficiency: ${efficiencyGain.toFixed(1)}% improvement per request`);
return result;
}
/**
* Get all metrics for a specific operation type
*/
getMetrics(operationType) {
if (operationType) {
return this.metrics.filter(m => m.operationType === operationType);
}
return [...this.metrics];
}
/**
* Generate performance report
*/
generateReport() {
const operationTypes = [...new Set(this.metrics.map(m => m.operationType))];
let report = `📊 BATCH PERFORMANCE REPORT\n\n`;
operationTypes.forEach(opType => {
const comparison = this.comparePerformance(opType);
if (comparison) {
report += `🔍 ${opType.toUpperCase()}\n`;
report += ` Traditional: ${comparison.traditional.duration}ms (${comparison.traditional.httpCalls} HTTP calls)\n`;
report += ` Batched: ${comparison.batched.duration}ms (${comparison.batched.httpCalls} HTTP calls)\n`;
report += ` 💡 Improvements:\n`;
report += ` - ${comparison.improvement.durationReduction.toFixed(1)}% faster execution\n`;
report += ` - ${comparison.improvement.httpCallReduction.toFixed(1)}% fewer HTTP calls\n`;
report += ` - ${comparison.improvement.efficiencyGain.toFixed(1)}% better efficiency\n\n`;
}
});
return report;
}
/**
* Clear all metrics
*/
clear() {
this.metrics = [];
}
}
// Singleton instance
export const performanceMonitor = new BatchPerformanceMonitor();