Skip to content

Commit

Permalink
Add a powerPreference option
Browse files Browse the repository at this point in the history
Not sure how to add this here's an attempt.

You can pass `powerPreference=low-power` or
`power-preference=high-performance` in the query string.
  • Loading branch information
greggman committed Jan 5, 2023
1 parent fd73609 commit d7f4a9e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 11 deletions.
2 changes: 2 additions & 0 deletions docs/intro/developing.md
Expand Up @@ -52,6 +52,8 @@ The following url parameters change how the harness runs:
- `runnow=1` runs all matching tests on page load.
- `debug=1` enables verbose debug logging from tests.
- `worker=1` runs the tests on a Web Worker instead of the main thread.
- `powerPreference=low-power` runs most tests passing `powerPreference: low-power` to `requestAdapter`
- `powerPreference=high-performance` runs most tests passing `powerPreference: high-performance` to `requestAdapter`

## Editor

Expand Down
26 changes: 15 additions & 11 deletions src/common/runtime/standalone.ts
Expand Up @@ -9,6 +9,7 @@ import { LiveTestCaseResult } from '../internal/logging/result.js';
import { parseQuery } from '../internal/query/parseQuery.js';
import { TestQueryLevel } from '../internal/query/query.js';
import { TestTreeNode, TestSubtree, TestTreeLeaf, TestTree } from '../internal/tree.js';
import { setDefaultRequestAdapterOptions } from '../util/navigator_gpu.js';
import { assert, ErrorWithExtra, unreachable } from '../util/util.js';

import { optionEnabled } from './helper/options.js';
Expand Down Expand Up @@ -46,6 +47,11 @@ stopButtonElem.addEventListener('click', () => {
stopRequested = true;
});

const powerPreference = new URLSearchParams(window.location.search).get('powerPreference');
if (powerPreference) {
setDefaultRequestAdapterOptions({ powerPreference: powerPreference as GPUPowerPreference });
}

dataCache.setStore({
load: async (path: string) => {
const response = await fetch(`data/${path}`);
Expand Down Expand Up @@ -435,17 +441,15 @@ void (async () => {

// Update the URL bar to match the exact current options.
{
let url = window.location.protocol + '//' + window.location.host + window.location.pathname;
url +=
'?' +
new URLSearchParams([
['runnow', runnow ? '1' : '0'],
['worker', worker ? '1' : '0'],
['debug', debug ? '1' : '0'],
['unroll_const_eval_loops', unrollConstEvalLoops ? '1' : '0'],
]).toString() +
'&' +
qs.map(q => 'q=' + q).join('&');
const params = {
runnow: runnow ? '1' : '0',
worker: worker ? '1' : '0',
debug: debug ? '1' : '0',
unroll_const_eval_loops: unrollConstEvalLoops ? '1' : '0',
...(powerPreference && { powerPreference }),
};
let url = `${window.location.protocol}//${window.location.host}${window.location.pathname}`;
url += `?${new URLSearchParams(params).toString()}&${qs.map(q => 'q=' + q).join('&')}`;
window.history.replaceState(null, '', url);
}

Expand Down
27 changes: 27 additions & 0 deletions src/common/util/navigator_gpu.ts
Expand Up @@ -32,6 +32,15 @@ export function setGPUProvider(provider: GPUProvider) {

let impl: GPU | undefined = undefined;

let defaultRequestAdapterOptions: GPURequestAdapterOptions | undefined;

export function setDefaultRequestAdapterOptions(options: GPURequestAdapterOptions) {
if (impl) {
throw new Error('must call setDefaultRequestAdapterOptions before getGPU');
}
defaultRequestAdapterOptions = { ...options };
}

/**
* Finds and returns the `navigator.gpu` object (or equivalent, for non-browser implementations).
* Throws an exception if not found.
Expand All @@ -43,5 +52,23 @@ export function getGPU(): GPU {

impl = gpuProvider();

if (defaultRequestAdapterOptions) {
// eslint-disable-next-line @typescript-eslint/unbound-method
const oldFn = impl.requestAdapter;
impl.requestAdapter = function (
options?: GPURequestAdapterOptions
): Promise<GPUAdapter | null> {
const promise = oldFn.call(this, { ...defaultRequestAdapterOptions, ...(options || {}) });
void promise.then(async adapter => {
if (adapter) {
const info = await adapter.requestAdapterInfo();
// eslint-disable-next-line no-console
console.log(info);
}
});
return promise;
};
}

return impl;
}

0 comments on commit d7f4a9e

Please sign in to comment.