Skip to content

A high-performance Android storage analyzer that bypasses MTP using a native C++ daemon, a Rust TCP bridge, and a React Treemap UI.

Rust

Record device discovery timing and readiness in Diagnostics (#144)

Vishnu Srivatsavae3c4e8a201ae
diff --git a/src/useDevices.ts b/src/useDevices.ts
index 128e07a..e710b16 100644
--- a/src/useDevices.ts
+++ b/src/useDevices.ts
@@ -4,15 +4,18 @@ import { deviceDiscoveryMessage, parseDeviceList, preserveDeviceSelection } from
import { boundDiagnostic } from "./logHistory";
import type { AndroidDevice } from "./devices";
-export function useDevices(enabled: boolean) {
+export function useDevices(enabled: boolean, addLog: (message: string) => void) {
const [devices, setDevices] = useState<AndroidDevice[]>([]);
const [serial, select] = useState("");
const [loading, setLoading] = useState(true);
const [error, setError] = useState<{ message: string; details: string } | null>(null);
const request = useRef(0);
const pending = useRef<Promise<AndroidDevice[]> | null>(null);
- const refresh = useCallback(async () => {
+ const completed = useRef<{ id: number; started: number; finished: number; label: string } | null>(null);
+ const discover = useCallback(async (source: "setup" | "refresh") => {
const id = ++request.current;
+ const started = performance.now();
+ let outcome = "failed";
setLoading(true);
setError(null);
try {
@@ -22,6 +25,7 @@ export function useDevices(enabled: boolean) {
.then(parseDeviceList)
.finally(() => { pending.current = null; }));
if (id !== request.current) return;
+ outcome = `${result.filter(device => device.state === "device").length} ready of ${result.length}`;
setDevices(result);
select(previous => preserveDeviceSelection(previous, result));
} catch (error) {
@@ -30,14 +34,26 @@ export function useDevices(enabled: boolean) {
const details = String(error);
setError({ message: deviceDiscoveryMessage(details), details: boundDiagnostic(details) });
} finally {
- if (id === request.current) setLoading(false);
+ if (id === request.current) {
+ completed.current = { id, started, finished: performance.now(), label: `${source}, ${outcome}` };
+ setLoading(false);
+ }
}
}, []);
+ useEffect(() => {
+ const timing = completed.current;
+ if (!enabled || loading || !timing || timing.id !== request.current) return;
+ completed.current = null;
+ const requestMs = Math.round(timing.finished - timing.started);
+ const pickerMs = Math.round(performance.now() - timing.finished);
+ addLog(`[ADB] Discovery (${timing.label}): response ${requestMs} ms, picker effect ${pickerMs} ms.`);
+ }, [enabled, loading, devices, addLog]);
useEffect(() => {
if (!enabled) return;
- void refresh();
+ void discover("setup");
return () => { request.current += 1; };
- }, [enabled, refresh]);
+ }, [enabled, discover]);
+ const refresh = useCallback(() => discover("refresh"), [discover]);
return {
devices, serial, select, loading, error, refresh,
ready: !loading && error === null && devices.some(device => device.serial === serial && device.state === "device"),
diff --git a/src/useScanSession.ts b/src/useScanSession.ts
index 5c5d747..684902e 100644
--- a/src/useScanSession.ts
+++ b/src/useScanSession.ts
@@ -21,7 +21,7 @@ type AppPhase = "setup" | "connecting" | "scanning" | "result";
export function useScanSession(pushToast: PushToast, addLog: (message: string) => void) {
const [phase, setPhase] = useState<AppPhase>("setup");
- const deviceSelection = useDevices(phase === "setup");
+ const deviceSelection = useDevices(phase === "setup", addLog);
const { serial: selectedSerial, ready: deviceReady } = deviceSelection;
const [rootMode, setRootMode] = useState(false);
const [rootPath, setRootPath] = useState("/data");
diff --git a/tests/device_discovery_fixture.jsx b/tests/device_discovery_fixture.jsx
index 8a89ce3..c4009f0 100644
--- a/tests/device_discovery_fixture.jsx
+++ b/tests/device_discovery_fixture.jsx
@@ -7,6 +7,8 @@ let model;
let effects = 0;
let calls = 0;
const pending = [];
+const diagnostics = [];
+const addLog = message => diagnostics.push(message);
const check = (condition, message) => { if (!condition) throw Error(message); };
const pause = ms => new Promise(resolve => setTimeout(resolve, ms));
const waitFor = async (predicate, message) => {
@@ -21,7 +23,7 @@ window.__TAURI_INTERNALS__ = {
},
};
function Probe() {
- model = useDevices(true);
+ model = useDevices(true, addLog);
useEffect(() => { effects++; }, []);
return <p>{model.loading ? "Checking" : "Finished"}</p>;
}
@@ -40,26 +42,30 @@ void (async () => {
check(calls === 1, "Repeated refresh launched another pending command");
pending.shift().resolve([{ serial: "chosen", state: "device", model: null }]);
await Promise.all([a, b]);
- await waitFor(() => model.ready, "Latest subscriber did not receive discovery");
+ await waitFor(() => model.ready && diagnostics.length === 1, "Latest subscriber did not report discovery");
+ check(diagnostics[0].includes("refresh, 1 ready of 1") && !diagnostics[0].includes("chosen"),
+ "Discovery timing lacked readiness or exposed the serial");
const failed = model.refresh();
check(calls === 2, "Settled discovery was reused as a cached result");
pending.shift().reject("ADB server didn't ACK\n<bad diagnostic>");
await failed;
- await waitFor(() => model.error && !model.loading, "Discovery failure did not finish");
+ await waitFor(() => model.error && !model.loading && diagnostics.length === 2, "Discovery failure did not finish");
+ check(diagnostics[1].includes("refresh, failed") && !diagnostics[1].includes("bad diagnostic"), "Failed discovery leaked details");
check(model.serial === "chosen" && !model.ready, "Failure replaced selection or granted readiness");
const recovered = model.refresh();
check(calls === 3, "Failed discovery prevented a fresh retry");
pending.shift().resolve([{ serial: "chosen", state: "device", model: null }]);
await recovered;
- await waitFor(() => model.ready && !model.error, "Discovery retry did not recover");
+ await waitFor(() => model.ready && !model.error && diagnostics.length === 3, "Discovery retry did not recover");
const abandoned = model.refresh();
check(calls === 4, "Final discovery missing");
root.unmount();
pending.shift().reject("late failure");
await abandoned;
+ check(diagnostics.length === 3, "Abandoned discovery logged a result");
check(!document.querySelector("#root").textContent, "Unmounted discovery changed the UI");
await report(true);
})().catch(error => report(false, error.stack ?? String(error)));
diff --git a/tests/scan_ui_fixture.js b/tests/scan_ui_fixture.js
index 45a13ff..0af635d 100644
--- a/tests/scan_ui_fixture.js
+++ b/tests/scan_ui_fixture.js
@@ -317,6 +317,8 @@
listedDevices = [...readyDevices].reverse();
await refreshDevices();
check(picker().value === "phone-two" && !connect.disabled, "Reordered/reconnected devices lost selection");
+ await waitFor(() => /Discovery \(refresh, 2 ready of 4\): response \d+ ms, picker effect \d+ ms/.test(document.querySelector("#terminal-log").textContent),
+ "Discovery diagnostics did not include timings and readiness");
// The display is only a snapshot. Native startup must reject a selected
// device that vanished after this refresh, without choosing the first one.
listedDevices = [readyDevices[0]];