Skip to content

Commit

Permalink
fix(stats): add a separate timing for measuring SSR time and latency …
Browse files Browse the repository at this point in the history
…to that alongside API fetch
  • Loading branch information
aleksasiriski committed May 14, 2024
1 parent 00883e3 commit afe67b8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
10 changes: 6 additions & 4 deletions src/lib/components/header/stats.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
/**
* @typedef {object} Props
* @property {number} numOfResults
* @property {number} timing
* @property {number} timingRender
* @property {number} timingApi
*/
/** @type {Props} */
let { numOfResults, timing } = $props();
const timingString = $derived((timing / 1000).toFixed(2));
let { numOfResults, timingRender, timingApi } = $props();
const timingRenderString = $derived((timingRender / 1000).toFixed(2));
const timingApiString = $derived((timingApi / 1000).toFixed(2));
</script>

<div id="stats" class="mx-auto px-2 pt-2 max-w-screen-sm">
<p class="text-sm text-neutral-800 dark:text-neutral-400">
Hearched {numOfResults} results in {timingString}s 🐹
Hearched {numOfResults} results in {timingRenderString}s (API took {timingApiString}s) 🐹
</p>
</div>
8 changes: 7 additions & 1 deletion src/routes/search/+page.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ export async function load({ data }) {
maxPages: data.maxPages,
category: data.category,
results: data.results,
timing: data.timing
timing: {
render: {
start: data.timing.render.start,
end: Date.now()
},
api: data.timing.api
}
};
}
17 changes: 14 additions & 3 deletions src/routes/search/+page.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ function getCategory(query, params) {

/** @type {import('./$types').PageServerLoad} */
export async function load({ url, fetch }) {
const renderStart = Date.now();

// Get query, current page, and max pages from URL
const query = url.searchParams.get('q') ?? '';
const currentPage = parseInt(url.searchParams.get('start') ?? '1', 10);
Expand Down Expand Up @@ -72,16 +74,25 @@ export async function load({ url, fetch }) {
});

// Fetch results
const timerStart = Date.now();
const fetchStart = Date.now();
const results = await fetchResults(newSearchParams, fetch);
const timerEnd = Date.now();
const fetchEnd = Date.now();

return {
query: queryWithoutCategory,
currentPage: currentPage,
maxPages: maxPages,
category: category,
results: results,
timing: timerEnd - timerStart
timing: {
render: {
start: renderStart,
end: Date.now()
},
api: {
start: fetchStart,
end: fetchEnd
}
}
};
}
5 changes: 3 additions & 2 deletions src/routes/search/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
// const maxPages = $derived(data.maxPages);
const category = $derived(data.category);
const results = $derived(data.results);
const timing = $derived(data.timing);
const timingRender = $derived(data.timing.render.end - data.timing.render.start);
const timingApi = $derived(data.timing.api.end - data.timing.api.start);
/** @type {ResultType | undefined} */
let imagePreview = $state(undefined);
Expand All @@ -40,7 +41,7 @@

<Header bind:query {category} />
{#if results.length !== 0}
<Stats numOfResults={results.length} {timing} />
<Stats numOfResults={results.length} {timingRender} {timingApi} />
{/if}

{#if results.length === 0}
Expand Down

0 comments on commit afe67b8

Please sign in to comment.