Skip to content

Commit c44e4a1

Browse files
dgozmanyury-s
authored andcommitted
cherry-pick(microsoft#31768): fix(trace viewer): library-only trace should not merge actions
Without `wallTime`, actions are matched by `actionName:undefined` and all actions with the same are merged. Fixes microsoft#31764.
1 parent ef99f59 commit c44e4a1

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

packages/trace-viewer/src/ui/modelUtil.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,8 @@ function mergeActionsAndUpdateTiming(contexts: ContextEntry[]) {
183183
if (traceFileToContexts.size > 1)
184184
makeCallIdsUniqueAcrossTraceFiles(contexts, ++traceFileId);
185185
// Align action times across runner and library contexts within each trace file.
186-
const map = mergeActionsAndUpdateTimingSameTrace(contexts);
187-
result.push(...map.values());
186+
const actions = mergeActionsAndUpdateTimingSameTrace(contexts);
187+
result.push(...actions);
188188
}
189189
result.sort((a1, a2) => {
190190
if (a2.parentId === a1.callId)
@@ -211,19 +211,26 @@ function makeCallIdsUniqueAcrossTraceFiles(contexts: ContextEntry[], traceFileId
211211
}
212212
}
213213

214-
function mergeActionsAndUpdateTimingSameTrace(contexts: ContextEntry[]) {
214+
function mergeActionsAndUpdateTimingSameTrace(contexts: ContextEntry[]): ActionTraceEventInContext[] {
215215
const map = new Map<string, ActionTraceEventInContext>();
216216

217217
const libraryContexts = contexts.filter(context => context.origin === 'library');
218218
const testRunnerContexts = contexts.filter(context => context.origin === 'testRunner');
219219

220+
// With library-only or test-runner-only traces there is nothing to match.
221+
if (!testRunnerContexts.length || !libraryContexts.length) {
222+
return contexts.map(context => {
223+
return context.actions.map(action => ({ ...action, context }));
224+
}).flat();
225+
}
226+
220227
// Library actions are replaced with corresponding test runner steps. Matching with
221228
// the test runner steps enables us to find parent steps.
222229
// - In the newer versions the actions are matched by explicit step id stored in the
223230
// library context actions.
224231
// - In the older versions the step id is not stored and the match is perfomed based on
225232
// action name and wallTime.
226-
const matchByStepId = !libraryContexts.length || libraryContexts.some(c => c.actions.some(a => !!a.stepId));
233+
const matchByStepId = libraryContexts.some(c => c.actions.some(a => !!a.stepId));
227234

228235
for (const context of libraryContexts) {
229236
for (const action of context.actions) {
@@ -264,7 +271,7 @@ function mergeActionsAndUpdateTimingSameTrace(contexts: ContextEntry[]) {
264271
map.set(key, { ...action, context });
265272
}
266273
}
267-
return map;
274+
return [...map.values()];
268275
}
269276

270277
function adjustMonotonicTime(contexts: ContextEntry[], monotonicTimeDelta: number) {
1.2 KB
Binary file not shown.

tests/library/trace-viewer.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,3 +1236,24 @@ test('should open snapshot in new browser context', async ({ browser, page, runA
12361236
await expect(newPage.getByText('hello')).toBeVisible();
12371237
await newPage.close();
12381238
});
1239+
1240+
test('should show similar actions from library-only trace', async ({ showTraceViewer, asset }) => {
1241+
const traceViewer = await showTraceViewer([asset('trace-library-1.46.zip')]);
1242+
await expect(traceViewer.actionTitles).toHaveText([
1243+
/page.setContent/,
1244+
/locator.getAttributelocator\('div'\)/,
1245+
/locator.isVisiblelocator\('div'\)/,
1246+
/locator.getAttributelocator\('div'\)/,
1247+
/locator.isVisiblelocator\('div'\)/,
1248+
]);
1249+
});
1250+
1251+
function parseMillis(s: string): number {
1252+
const matchMs = s.match(/(\d+)ms/);
1253+
if (matchMs)
1254+
return +matchMs[1];
1255+
const matchSeconds = s.match(/([\d.]+)s/);
1256+
if (!matchSeconds)
1257+
throw new Error('Failed to parse to millis: ' + s);
1258+
return (+matchSeconds[1]) * 1000;
1259+
}

0 commit comments

Comments
 (0)