@@ -1788,17 +1788,38 @@ async function roadmapContext() { |
1788 | 1788 | return { rows, items, pool: poolResult.pool, reconciliation, drill }; |
1789 | 1789 | } |
1790 | 1790 |
|
| 1791 | +type SearchTimings = Record<string, number>; |
| 1792 | + |
| 1793 | +async function withSearchTiming<T>(timings: SearchTimings, stage: string, run: () => T | Promise<T>): Promise<T> { |
| 1794 | + const started = performance.now(); |
| 1795 | + try { |
| 1796 | + return await run(); |
| 1797 | + } finally { |
| 1798 | + timings[stage] = performance.now() - started; |
| 1799 | + } |
| 1800 | +} |
| 1801 | + |
| 1802 | +function withSearchTimings(response: Response, timings: SearchTimings): Response { |
| 1803 | + const value = Object.entries(timings) |
| 1804 | + .map(([stage, duration]) => `${stage};dur=${Math.max(0, duration).toFixed(2)}`) |
| 1805 | + .join(", "); |
| 1806 | + response.headers.set("server-timing", value); |
| 1807 | + return response; |
| 1808 | +} |
| 1809 | + |
1791 | 1810 | async function searchContext(query: string) { |
| 1811 | + const timings: SearchTimings = {}; |
1792 | 1812 | // #1212 slice 2: registry, DNS, services, and tier items join the |
1793 | 1813 | // board/roadmap subjects. Each extra source fails soft (empty on |
1794 | | - // error) so one broken source never takes search down with it. |
| 1814 | + // error) so one broken source never takes search down with it. The |
| 1815 | + // source timings below overlap: these reads intentionally run in parallel. |
1795 | 1816 | const [board, rows, registry, dns, usage, orchestration] = await Promise.all([ |
1796 | | - source.board(), |
1797 | | - source.roadmap(), |
1798 | | - source.registry().catch(() => []), |
1799 | | - source.dns().catch(() => null), |
1800 | | - source.usage().catch(() => null), |
1801 | | - source.orchestration().catch(() => null), |
| 1817 | + withSearchTiming(timings, "board", () => source.board()), |
| 1818 | + withSearchTiming(timings, "roadmap", () => source.roadmap()), |
| 1819 | + withSearchTiming(timings, "registry", () => source.registry().catch(() => [])), |
| 1820 | + withSearchTiming(timings, "dns", () => source.dns().catch(() => null)), |
| 1821 | + withSearchTiming(timings, "usage", () => source.usage().catch(() => null)), |
| 1822 | + withSearchTiming(timings, "orchestration", () => source.orchestration().catch(() => null)), |
1802 | 1823 | ]); |
1803 | 1824 | const tierItems = orchestration ? [...orchestration.tiers.needsDrew, ...orchestration.tiers.needsOrchestrator] : []; |
1804 | 1825 | // Codex P2 on PR #7331: refs carried only by the slice-2 sources |
@@ -1809,9 +1830,10 @@ async function searchContext(query: string) { |
1809 | 1830 | isValidRef, |
1810 | 1831 | ); |
1811 | 1832 | const refs = [...new Set([...allRefs(board), ...allRefs(rows), ...extraRefs])]; |
1812 | | - const items = await source.items(refs); |
| 1833 | + const items = await withSearchTiming(timings, "items", () => source.items(refs)); |
1813 | 1834 | const extras = { registry, dns, services: usage?.services ?? [], tierItems }; |
1814 | | - return { query, results: searchWork(board, rows, items, query, extras), items }; |
| 1835 | + const results = await withSearchTiming(timings, "search", () => searchWork(board, rows, items, query, extras)); |
| 1836 | + return { query, results, items, timings }; |
1815 | 1837 | } |
1816 | 1838 |
|
1817 | 1839 | // #3321: watchdog gap-sync passes run whole and in observation |
@@ -3994,12 +4016,20 @@ async function handler(req: Request, remoteAddress?: string): Promise<Response> |
3994 | 4016 | ); |
3995 | 4017 | } |
3996 | 4018 | if (path === "/search") { |
3997 | | - const { query, results, items } = await searchContext(url.searchParams.get("q") ?? ""); |
3998 | | - return html(renderSearch(query, results, items, prov(), provDetail())); |
| 4019 | + const started = performance.now(); |
| 4020 | + const { query, results, items, timings } = await searchContext(url.searchParams.get("q") ?? ""); |
| 4021 | + const body = await withSearchTiming(timings, "render", () => renderSearch(query, results, items, prov(), provDetail())); |
| 4022 | + timings.total = performance.now() - started; |
| 4023 | + return withSearchTimings(html(body), timings); |
3999 | 4024 | } |
4000 | 4025 | if (path === "/api/search.json") { |
4001 | | - const { query, results } = await searchContext(url.searchParams.get("q") ?? ""); |
4002 | | - return json({ query: query.trim(), total: results.length, results }); |
| 4026 | + const started = performance.now(); |
| 4027 | + const { query, results, timings } = await searchContext(url.searchParams.get("q") ?? ""); |
| 4028 | + const response = await withSearchTiming(timings, "serialize", () => |
| 4029 | + json({ query: query.trim(), total: results.length, results }), |
| 4030 | + ); |
| 4031 | + timings.total = performance.now() - started; |
| 4032 | + return withSearchTimings(response, timings); |
4003 | 4033 | } |
4004 | 4034 | if (path === "/api/registry.json") { |
4005 | 4035 | const records = await source.registry(); |
|