forked from Blendman974/faceclaw
CPU usage optimizations
This commit is contained in:
1 parent
95b2ca8afa
commit
3601a998a3
17 files changed
+494
-94
No files matched your search
@@ -0,0 +1,77 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
const vm = require('node:vm');
|
||||
|
||||
const source = fs.readFileSync(path.join(__dirname, '../app/apps/evenhub/session.ts'), 'utf8');
|
||||
const shim = source.match(/export const EVENHUB_BRIDGE_INJECT_SCRIPT = `([\s\S]*?)`;/)[1];
|
||||
|
||||
function page() {
|
||||
const state = { now: 1000, wakes: 0, log: [] };
|
||||
const win = {
|
||||
Date: { now: () => state.now }, console, Map, Array, Math,
|
||||
__faceclawEvenHub: { postMessage() {}, wakeTimers() { state.wakes++; } },
|
||||
};
|
||||
win.window = win;
|
||||
win.performance = { now: () => state.now };
|
||||
vm.createContext(win);
|
||||
vm.runInContext(shim, win);
|
||||
state.win = win;
|
||||
state.tick = () => vm.runInContext('__fcTick()', win);
|
||||
return state;
|
||||
}
|
||||
|
||||
test('an idle page asks for no ticks, and its first timer wakes the host once', () => {
|
||||
const p = page();
|
||||
assert.equal(p.tick(), -1);
|
||||
p.win.setTimeout(() => p.log.push('a'), 100);
|
||||
p.win.setTimeout(() => p.log.push('b'), 200);
|
||||
assert.equal(p.wakes, 1);
|
||||
p.now += 5;
|
||||
assert.equal(p.tick(), 95);
|
||||
p.now += 95;
|
||||
assert.equal(p.tick(), 100);
|
||||
assert.deepEqual(p.log, ['a']);
|
||||
p.now += 100;
|
||||
assert.equal(p.tick(), -1);
|
||||
assert.deepEqual(p.log, ['a', 'b']);
|
||||
});
|
||||
|
||||
test('work due before the scheduled tick wakes the host; work added during a tick does not', () => {
|
||||
const p = page();
|
||||
p.win.setTimeout(() => {}, 500);
|
||||
p.tick();
|
||||
const wakes = p.wakes;
|
||||
p.win.setTimeout(() => { p.win.setTimeout(() => p.log.push('inner'), 0); }, 10);
|
||||
assert.equal(p.wakes, wakes + 1);
|
||||
p.now += 10;
|
||||
assert.equal(p.tick(), 16, 'the inner zero-delay timer runs next frame');
|
||||
assert.equal(p.wakes, wakes + 1);
|
||||
p.now += 16;
|
||||
p.tick();
|
||||
assert.deepEqual(p.log, ['inner']);
|
||||
});
|
||||
|
||||
test('an animation-frame loop keeps the host at frame rate until it stops', () => {
|
||||
const p = page();
|
||||
let frames = 0;
|
||||
const loop = () => { if (++frames < 3) p.win.requestAnimationFrame(loop); };
|
||||
p.win.requestAnimationFrame(loop);
|
||||
p.now += 16; assert.equal(p.tick(), 16);
|
||||
p.now += 16; assert.equal(p.tick(), 16);
|
||||
p.now += 16; assert.equal(p.tick(), -1);
|
||||
assert.equal(frames, 3);
|
||||
});
|
||||
|
||||
test('intervals keep cadence and the fixed-rate entry points still work', () => {
|
||||
const p = page();
|
||||
const id = p.win.setInterval(() => p.log.push('iv'), 50);
|
||||
p.now += 50; assert.equal(p.tick(), 50);
|
||||
p.now += 50; p.tick();
|
||||
p.win.clearInterval(id);
|
||||
assert.equal(p.tick(), -1);
|
||||
p.win.setTimeout(() => p.log.push('legacy'), 0);
|
||||
vm.runInContext('__fcTimerTick(); __fcRafTick()', p.win);
|
||||
assert.deepEqual(p.log, ['iv', 'iv', 'legacy']);
|
||||
});
|
||||
@@ -36,6 +36,46 @@ class DisplayListTest {
|
||||
renderer.render(DrawProtocol.SCREEN, DisplayListRenderer.Target(source,640,480), DisplayListRenderer.Target(destination,640,480))
|
||||
assertContentEquals(source, destination)
|
||||
}
|
||||
@Test fun rectCopyMatchesPerPixelReferenceWithShiftsClipsAndAliasing() {
|
||||
val random = Random(7)
|
||||
repeat(4000) { case ->
|
||||
val width = random.nextInt(1, 20); val height = random.nextInt(1, 6)
|
||||
val offset = random.nextInt(3); val shift = random.nextInt(-4, 5)
|
||||
val clip = if (random.nextBoolean()) null else {
|
||||
val left = random.nextInt(-3, width + 1); val top = random.nextInt(-2, height + 1)
|
||||
intArrayOf(left, top, left + random.nextInt(0, width + 3), top + random.nextInt(0, height + 2))
|
||||
}
|
||||
// Screen as a separate array, the target's own array at another offset, or the target itself.
|
||||
val mode = random.nextInt(3)
|
||||
val screenWidth = random.nextInt(1, 20); val screenHeight = random.nextInt(1, 6)
|
||||
val screenOffset = random.nextInt(3)
|
||||
val targetSize = offset + (width + 1) / 2 * height
|
||||
val screenSize = screenOffset + (screenWidth + 1) / 2 * screenHeight
|
||||
val bytes = random.nextBytes(maxOf(targetSize, if (mode == 1) screenSize else 0) + 2)
|
||||
val screenBytes = if (mode == 0) random.nextBytes(screenSize + 1) else bytes
|
||||
val sourceId = if (mode == 2) DrawProtocol.CURRENT else DrawProtocol.SCREEN
|
||||
val sourceWidth = if (mode == 2) width else screenWidth
|
||||
val sourceHeight = if (mode == 2) height else screenHeight
|
||||
val w = random.nextInt(1, sourceWidth + 1); val h = random.nextInt(1, sourceHeight + 1)
|
||||
val x = random.nextInt(sourceWidth - w + 1); val y = random.nextInt(sourceHeight - h + 1)
|
||||
val dx = random.nextInt(-w - 5, width + 5); val dy = random.nextInt(-h - 2, height + 2)
|
||||
|
||||
val expected = bytes.copyOf()
|
||||
val expectedScreen = if (mode == 0) screenBytes.copyOf() else expected
|
||||
val referenceTarget = DisplayListRenderer.Target(expected, width, height, offset, shift, clip)
|
||||
val referenceSource = if (mode == 2) referenceTarget
|
||||
else DisplayListRenderer.Target(expectedScreen, screenWidth, screenHeight, screenOffset)
|
||||
val snapshot = IntArray(w * h) { referenceSource.get(x + it % w, y + it / w) }
|
||||
for (i in snapshot.indices) referenceTarget.put(dx + i % w, dy + i / w, snapshot[i])
|
||||
|
||||
val target = DisplayListRenderer.Target(bytes, width, height, offset, shift, clip)
|
||||
val screen = if (mode == 2) target else DisplayListRenderer.Target(screenBytes, screenWidth, screenHeight, screenOffset)
|
||||
DisplayListRenderer(emptyMap()).execute(
|
||||
DrawProtocol.sequence(listOf(DrawProtocol.rectCopy(sourceId, x, y, w, h, dx, dy))), target, screen)
|
||||
assertContentEquals(expected, bytes, "case $case")
|
||||
if (mode == 0) assertContentEquals(expectedScreen, screenBytes, "case $case screen")
|
||||
}
|
||||
}
|
||||
@Test fun invalidGraphDoesNotCopyScreenIntoComposition() {
|
||||
val screen = hex("12345678"); val composition = hex("9abcdef0")
|
||||
val resources = mapOf(1 to DrawProtocol.displayList(listOf(byteArrayOf(99,0))))
|
||||
|
||||
@@ -206,7 +206,7 @@ test('bridge exposes numeric measurements and treats absent values as unknown',
|
||||
let pump = { reservoir: '10.5', battery: { voltage: '1.15' } };
|
||||
const module = load('app/native/nightscout-bridge.ts', {
|
||||
'../ui/dashboard-settings': { nightscoutSiteUrlSetting: { get: () => 'https://example.test' }, nightscoutApiTokenSetting: { get: () => 'test' } },
|
||||
'../util/http': { async fetchWithUserAgent(url) {
|
||||
'../util/http': { async fetchTextWithUserAgent(url) {
|
||||
const body = url.includes('/entries.') ? [{ date: now, sgv: 100 }] : url.includes('/devicestatus.') ? [{ pump, openaps: { suggested: { mills: now - 60_000 } } }] : url.includes('/status.') ? {} : [];
|
||||
return { ok: true, json: async () => body };
|
||||
} },
|
||||
@@ -231,7 +231,7 @@ test('polling starts once and stops even while the initial HTTP request is pendi
|
||||
const timers = new Map();
|
||||
const module = load('app/native/nightscout-bridge.ts', {
|
||||
'../ui/dashboard-settings': { nightscoutSiteUrlSetting: { get: () => 'https://example.test' }, nightscoutApiTokenSetting: { get: () => 'test' } },
|
||||
'../util/http': { async fetchWithUserAgent() {
|
||||
'../util/http': { async fetchTextWithUserAgent() {
|
||||
calls++;
|
||||
await pending;
|
||||
return { ok: true, json: async () => [] };
|
||||
|
||||
Reference in new issue
Block a user