Skip to content

Commit

Permalink
cherry-pick(#33244): fix(trace viewer): limit the number of contexts …
Browse files Browse the repository at this point in the history
…loaded in sw (#33261)
  • Loading branch information
dgozman authored Oct 24, 2024
1 parent a96f483 commit ff1932b
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 12 deletions.
31 changes: 19 additions & 12 deletions packages/trace-viewer/src/sw/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@ const scopePath = new URL(self.registration.scope).pathname;

const loadedTraces = new Map<string, { traceModel: TraceModel, snapshotServer: SnapshotServer }>();

const clientIdToTraceUrls = new Map<string, Set<string>>();
const clientIdToTraceUrls = new Map<string, { limit: number | undefined, traceUrls: Set<string> }>();

async function loadTrace(traceUrl: string, traceFileName: string | null, clientId: string, progress: (done: number, total: number) => undefined): Promise<TraceModel> {
async function loadTrace(traceUrl: string, traceFileName: string | null, clientId: string, limit: number | undefined, progress: (done: number, total: number) => undefined): Promise<TraceModel> {
await gc();
let set = clientIdToTraceUrls.get(clientId);
if (!set) {
set = new Set();
clientIdToTraceUrls.set(clientId, set);
let data = clientIdToTraceUrls.get(clientId);
if (!data) {
data = { limit, traceUrls: new Set() };
clientIdToTraceUrls.set(clientId, data);
}
set.add(traceUrl);
data.traceUrls.add(traceUrl);

const traceModel = new TraceModel();
try {
Expand Down Expand Up @@ -97,7 +97,8 @@ async function doFetch(event: FetchEvent): Promise<Response> {

if (relativePath === '/contexts') {
try {
const traceModel = await loadTrace(traceUrl!, url.searchParams.get('traceFileName'), event.clientId, (done: number, total: number) => {
const limit = url.searchParams.has('limit') ? +url.searchParams.get('limit')! : undefined;
const traceModel = await loadTrace(traceUrl!, url.searchParams.get('traceFileName'), event.clientId, limit, (done: number, total: number) => {
client.postMessage({ method: 'progress', params: { done, total } });
});
return new Response(JSON.stringify(traceModel!.contextEntries), {
Expand Down Expand Up @@ -172,12 +173,18 @@ async function gc() {
const clients = await self.clients.matchAll();
const usedTraces = new Set<string>();

for (const [clientId, traceUrls] of clientIdToTraceUrls) {
for (const [clientId, data] of clientIdToTraceUrls) {
// @ts-ignore
if (!clients.find(c => c.id === clientId))
if (!clients.find(c => c.id === clientId)) {
clientIdToTraceUrls.delete(clientId);
else
traceUrls.forEach(url => usedTraces.add(url));
continue;
}
if (data.limit !== undefined) {
const ordered = [...data.traceUrls];
// Leave the newest requested traces.
data.traceUrls = new Set(ordered.slice(ordered.length - data.limit));
}
data.traceUrls.forEach(url => usedTraces.add(url));
}

for (const traceUrl of loadedTraces.keys()) {
Expand Down
1 change: 1 addition & 0 deletions packages/trace-viewer/src/ui/embeddedWorkbenchLoader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export const EmbeddedWorkbenchLoader: React.FunctionComponent = () => {
const url = traceURLs[i];
const params = new URLSearchParams();
params.set('trace', url);
params.set('limit', String(traceURLs.length));
const response = await fetch(`contexts?${params.toString()}`);
if (!response.ok) {
setProcessingErrorMessage((await response.json()).error);
Expand Down
1 change: 1 addition & 0 deletions packages/trace-viewer/src/ui/recorder/modelContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export const ModelProvider: React.FunctionComponent<React.PropsWithChildren<{
async function loadSingleTraceFile(url: string): Promise<{ model: MultiTraceModel, sha1: string }> {
const params = new URLSearchParams();
params.set('trace', url);
params.set('limit', '1');
const response = await fetch(`contexts?${params.toString()}`);
const contextEntries = await response.json() as ContextEntry[];

Expand Down
1 change: 1 addition & 0 deletions packages/trace-viewer/src/ui/uiModeTraceView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ const outputDirForTestCase = (testCase: reporterTypes.TestCase): string | undefi
async function loadSingleTraceFile(url: string): Promise<MultiTraceModel> {
const params = new URLSearchParams();
params.set('trace', url);
params.set('limit', '1');
const response = await fetch(`contexts?${params.toString()}`);
const contextEntries = await response.json() as ContextEntry[];
return new MultiTraceModel(contextEntries);
Expand Down
1 change: 1 addition & 0 deletions packages/trace-viewer/src/ui/workbenchLoader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ export const WorkbenchLoader: React.FunctionComponent<{
params.set('trace', url);
if (uploadedTraceNames.length)
params.set('traceFileName', uploadedTraceNames[i]);
params.set('limit', String(traceURLs.length));
const response = await fetch(`contexts?${params.toString()}`);
if (!response.ok) {
if (!isServer)
Expand Down

0 comments on commit ff1932b

Please sign in to comment.