Skip to content

Commit ce52f65

Browse files
Add some instrumentation to web
1 parent d70b729 commit ce52f65

File tree

3 files changed

+52
-12
lines changed

3 files changed

+52
-12
lines changed

packages/web/src/app/[domain]/search/page.tsx

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import useCaptureEvent from "@/hooks/useCaptureEvent";
1010
import { useNonEmptyQueryParam } from "@/hooks/useNonEmptyQueryParam";
1111
import { useSearchHistory } from "@/hooks/useSearchHistory";
1212
import { Repository, SearchQueryParams, SearchResultFile } from "@/lib/types";
13-
import { createPathWithQueryParams } from "@/lib/utils";
13+
import { createPathWithQueryParams, measureSync } from "@/lib/utils";
1414
import { SymbolIcon } from "@radix-ui/react-icons";
1515
import { useQuery } from "@tanstack/react-query";
1616
import { useRouter } from "next/navigation";
@@ -139,16 +139,19 @@ const SearchPageInternal = () => {
139139

140140
// We only want to show matches for the default branch when
141141
// the user isn't explicitly filtering by branch.
142-
if (!isBranchFilteringEnabled) {
143-
fileMatches = fileMatches.filter(match => {
144-
// @note : this case handles local repos that don't have any branches.
145-
if (!match.Branches) {
146-
return true;
147-
}
148142

149-
return match.Branches.includes("HEAD");
150-
});
151-
}
143+
measureSync(() => {
144+
if (!isBranchFilteringEnabled) {
145+
fileMatches = fileMatches.filter(match => {
146+
// @note : this case handles local repos that don't have any branches.
147+
if (!match.Branches) {
148+
return true;
149+
}
150+
151+
return match.Branches.includes("HEAD");
152+
});
153+
}
154+
}, "search.branchFiltering");
152155

153156
return {
154157
fileMatches,

packages/web/src/app/api/(client)/client.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@
33
import { NEXT_PUBLIC_DOMAIN_SUB_PATH } from "@/lib/environment.client";
44
import { fileSourceResponseSchema, listRepositoriesResponseSchema, searchResponseSchema } from "@/lib/schemas";
55
import { FileSourceRequest, FileSourceResponse, ListRepositoriesResponse, SearchRequest, SearchResponse } from "@/lib/types";
6+
import { measure } from "@/lib/utils";
67
import assert from "assert";
78

89
export const search = async (body: SearchRequest, domain: string): Promise<SearchResponse> => {
910
const path = resolveServerPath("/api/search");
10-
const result = await fetch(path, {
11+
const { data: result } = await measure(() => fetch(path, {
1112
method: "POST",
1213
headers: {
1314
"Content-Type": "application/json",
1415
"X-Org-Domain": domain,
1516
},
1617
body: JSON.stringify(body),
17-
}).then(response => response.json());
18+
}).then(response => response.json()), "client.search");
1819

1920
return searchResponseSchema.parse(result);
2021
}

packages/web/src/lib/utils.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,39 @@ export const getDisplayTime = (date: Date) => {
208208
return formatTime(months, 'month');
209209
}
210210
}
211+
212+
export const measureSync = <T>(cb: () => T, measureName: string) => {
213+
const startMark = `${measureName}.start`;
214+
const endMark = `${measureName}.end`;
215+
216+
performance.mark(startMark);
217+
const data = cb();
218+
performance.mark(endMark);
219+
220+
const measure = performance.measure(measureName, startMark, endMark);
221+
const durationMs = measure.duration;
222+
console.debug(`[${measureName}] took ${durationMs}ms`);
223+
224+
return {
225+
data,
226+
durationMs
227+
}
228+
}
229+
230+
export const measure = async <T>(cb: () => Promise<T>, measureName: string) => {
231+
const startMark = `${measureName}.start`;
232+
const endMark = `${measureName}.end`;
233+
234+
performance.mark(startMark);
235+
const data = await cb();
236+
performance.mark(endMark);
237+
238+
const measure = performance.measure(measureName, startMark, endMark);
239+
const durationMs = measure.duration;
240+
console.debug(`[${measureName}] took ${durationMs}ms`);
241+
242+
return {
243+
data,
244+
durationMs
245+
}
246+
}

0 commit comments

Comments
 (0)