* Get a formatted report of all checkpoints for the current/last query
()
| 129 | * Get a formatted report of all checkpoints for the current/last query |
| 130 | */ |
| 131 | function getQueryProfileReport(): string { |
| 132 | if (!ENABLED) { |
| 133 | return 'Query profiling not enabled (set CLAUDE_CODE_PROFILE_QUERY=1)' |
| 134 | } |
| 135 | |
| 136 | const perf = getPerformance() |
| 137 | const marks = perf.getEntriesByType('mark') |
| 138 | if (marks.length === 0) { |
| 139 | return 'No query profiling checkpoints recorded' |
| 140 | } |
| 141 | |
| 142 | const lines: string[] = [] |
| 143 | lines.push('='.repeat(80)) |
| 144 | lines.push(`QUERY PROFILING REPORT - Query #${queryCount}`) |
| 145 | lines.push('='.repeat(80)) |
| 146 | lines.push('') |
| 147 | |
| 148 | // Use first mark as baseline (query start time) to show relative times |
| 149 | const baselineTime = marks[0]?.startTime ?? 0 |
| 150 | let prevTime = baselineTime |
| 151 | let apiRequestSentTime = 0 |
| 152 | let firstChunkTime = 0 |
| 153 | |
| 154 | for (const mark of marks) { |
| 155 | const relativeTime = mark.startTime - baselineTime |
| 156 | const deltaMs = mark.startTime - prevTime |
| 157 | lines.push( |
| 158 | formatTimelineLine( |
| 159 | relativeTime, |
| 160 | deltaMs, |
| 161 | mark.name, |
| 162 | memorySnapshots.get(mark.name), |
| 163 | 10, |
| 164 | 9, |
| 165 | getSlowWarning(deltaMs, mark.name), |
| 166 | ), |
| 167 | ) |
| 168 | |
| 169 | // Track key milestones for summary (use relative times) |
| 170 | if (mark.name === 'query_api_request_sent') { |
| 171 | apiRequestSentTime = relativeTime |
| 172 | } |
| 173 | if (mark.name === 'query_first_chunk_received') { |
| 174 | firstChunkTime = relativeTime |
| 175 | } |
| 176 | |
| 177 | prevTime = mark.startTime |
| 178 | } |
| 179 | |
| 180 | // Calculate summary statistics (relative to baseline) |
| 181 | const lastMark = marks[marks.length - 1] |
| 182 | const totalTime = lastMark ? lastMark.startTime - baselineTime : 0 |
| 183 | |
| 184 | lines.push('') |
| 185 | lines.push('-'.repeat(80)) |
| 186 | |
| 187 | if (firstChunkTime > 0) { |
| 188 | const preRequestOverhead = apiRequestSentTime |
no test coverage detected