Skip to content

Commit

Permalink
refactor(cli, core): tracing logic
Browse files Browse the repository at this point in the history
  • Loading branch information
lynchbomb committed Oct 28, 2019
1 parent a3c1d18 commit 6561ce5
Show file tree
Hide file tree
Showing 14 changed files with 213 additions and 1,060 deletions.
16 changes: 16 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,22 @@
"internalConsoleOptions": "neverOpen",
"protocol": "inspector"
},
{
"type": "node",
"request": "launch",
"name": "Only CLI Trace",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"runtimeArgs": ["--nolazy", "-r", "ts-node/register"],
"args": [
"--no-timeout",
"${workspaceFolder}/packages/cli/test/commands/trace.test.ts"
],
"sourceMaps": true,
"cwd": "${workspaceFolder}/packages/cli",
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"protocol": "inspector"
},
{
"type": "node",
"request": "launch",
Expand Down
901 changes: 0 additions & 901 deletions CHANGELOG.md

This file was deleted.

3 changes: 3 additions & 0 deletions packages/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,9 @@ OPTIONS
--locations=locations
include locations in names
--marker=marker
(required) [default: domComplete] The last marker before ending recording
--network=none | offline | dialup | 2g | edge | slow-3g | em-3g | dsl | 3g | fast-3g | 4g | cable | LTE | FIOS
[default: none] Simulated network conditions.
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/oclif.manifest.json

Large diffs are not rendered by default.

66 changes: 2 additions & 64 deletions packages/cli/src/commands/record-har.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { readJson, writeFileSync } from 'fs-extra';
import { resolve, join } from 'path';
import { setGracefulCleanup, dirSync } from 'tmp';
import { recordHARClient } from '@tracerbench/core';
import { setGracefulCleanup } from 'tmp';
import { recordHARClient, getBrowserArgs } from '@tracerbench/core';

import { TBBaseCommand } from '../command-config';
import { dest, url, cookiespath, filename, marker } from '../helpers/flags';
Expand Down Expand Up @@ -40,65 +40,3 @@ export default class RecordHAR extends TBBaseCommand {
this.log(`HAR recorded and available here: ${harPath}`);
}
}

function getBrowserArgs(): string[] {
interface IViewOptions {
windowSize: {
width: number;
height: number;
};
deviceScaleFactor: number;
userAgent: string | undefined;
}

const tmpDir = dirSync({
unsafeCleanup: true,
});

const options: IViewOptions = {
windowSize: {
width: 1280,
height: 800,
},
deviceScaleFactor: 0,
userAgent: undefined,
};

return [
`--crash-dumps-dir=${tmpDir.name}`,
'--disable-background-networking',
'--disable-background-timer-throttling',
'--disable-backgrounding-occluded-windows',
'--disable-component-extensions-with-background-pages',
'--disable-client-side-phishing-detection',
'--disable-default-apps',
'--disable-dev-shm-usage',
'--disable-domain-reliability',
'--disable-extensions',
'--disable-features=NetworkPrediction',
'--disable-features=site-per-process,TranslateUI,BlinkGenPropertyTrees',
'--disable-hang-monitor',
'--disable-ipc-flooding-protection',
'--disable-notifications',
'--disable-renderer-backgrounding',
'--disable-sync',
'--disable-translate',
'--disable-v8-idle-tasks',
`--device-scale-factor=${options.deviceScaleFactor}`,
'--ignore-certificate-errors-spki-list=uU0W87bsSHNaY+g/o8S9PmyxIgf92JepLWrPg5bYb+s=',
'--metrics-recording-only',
'--no-pings',
'--no-first-run',
'--no-default-browser-check',
'--no-experiments',
'--no-sandbox',
'--password-store=basic',
'--safebrowsing-disable-auto-update',
'--use-mock-keychain',
`--user-agent=${options.userAgent}`,
`--user-data-dir=${tmpDir.name}`,
'--v8-cache-options=none',
`--window-size=${options.windowSize.width},${options.windowSize.height}`,
'--headless',
];
}
28 changes: 19 additions & 9 deletions packages/cli/src/commands/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
locations,
harpath,
cookiespath,
marker,
} from '../helpers/flags';
import {
normalizeFnName,
Expand All @@ -37,6 +38,7 @@ export default class Trace extends TBBaseCommand {
cookiespath: cookiespath({ required: true }),
iterations: iterations({ required: true }),
locations: locations(),
marker: marker({ required: true }),
insights,
};

Expand All @@ -51,28 +53,36 @@ export default class Trace extends TBBaseCommand {
locations,
network,
harpath,
marker,
} = flags;

const methods = [''];

const traceJSONFile = path.resolve(tbResultsFolder, 'trace.json');
const traceJSON = await readJson(traceJSONFile);
const cookiesJSON = await readJson(path.resolve(cookiespath));
const traceHAR = path.resolve(harpath);
const traceHARJSON = await readJson(traceHAR);
const conditions: IConditions = {
cpu: cpuThrottleRate,
network,
};

console.log(`running live trace`);

// run the liveTrace
const { traceEvents } = await liveTrace(
url,
tbResultsFolder,
cookiesJSON,
conditions,
marker
);

const analyzeOptions: IAnalyze = {
traceJSON,
traceEvents,
traceHARJSON,
methods,
};

// run the liveTrace
await liveTrace(url, traceJSONFile, cookiesJSON, conditions);

// analyze the liveTrace
await analyze(analyzeOptions);

Expand All @@ -85,7 +95,7 @@ export default class Trace extends TBBaseCommand {
const methods = new Set();

try {
trace = setTraceEvents(traceJSON);
trace = setTraceEvents(traceEvents);
} catch (error) {
this.error(`${error}`);
}
Expand Down Expand Up @@ -149,7 +159,7 @@ export default class Trace extends TBBaseCommand {
}

try {
trace = setTraceEvents(traceJSON);
trace = setTraceEvents(traceEvents);
const traceLoad = trace.filter(isCommitLoad);
traceLoad.forEach(
({
Expand All @@ -166,6 +176,6 @@ export default class Trace extends TBBaseCommand {
this.error(`${error}`);
}
}
return this.log(`${traceJSON}`);
return this.log(`${traceEvents}`);
}
}
36 changes: 6 additions & 30 deletions packages/cli/test/commands/trace.test.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,20 @@
import { test } from '@oclif/test';
import { expect } from 'chai';
import { readJsonSync } from 'fs-extra';
import { join } from 'path';

import Trace from '../../src/commands/trace';
import {
COOKIES,
HAR_PATH,
TB_RESULTS_FOLDER,
URL,
generateFileStructure,
MARKER,
} from '../test-helpers';

const TRACE_JSON = {
'trace.json': JSON.stringify(
readJsonSync(join(process.cwd(), '/test/fixtures/results/trace.json'))
),
};

generateFileStructure(TRACE_JSON, TB_RESULTS_FOLDER);

describe('trace', () => {
describe('trace: insights', () => {
test
.stdout()
.it(
`runs trace --url ${URL} --tbResultsFolder ${TB_RESULTS_FOLDER}`,
`runs trace --url ${URL} --tbResultsFolder ${TB_RESULTS_FOLDER} --harpath ${HAR_PATH} --cookiespath ${COOKIES} --marker ${MARKER}--insights`,
async ctx => {
await Trace.run([
'--url',
Expand All @@ -35,26 +25,12 @@ describe('trace', () => {
HAR_PATH,
'--cookiespath',
COOKIES,
'--marker',
MARKER,
'--insights',
]);
expect(ctx.stdout).to.contain(`Trace`);
expect(ctx.stdout).to.contain(`Subtotal`);
}
);
});

describe('trace: insights', () => {
test
.stdout()
.it(
`runs trace --url ${URL} --tbResultsFolder ${TB_RESULTS_FOLDER} --insights`,
async ctx => {
await Trace.run([
'--url',
URL,
'--tbResultsFolder',
TB_RESULTS_FOLDER,
'--insights',
]);
expect(ctx.stdout).to.contain(`.js`);
expect(ctx.stdout).to.contain(`.css`);
expect(ctx.stdout).to.contain(`Frame-URL:`);
Expand Down
3 changes: 1 addition & 2 deletions packages/cli/test/test-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ export const URL = 'https://www.tracerbench.com';
export const COMPARE_JSON = resolve(
join(process.cwd(), '/test/fixtures/results/compare.json')
);
export const TRACE = resolve(join(process.cwd(), '/test/fixtures/trace.json'));

export const MARKER = 'domComplete';
export interface FileStructure {
[key: string]: string | FileStructure;
}
Expand Down
1 change: 1 addition & 0 deletions packages/tracerbench/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@ export {
IHeaders,
IContent,
IEntry,
getBrowserArgs,
} from './trace';
export * from './util';
15 changes: 11 additions & 4 deletions packages/tracerbench/src/trace/analyze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
} from '../trace';

export interface IAnalyze {
traceJSON: ITraceEvent[] | ITrace;
traceEvents: ITraceEvent[] | ITrace;
traceHARJSON: IArchive;
methods: string[];
filename?: string;
Expand All @@ -36,13 +36,20 @@ export interface IAnalyze {
}

export async function analyze(options: IAnalyze) {
const { traceHARJSON, event, filename, traceJSON, report, methods } = options;
const trace = loadTrace(traceJSON);
const {
traceHARJSON,
event,
filename,
traceEvents,
report,
methods,
} = options;
const trace = loadTrace(traceEvents);
const profile = getCPUProfile(trace, event)!;
const { hierarchy } = profile;

const modMatcher = new ModuleMatcher(hierarchy, traceHARJSON);
exportHierarchy(traceJSON, hierarchy, trace, filename, modMatcher);
exportHierarchy(traceEvents, hierarchy, trace, filename, modMatcher);

const categories = formatCategories(report, methods);
const allMethods = methodsFromCategories(categories);
Expand Down
9 changes: 7 additions & 2 deletions packages/tracerbench/src/trace/archive_trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ export async function recordHARClient(
},
};
const browser = await createBrowser(browserArgs);
const chrome = await getTab(browser.connection);

try {
const chrome = await getTab(browser.connection);

chrome.on('Network.requestWillBeSent', params => {
console.log(
`RECORDING-REQUEST :: ${params.request.method} :: ${params.type} :: ${params.request.url}`
Expand All @@ -66,6 +67,10 @@ export async function recordHARClient(
await chrome.send('Network.clearBrowserCache');
// disable cache
await chrome.send('Network.setCacheDisabled', { cacheDisabled: true });

// !todo add device emulation flag/args
// await emulate(chrome, conditions);

// set cookies
await setCookies(chrome, cookies);
// add performance observer script to eval
Expand Down Expand Up @@ -113,7 +118,7 @@ export async function recordHARClient(

await chrome.send('Page.close');
} catch (e) {
throw new Error('Network Request could not be captured. ' + e);
throw new Error(`Network Request could not be captured. ${e}`);
} finally {
if (browser) {
await browser.dispose();
Expand Down
1 change: 1 addition & 0 deletions packages/tracerbench/src/trace/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export { loadTrace } from './load_trace';
export { liveTrace } from './live_trace';
export { networkConditions, IConditions } from './conditions';
export * from './trace_event';
export { getBrowserArgs } from './utils';
Loading

0 comments on commit 6561ce5

Please sign in to comment.