Skip to content

Commit

Permalink
Make usb-power an optional package
Browse files Browse the repository at this point in the history
  • Loading branch information
soulgalore committed Jan 10, 2025
1 parent e4f45d9 commit 43a3d0f
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 47 deletions.
12 changes: 11 additions & 1 deletion lib/android/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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';
import { loadUsbPowerProfiler } from './support/usbPower.js';
const { Adb } = pkg;
import get from 'lodash.get';
import { pathToFolder } from '../support/pathToFolder.js';
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,7 @@ 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';
import { loadUsbPowerProfiler } from './support/usbPower.js';
const log = getLogger('browsertime.chrome');
const { Type } = logging;
import { longTaskMetrics } from '../longTaskMetrics.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,7 +3,7 @@ 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 { loadUsbPowerProfiler } from './support/usbPower.js';
import { adapters } from 'ff-test-bidi-har-export';
import { getEmptyHAR, mergeHars } from '../../support/har/index.js';
import { pathToFolder } from '../../support/pathToFolder.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

0 comments on commit 43a3d0f

Please sign in to comment.