-
Notifications
You must be signed in to change notification settings - Fork 3
feat: Add timing/performance logging to Logger class #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,84 @@ | ||
| import chalk from "chalk"; | ||
|
|
||
| interface TimingEntry { | ||
| label: string; | ||
| startTime: number; | ||
| endTime?: number; | ||
| } | ||
|
|
||
| export class Logger { | ||
| private verbose: boolean; | ||
| private timings: Map<string, TimingEntry> = new Map(); | ||
|
|
||
| constructor() { | ||
| this.verbose = process.env.VERBOSE === "true"; | ||
| } | ||
|
|
||
| /** | ||
| * Start timing an operation | ||
| */ | ||
| startTimer(label: string): void { | ||
| this.timings.set(label, { | ||
| label, | ||
| startTime: Date.now(), | ||
| }); | ||
| if (this.verbose) { | ||
| console.log(chalk.magenta("⏱"), `Started: ${label}`); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * End timing and log the duration | ||
| */ | ||
| endTimer(label: string): number { | ||
| const entry = this.timings.get(label); | ||
| if (!entry) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. MINOR (70% confidence) Returning 0 for non-existent timers could mask bugs. Consider throwing an error or returning null/undefined to make the error more explicit to calling code. |
||
| this.warning(`Timer "${label}" was never started`); | ||
| return 0; | ||
| } | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. MINOR (90% confidence) Redundant Date.now() call - you're calculating duration correctly but then overwriting entry.endTime with a new timestamp. Should be: entry.endTime = entry.startTime + duration; |
||
| const duration = Date.now() - entry.startTime; | ||
| entry.endTime = Date.now(); | ||
|
|
||
| // Format duration for display | ||
| let durationStr: string; | ||
| if (duration < 1000) { | ||
| durationStr = `${duration}ms`; | ||
| } else if (duration < 60000) { | ||
| durationStr = `${(duration / 1000).toFixed(2)}s`; | ||
| } else { | ||
| const mins = Math.floor(duration / 60000); | ||
| const secs = ((duration % 60000) / 1000).toFixed(1); | ||
| durationStr = `${mins}m ${secs}s`; | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. MINOR (80% confidence) The endTimer method always logs to console regardless of verbose setting, while startTimer respects it. Consider making this behavior consistent - either both respect verbose or both always log. |
||
|
|
||
| console.log(chalk.magenta("⏱"), `${label}: ${chalk.bold(durationStr)}`); | ||
| return duration; | ||
| } | ||
|
|
||
| /** | ||
| * Get all timing data for analysis | ||
| */ | ||
| getTimingReport(): { label: string; duration: number }[] { | ||
| const report: { label: string; duration: number }[] = []; | ||
| for (const [label, entry] of this.timings) { | ||
| if (entry.endTime) { | ||
| report.push({ | ||
| label, | ||
| duration: entry.endTime - entry.startTime, | ||
| }); | ||
| } | ||
| } | ||
| return report; | ||
| } | ||
|
|
||
| /** | ||
| * Clear all timing data | ||
| */ | ||
| clearTimings(): void { | ||
| this.timings = new Map(); | ||
| } | ||
|
|
||
| info(...args: any[]): void { | ||
| console.log(chalk.blue("ℹ"), ...args); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MAJOR (80% confidence)
Using Date.now() for timing measurements can be inaccurate due to system clock adjustments. Consider using performance.now() for more precise timing measurements, especially for performance monitoring.