forked from Blendman974/faceclaw
Add bluetooth settings options to bandwidth benchmark
This commit is contained in:
1 parent
50cbc3a756
commit
5a97b218e6
4 files changed
+85
-7
No files matched your search
@@ -192,6 +192,9 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
private int benchmarkMessageSize;
|
private int benchmarkMessageSize;
|
||||||
private int benchmarkWindowSize;
|
private int benchmarkWindowSize;
|
||||||
private int benchmarkDurationMs;
|
private int benchmarkDurationMs;
|
||||||
|
private int benchmarkLinkMode;
|
||||||
|
private boolean benchmarkLinkPending;
|
||||||
|
private long benchmarkReadyAtMs;
|
||||||
private long benchmarkStartAtMs; // first benchmark write; 0 until then
|
private long benchmarkStartAtMs; // first benchmark write; 0 until then
|
||||||
private long benchmarkDeadlineAtMs; // start + duration; MAX_VALUE until first write
|
private long benchmarkDeadlineAtMs; // start + duration; MAX_VALUE until first write
|
||||||
private long benchmarkLastAckAtMs;
|
private long benchmarkLastAckAtMs;
|
||||||
@@ -620,6 +623,12 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
* traffic already queued ahead of the run doesn't count against it.
|
* traffic already queued ahead of the run doesn't count against it.
|
||||||
*/
|
*/
|
||||||
public boolean startBandwidthBenchmark(int messageSize, int windowSize, int durationMs) {
|
public boolean startBandwidthBenchmark(int messageSize, int windowSize, int durationMs) {
|
||||||
|
return startBandwidthBenchmarkWithLinkMode(messageSize, windowSize, durationMs, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 0: current link; 1: re-request HIGH; 2: request 2M; 3: both.
|
||||||
|
public boolean startBandwidthBenchmarkWithLinkMode(int messageSize, int windowSize,
|
||||||
|
int durationMs, int linkMode) {
|
||||||
synchronized (lock) {
|
synchronized (lock) {
|
||||||
if (benchmarkActive || !running || !sessionReady || !fixedLayoutCreated
|
if (benchmarkActive || !running || !sessionReady || !fixedLayoutCreated
|
||||||
|| shutdownRequested || chargingMode) {
|
|| shutdownRequested || chargingMode) {
|
||||||
@@ -633,6 +642,9 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
benchmarkMessageSize = payload.length;
|
benchmarkMessageSize = payload.length;
|
||||||
benchmarkWindowSize = Math.max(1, Math.min(windowSize, BENCHMARK_MAX_WINDOW));
|
benchmarkWindowSize = Math.max(1, Math.min(windowSize, BENCHMARK_MAX_WINDOW));
|
||||||
benchmarkDurationMs = Math.max(1_000, durationMs);
|
benchmarkDurationMs = Math.max(1_000, durationMs);
|
||||||
|
benchmarkLinkMode = linkMode & 3;
|
||||||
|
benchmarkLinkPending = true;
|
||||||
|
benchmarkReadyAtMs = Long.MAX_VALUE;
|
||||||
benchmarkStartAtMs = 0;
|
benchmarkStartAtMs = 0;
|
||||||
benchmarkDeadlineAtMs = Long.MAX_VALUE;
|
benchmarkDeadlineAtMs = Long.MAX_VALUE;
|
||||||
benchmarkLastAckAtMs = 0;
|
benchmarkLastAckAtMs = 0;
|
||||||
@@ -645,7 +657,8 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
benchmarkAborted = false;
|
benchmarkAborted = false;
|
||||||
benchmarkActive = true;
|
benchmarkActive = true;
|
||||||
logLine("bandwidth benchmark start: size=" + benchmarkMessageSize
|
logLine("bandwidth benchmark start: size=" + benchmarkMessageSize
|
||||||
+ "B window=" + benchmarkWindowSize + " duration=" + benchmarkDurationMs + "ms");
|
+ "B window=" + benchmarkWindowSize + " duration=" + benchmarkDurationMs
|
||||||
|
+ "ms linkMode=" + benchmarkLinkMode);
|
||||||
}
|
}
|
||||||
interruptibleSleep.interrupt();
|
interruptibleSleep.interrupt();
|
||||||
return true;
|
return true;
|
||||||
@@ -679,6 +692,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
status.put("state", state);
|
status.put("state", state);
|
||||||
status.put("messageSize", benchmarkMessageSize);
|
status.put("messageSize", benchmarkMessageSize);
|
||||||
status.put("windowSize", benchmarkWindowSize);
|
status.put("windowSize", benchmarkWindowSize);
|
||||||
|
status.put("linkMode", benchmarkLinkMode);
|
||||||
status.put("elapsedMs", elapsed);
|
status.put("elapsedMs", elapsed);
|
||||||
status.put("messagesSent", benchmarkMessagesSent);
|
status.put("messagesSent", benchmarkMessagesSent);
|
||||||
status.put("messagesAcked", benchmarkMessagesAcked);
|
status.put("messagesAcked", benchmarkMessagesAcked);
|
||||||
@@ -715,6 +729,9 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
finishBenchmarkLocked(true, "session no longer ready");
|
finishBenchmarkLocked(true, "session no longer ready");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (benchmarkLinkPending || now < benchmarkReadyAtMs) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (benchmarkAborted || now >= benchmarkDeadlineAtMs) {
|
if (benchmarkAborted || now >= benchmarkDeadlineAtMs) {
|
||||||
// The run is over: drop queued-but-unsent no-ops (sending them
|
// The run is over: drop queued-but-unsent no-ops (sending them
|
||||||
// would stretch the run past its deadline) and finish once the
|
// would stretch the run past its deadline) and finish once the
|
||||||
@@ -2175,6 +2192,33 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
|||||||
private long driveSession() {
|
private long driveSession() {
|
||||||
//Log.d(TAG, "driveSession called (pendingMessages.size=" + pendingMessages.size() + " inFlightMessages.size=" + inFlightMessages.size() + ")");
|
//Log.d(TAG, "driveSession called (pendingMessages.size=" + pendingMessages.size() + " inFlightMessages.size=" + inFlightMessages.size() + ")");
|
||||||
while (true) {
|
while (true) {
|
||||||
|
// Run on the sender thread, outside `lock`: Bluetooth API calls can
|
||||||
|
// wait on the global GATT lock, and callbacks need the session lock.
|
||||||
|
// Keep negotiation outside the timed stream. A fixed settling period
|
||||||
|
// is only an experiment boundary; HCI must confirm actual parameters.
|
||||||
|
int linkMode = -1;
|
||||||
|
String[] benchmarkAddresses = null;
|
||||||
|
synchronized (lock) {
|
||||||
|
if (benchmarkActive && benchmarkLinkPending) {
|
||||||
|
benchmarkLinkPending = false;
|
||||||
|
linkMode = benchmarkLinkMode;
|
||||||
|
benchmarkAddresses = new String[] {leftAddress, rightAddress};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (benchmarkAddresses != null) {
|
||||||
|
for (String address : benchmarkAddresses) {
|
||||||
|
try {
|
||||||
|
bleManager.prepareBenchmarkLink(address, linkMode);
|
||||||
|
} catch (RuntimeException error) {
|
||||||
|
logLine("benchmark link request failed: " + safeMessage(error));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
synchronized (lock) {
|
||||||
|
// Cancellation/new-run races leave the new run pending; its
|
||||||
|
// own preparation will replace this deadline before sending.
|
||||||
|
benchmarkReadyAtMs = SystemClock.elapsedRealtime() + 1_000;
|
||||||
|
}
|
||||||
|
}
|
||||||
OutboundMessage messageToWrite = null;
|
OutboundMessage messageToWrite = null;
|
||||||
OutboundMessage messageToPrewrite = null;
|
OutboundMessage messageToPrewrite = null;
|
||||||
long now = SystemClock.elapsedRealtime();
|
long now = SystemClock.elapsedRealtime();
|
||||||
|
|||||||
@@ -160,6 +160,23 @@ public class FaceclawBleManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** One-shot benchmark preferences; success is established by callbacks/HCI, not submission. */
|
||||||
|
public void prepareBenchmarkLink(String address, int mode) {
|
||||||
|
synchronized (gattLock(address)) {
|
||||||
|
BluetoothGatt gatt = requireGatt(address);
|
||||||
|
if ((mode & 1) != 0) {
|
||||||
|
boolean accepted = gatt.requestConnectionPriority(BluetoothGatt.CONNECTION_PRIORITY_HIGH);
|
||||||
|
Log.i(TAG, "benchmark HIGH address=" + address + " submitted=" + accepted);
|
||||||
|
}
|
||||||
|
if ((mode & 2) != 0) {
|
||||||
|
Log.i(TAG, "benchmark request 2M address=" + address);
|
||||||
|
gatt.setPreferredPhy(BluetoothDevice.PHY_LE_2M_MASK,
|
||||||
|
BluetoothDevice.PHY_LE_2M_MASK, BluetoothDevice.PHY_OPTION_NO_PREFERRED);
|
||||||
|
}
|
||||||
|
gatt.readPhy();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public boolean requestMtu(String address, int mtu, int timeoutMs) {
|
public boolean requestMtu(String address, int mtu, int timeoutMs) {
|
||||||
synchronized (gattLock(address)) {
|
synchronized (gattLock(address)) {
|
||||||
CountDownLatch latch = new CountDownLatch(1);
|
CountDownLatch latch = new CountDownLatch(1);
|
||||||
@@ -451,7 +468,14 @@ public class FaceclawBleManager {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPhyRead(BluetoothGatt gatt, int txPhy, int rxPhy, int status) {
|
public void onPhyRead(BluetoothGatt gatt, int txPhy, int rxPhy, int status) {
|
||||||
Log.i(TAG, "onPhyRead: txPhy=" + txPhy + " rxPhy=" + rxPhy + " status=" + status);
|
Log.i(TAG, "onPhyRead: address=" + gatt.getDevice().getAddress()
|
||||||
|
+ " txPhy=" + txPhy + " rxPhy=" + rxPhy + " status=" + status);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPhyUpdate(BluetoothGatt gatt, int txPhy, int rxPhy, int status) {
|
||||||
|
Log.i(TAG, "onPhyUpdate: address=" + gatt.getDevice().getAddress()
|
||||||
|
+ " txPhy=" + txPhy + " rxPhy=" + rxPhy + " status=" + status);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import {
|
|||||||
|
|
||||||
const MESSAGE_SIZES = [250, 500, 1000, 2000, 3800];
|
const MESSAGE_SIZES = [250, 500, 1000, 2000, 3800];
|
||||||
const WINDOW_SIZES = [1, 2, 3, 4, 6];
|
const WINDOW_SIZES = [1, 2, 3, 4, 6];
|
||||||
|
const LINK_MODES = ["Current", "Request HIGH", "Request 2M", "HIGH + 2M"];
|
||||||
const DURATION_MS = 15_000;
|
const DURATION_MS = 15_000;
|
||||||
// After painting the "running" screen, wait for that frame to reach the
|
// After painting the "running" screen, wait for that frame to reach the
|
||||||
// glasses before the benchmark blocks normal image sends — otherwise the
|
// glasses before the benchmark blocks normal image sends — otherwise the
|
||||||
@@ -37,9 +38,10 @@ type Phase = "config" | "arming" | "running" | "done";
|
|||||||
*/
|
*/
|
||||||
export class BandwidthBenchmarkLayer implements Layer {
|
export class BandwidthBenchmarkLayer implements Layer {
|
||||||
private phase: Phase = "config";
|
private phase: Phase = "config";
|
||||||
private selectedRow = 0; // 0 = message size, 1 = window size, 2 = start
|
private selectedRow = 0; // size, window, link preference, start
|
||||||
private sizeIndex = 2;
|
private sizeIndex = 2;
|
||||||
private windowIndex = 2;
|
private windowIndex = 2;
|
||||||
|
private linkModeIndex = 0;
|
||||||
private result: BandwidthBenchmarkStatus | null = null;
|
private result: BandwidthBenchmarkStatus | null = null;
|
||||||
private error: string | null = null;
|
private error: string | null = null;
|
||||||
private armTimer: ReturnType<typeof setTimeout> | null = null;
|
private armTimer: ReturnType<typeof setTimeout> | null = null;
|
||||||
@@ -77,6 +79,7 @@ export class BandwidthBenchmarkLayer implements Layer {
|
|||||||
MESSAGE_SIZES[this.sizeIndex]!,
|
MESSAGE_SIZES[this.sizeIndex]!,
|
||||||
WINDOW_SIZES[this.windowIndex]!,
|
WINDOW_SIZES[this.windowIndex]!,
|
||||||
DURATION_MS,
|
DURATION_MS,
|
||||||
|
this.linkModeIndex,
|
||||||
);
|
);
|
||||||
if (!started) {
|
if (!started) {
|
||||||
this.phase = "config";
|
this.phase = "config";
|
||||||
@@ -123,7 +126,7 @@ export class BandwidthBenchmarkLayer implements Layer {
|
|||||||
font,
|
font,
|
||||||
LIST_X,
|
LIST_X,
|
||||||
HEADER_HEIGHT + 30,
|
HEADER_HEIGHT + 30,
|
||||||
`${MESSAGE_SIZES[this.sizeIndex]} B messages, window ${WINDOW_SIZES[this.windowIndex]}`,
|
`${MESSAGE_SIZES[this.sizeIndex]} B, window ${WINDOW_SIZES[this.windowIndex]}, ${LINK_MODES[this.linkModeIndex]}`,
|
||||||
150,
|
150,
|
||||||
);
|
);
|
||||||
image.drawText(font, LIST_X, HEADER_HEIGHT + 52, "Display updates pause until the run finishes.", 110);
|
image.drawText(font, LIST_X, HEADER_HEIGHT + 52, "Display updates pause until the run finishes.", 110);
|
||||||
@@ -134,6 +137,7 @@ export class BandwidthBenchmarkLayer implements Layer {
|
|||||||
const rows: Array<[string, string]> = [
|
const rows: Array<[string, string]> = [
|
||||||
["Message size", `${MESSAGE_SIZES[this.sizeIndex]} B`],
|
["Message size", `${MESSAGE_SIZES[this.sizeIndex]} B`],
|
||||||
["Window size", `${WINDOW_SIZES[this.windowIndex]}`],
|
["Window size", `${WINDOW_SIZES[this.windowIndex]}`],
|
||||||
|
["Link request", LINK_MODES[this.linkModeIndex]!],
|
||||||
[this.phase === "done" ? "Run again" : "Start", ""],
|
[this.phase === "done" ? "Run again" : "Start", ""],
|
||||||
];
|
];
|
||||||
for (let index = 0; index < rows.length; index++) {
|
for (let index = 0; index < rows.length; index++) {
|
||||||
@@ -161,6 +165,7 @@ export class BandwidthBenchmarkLayer implements Layer {
|
|||||||
`Throughput: ${(payloadRate / 1024).toFixed(2)} KB/s payload (${(wireRate / 1024).toFixed(2)} KB/s wire)`,
|
`Throughput: ${(payloadRate / 1024).toFixed(2)} KB/s payload (${(wireRate / 1024).toFixed(2)} KB/s wire)`,
|
||||||
`Messages: ${r.messagesAcked}/${r.messagesSent} acked, ${r.timeouts} timeouts`,
|
`Messages: ${r.messagesAcked}/${r.messagesSent} acked, ${r.timeouts} timeouts`,
|
||||||
`Elapsed: ${seconds.toFixed(1)} s` + (r.aborted ? " (aborted early)" : ""),
|
`Elapsed: ${seconds.toFixed(1)} s` + (r.aborted ? " (aborted early)" : ""),
|
||||||
|
`Link request: ${LINK_MODES[r.linkMode] ?? "Current"}`,
|
||||||
];
|
];
|
||||||
for (const line of lines) {
|
for (const line of lines) {
|
||||||
image.drawText(font, LIST_X, y, line, 190);
|
image.drawText(font, LIST_X, y, line, 190);
|
||||||
@@ -184,13 +189,15 @@ export class BandwidthBenchmarkLayer implements Layer {
|
|||||||
this.selectedRow = Math.max(0, this.selectedRow - 1);
|
this.selectedRow = Math.max(0, this.selectedRow - 1);
|
||||||
return;
|
return;
|
||||||
case "scroll-down":
|
case "scroll-down":
|
||||||
this.selectedRow = Math.min(2, this.selectedRow + 1);
|
this.selectedRow = Math.min(3, this.selectedRow + 1);
|
||||||
return;
|
return;
|
||||||
case "click":
|
case "click":
|
||||||
if (this.selectedRow === 0) {
|
if (this.selectedRow === 0) {
|
||||||
this.sizeIndex = (this.sizeIndex + 1) % MESSAGE_SIZES.length;
|
this.sizeIndex = (this.sizeIndex + 1) % MESSAGE_SIZES.length;
|
||||||
} else if (this.selectedRow === 1) {
|
} else if (this.selectedRow === 1) {
|
||||||
this.windowIndex = (this.windowIndex + 1) % WINDOW_SIZES.length;
|
this.windowIndex = (this.windowIndex + 1) % WINDOW_SIZES.length;
|
||||||
|
} else if (this.selectedRow === 2) {
|
||||||
|
this.linkModeIndex = (this.linkModeIndex + 1) % LINK_MODES.length;
|
||||||
} else {
|
} else {
|
||||||
this.startRun();
|
this.startRun();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ export type BandwidthBenchmarkStatus = {
|
|||||||
state: "idle" | "starting" | "running" | "done";
|
state: "idle" | "starting" | "running" | "done";
|
||||||
messageSize: number;
|
messageSize: number;
|
||||||
windowSize: number;
|
windowSize: number;
|
||||||
|
linkMode: number;
|
||||||
elapsedMs: number;
|
elapsedMs: number;
|
||||||
messagesSent: number;
|
messagesSent: number;
|
||||||
messagesAcked: number;
|
messagesAcked: number;
|
||||||
@@ -33,12 +34,14 @@ function activeCommunicator(): any {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** Start a run. False if not connected or a run is already active. */
|
/** Start a run. False if not connected or a run is already active. */
|
||||||
export function startBandwidthBenchmark(messageSize: number, windowSize: number, durationMs: number): boolean {
|
export function startBandwidthBenchmark(messageSize: number, windowSize: number, durationMs: number, linkMode = 0): boolean {
|
||||||
const active = activeCommunicator();
|
const active = activeCommunicator();
|
||||||
if (!active) return false;
|
if (!active) return false;
|
||||||
try {
|
try {
|
||||||
return Boolean(
|
return Boolean(
|
||||||
active.startBandwidthBenchmark(Math.round(messageSize), Math.round(windowSize), Math.round(durationMs)),
|
active.startBandwidthBenchmarkWithLinkMode(
|
||||||
|
Math.round(messageSize), Math.round(windowSize), Math.round(durationMs), Math.round(linkMode),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.warn(`startBandwidthBenchmark failed: ${error}`);
|
console.warn(`startBandwidthBenchmark failed: ${error}`);
|
||||||
|
|||||||
Reference in new issue
Block a user