ecs-pf
Version:
CLI for port-forwarding to RDS via AWS ECS
53 lines (52 loc) ⢠1.84 kB
JavaScript
export class PerformanceTracker {
metrics = [];
currentStep = null;
stepStartTime = null;
startStep(step) {
if (this.currentStep) {
this.endStep();
}
this.currentStep = step;
this.stepStartTime = performance.now();
}
endStep() {
if (this.currentStep && this.stepStartTime !== null) {
const endTime = performance.now();
this.metrics.push({
step: this.currentStep,
startTime: this.stepStartTime,
endTime,
duration: endTime - this.stepStartTime,
});
this.currentStep = null;
this.stepStartTime = null;
}
}
getMetrics() {
if (this.currentStep) {
this.endStep();
}
return [...this.metrics];
}
getTotalDuration() {
return this.metrics.reduce((total, metric) => total + metric.duration, 0);
}
getReport() {
const metrics = this.getMetrics();
const total = this.getTotalDuration();
const reportLines = ["\nš Performance Report\n", `${"=".repeat(50)}\n`];
for (const metric of metrics) {
const percentage = total > 0 ? ((metric.duration / total) * 100).toFixed(1) : "0.0";
reportLines.push(`${metric.step.padEnd(30)} ${metric.duration.toFixed(0).padStart(6)}ms (${percentage}%)\n`);
}
reportLines.push(`${"=".repeat(50)}\n`, `${"Total".padEnd(30)} ${total.toFixed(0).padStart(6)}ms (100.0%)\n`);
if (total > 3000) {
reportLines.push("\nWarning: Total time exceeds 3 seconds threshold\n");
}
else {
reportLines.push("\nPerformance within acceptable range (<3s)\n");
}
const report = reportLines.join("");
return report;
}
}