Skip to content

Commit

Permalink
Track timings for fetching library versions
Browse files Browse the repository at this point in the history
  • Loading branch information
MattIPv4 committed Apr 1, 2024
1 parent 56ba903 commit b2b978c
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion util/build/routes.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import chunk from 'chunk';
import fetch from 'node-fetch';
import consola from 'consola';
import { baseApi } from '../../data/config';

export default async () => {
// Create the lib promises
// Fetch the library names
const libsStart = Date.now();
const libsRaw = await fetch(`${baseApi}/libraries?fields=name`);
const libsJson = await libsRaw.json();
consola.debug(` Fetched ${libsJson.results.length} libraries in ${Date.now() - libsStart}ms`);

// Track timings for individual libraries
const timings = [];

// Create the lib promises
const libsAsync = libsJson.results.map((lib) => {
return [lib.name, async () => {
const start = Date.now();
const libRaw = await fetch(`${baseApi}/libraries/${encodeURIComponent(lib.name)}?fields=versions`);
const libJson = await libRaw.json();

Expand All @@ -22,6 +31,8 @@ export default async () => {
};
});

timings.push([lib.name, Date.now() - start]);

return [
{
url: `/libraries/${encodeURIComponent(lib.name)}`,
Expand All @@ -33,6 +44,7 @@ export default async () => {
});

// Split into chunks and fetch
const libsChunksStart = Date.now();
const libsChunks = chunk(libsAsync, 100);
const failed = [];
const libs = [];
Expand All @@ -45,6 +57,13 @@ export default async () => {
if (result) { libs.push(...result); }
}

// Report the timings
consola.debug(` Fetched ${libs.length} libraries in ${Date.now() - libsChunksStart}ms`);
timings.sort((a, b) => b[1] - a[1]);
consola.debug(` p99: ${timings[Math.floor(timings.length * 0.99)][1]}ms`);
consola.debug(` p90: ${timings[Math.floor(timings.length * 0.9)][1]}ms`);
consola.debug(` p50: ${timings[Math.floor(timings.length * 0.5)][1]}ms`);

// Ensure everything is valid & sort by URL
return libs.filter(x => !!x && !!x.url).sort((a, b) => a.url.localeCompare(b.url));
};

0 comments on commit b2b978c

Please sign in to comment.