Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make usb-power an optional package #2249

Merged
merged 2 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/android/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { EOL as endOfLine } from 'node:os';
import { execa } from 'execa';
import { getLogger } from '@sitespeed.io/log';
import pkg from '@devicefarmer/adbkit';
import usbPowerProfiler from 'usb-power-profiling/usb-power-profiling.js';
const { Adb } = pkg;
import get from 'lodash.get';
import { pathToFolder } from '../support/pathToFolder.js';
import { loadUsbPowerProfiler } from '../support/usbPower.js';
const log = getLogger('browsertime.android');
const mkdir = promisify(_mkdir);
const delay = ms => new Promise(res => setTimeout(res, ms));
Expand Down Expand Up @@ -474,6 +474,11 @@ export class Android {
}

async getUsbPowerUsageProfile(index, url, result, options, storageManager) {
const usbPowerProfiler = await loadUsbPowerProfiler();
if (!usbPowerProfiler) {
log.info('USB profiler is not installed');
return;
}
let profileData = await usbPowerProfiler.profileFromData();
let destinationFilename = path.join(
await pathToFolder(url, options),
Expand All @@ -485,6 +490,11 @@ export class Android {
}

async function getUsbPowerUsage(startTime, endTime) {
const usbPowerProfiler = await loadUsbPowerProfiler();
if (!usbPowerProfiler) {
log.info('USB profiler is not installed');
return;
}
let baselineUsageData = await usbPowerProfiler.getPowerData(
startTime - 2,
endTime - 1
Expand Down
17 changes: 13 additions & 4 deletions lib/chrome/webdriver/chromium.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { unlink as _unlink, rm as _rm } from 'node:fs';
import path from 'node:path';
import { logging } from 'selenium-webdriver';
import { getLogger } from '@sitespeed.io/log';
import usbPowerProfiler from 'usb-power-profiling/usb-power-profiling.js';
const log = getLogger('browsertime.chrome');
const { Type } = logging;
import { longTaskMetrics } from '../longTaskMetrics.js';
Expand All @@ -14,6 +13,7 @@ import { toArray } from '../../support/util.js';
import { logging as _logging } from 'selenium-webdriver';
import { parse } from '../traceCategoriesParser.js';
import { pathToFolder } from '../../support/pathToFolder.js';
import { loadUsbPowerProfiler } from '../../support/usbPower.js';
import { ChromeDevtoolsProtocol } from '../chromeDevtoolsProtocol.js';
import { NetworkManager } from '../networkManager.js';
import { Android, isAndroidConfigured } from '../../android/index.js';
Expand Down Expand Up @@ -80,7 +80,10 @@ export class Chromium {
await this.android.addDevtoolsFw();

if (this.options.androidUsbPower) {
usbPowerProfiler.startSampling();
const usbPowerProfiler = await loadUsbPowerProfiler();
if (usbPowerProfiler) {
usbPowerProfiler.startSampling();
}
}
}

Expand Down Expand Up @@ -171,7 +174,10 @@ export class Chromium {
if (this.options.androidPower) {
await this.android.resetPowerUsage();
} else if (this.options.androidUsbPower) {
await usbPowerProfiler.resetPowerData();
const usbPowerProfiler = await loadUsbPowerProfiler();
if (usbPowerProfiler) {
await usbPowerProfiler.resetPowerData();
}
}
}

Expand Down Expand Up @@ -511,7 +517,10 @@ export class Chromium {
if (this.options.androidPower) {
await this.android.stopPowerTesting();
} else if (this.options.androidUsbPower) {
await usbPowerProfiler.stopSampling();
const usbPowerProfiler = await loadUsbPowerProfiler();
if (usbPowerProfiler) {
await usbPowerProfiler.stopSampling();
}
}
}

Expand Down
17 changes: 13 additions & 4 deletions lib/firefox/webdriver/firefox.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { promisify } from 'node:util';
import path from 'node:path';
import { getLogger } from '@sitespeed.io/log';
import get from 'lodash.get';
import usbPowerProfiler from 'usb-power-profiling/usb-power-profiling.js';
import { adapters } from 'ff-test-bidi-har-export';
import { getEmptyHAR, mergeHars } from '../../support/har/index.js';
import { loadUsbPowerProfiler } from '../../support/usbPower.js';
import { pathToFolder } from '../../support/pathToFolder.js';
import { isAndroidConfigured, Android } from '../../android/index.js';
import { adjustVisualProgressTimestamps } from '../../support/util.js';
Expand Down Expand Up @@ -75,7 +75,10 @@ export class Firefox {
);

if (isAndroidConfigured(this.options) && this.options.androidUsbPower) {
usbPowerProfiler.startSampling();
const usbPowerProfiler = await loadUsbPowerProfiler();
if (usbPowerProfiler) {
usbPowerProfiler.startSampling();
}
}
}

Expand Down Expand Up @@ -144,7 +147,10 @@ export class Firefox {
if (this.options.androidPower) {
await this.android.resetPowerUsage();
} else if (this.options.androidUsbPower) {
await usbPowerProfiler.resetPowerData();
const usbPowerProfiler = await loadUsbPowerProfiler();
if (usbPowerProfiler) {
await usbPowerProfiler.resetPowerData();
}
}
}

Expand Down Expand Up @@ -408,7 +414,10 @@ export class Firefox {
if (this.options.androidPower) {
await this.android.stopPowerTesting();
} else if (this.options.androidUsbPower) {
await usbPowerProfiler.stopSampling();
const usbPowerProfiler = await loadUsbPowerProfiler();
if (usbPowerProfiler) {
await usbPowerProfiler.stopSampling();
}
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions lib/support/usbPower.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export async function loadUsbPowerProfiler() {
try {
// usb-power-profiling/usb-power-profiling.js exports a default, so we destructure it
const { default: usbPowerProfiler } = await import(
'usb-power-profiling/usb-power-profiling.js'
);
return usbPowerProfiler;
} catch {
return;
}
}
Loading
Loading