diff --git a/packages/desktop/scripts/bench-startup.ts b/packages/desktop/scripts/bench-startup.ts index 9c8ee733b87..c7ec74dded9 100644 --- a/packages/desktop/scripts/bench-startup.ts +++ b/packages/desktop/scripts/bench-startup.ts @@ -175,8 +175,12 @@ const summaries = Object.fromEntries( const phaseOrder = [ ["electron native init", "processCreated", "nodeStart"], ["node bootstrap", "nodeStart", "nodeBootstrapped"], - ["electron js init + main bundle", "nodeBootstrapped", "appStarting"], - ["main layers → window created", "appStarting", "rendererProcess"], + ["electron js init → entry", "nodeBootstrapped", "entryStart"], + ["entry → chromium ready", "entryStart", "electronReady"], + ["ready → window shown", "electronReady", "windowVisible"], + ["main bundle load + evaluate", "windowVisible", "bundleEvaluated"], + ["layers → first log line", "bundleEvaluated", "appStarting"], + ["layers → renderer process", "appStarting", "rendererProcess"], ["renderer boot → first paint", "rendererProcess", "firstPaint"], ["first paint → shell", "firstPaint", "shellVisible"], ["shell → idle", "shellVisible", "rendererIdle"], @@ -368,6 +372,9 @@ async function launch(build: { label: string; exe: string }, run: number): Promi processCreated: boot && Math.round(boot.created - spawnAt), nodeStart: boot && Math.round(boot.origin + boot.nodeStart - spawnAt), nodeBootstrapped: boot && Math.round(boot.origin + boot.bootstrapComplete - spawnAt), + entryStart: main.marks.entry && main.marks.entry - spawnAt, + electronReady: main.marks.ready && main.marks.ready - spawnAt, + bundleEvaluated: main.marks.bundle && main.marks.bundle - spawnAt, appStarting: main.appStarting && main.appStarting - spawnAt, cliVersionStart: main.versionStart && main.versionStart - spawnAt, cliVersionDone: main.versionDone && main.versionDone - spawnAt, @@ -525,6 +532,8 @@ function mainLog() { const dir = dirs.map((d) => join(paths.logs, d)).find((d) => existsSync(join(d, "main.log"))) const timeline: [number, string, string][] = [] let windowShownAt: number | undefined + // Epoch marks the entry module recorded before any logger existed, reported with "app starting". + const marks: Record = {} for (const name of dir ? readdirSync(dir).filter((f) => f.endsWith(".log")) : []) { const text = readFileSync(join(dir!, name), "utf8") // electron-log wraps long objects onto continuation lines; read them as part of the entry. @@ -536,6 +545,8 @@ function mainLog() { // A window shown before the logger existed reports when it was shown; the line itself is later. const shown = /main window visible/.test(message) ? entry.match(/shownAt: (\d+)/)?.[1] : undefined if (shown) windowShownAt = Number(shown) + if (/app starting/.test(message)) + for (const [, key, value] of entry.matchAll(/\b(entry|ready|window|bundle): (\d{10,})/g)) marks[key] = Number(value) timeline.push([new Date(m[1].replace(" ", "T")).getTime(), name.replace(/\.log$/, ""), message]) } } @@ -548,7 +559,8 @@ function mainLog() { versionDone: at(/v2 CLI command completed/), serviceStarting: at(/v2 CLI background service starting/), serviceReady: at(/background service ready/), - windowVisible: windowShownAt ?? at(/main window visible/), + windowVisible: windowShownAt ?? marks.window ?? at(/main window visible/), + marks, } } diff --git a/packages/desktop/src/main/desktop.ts b/packages/desktop/src/main/desktop.ts index 4f94c34ec86..33c88d23d50 100644 --- a/packages/desktop/src/main/desktop.ts +++ b/packages/desktop/src/main/desktop.ts @@ -8,6 +8,10 @@ import { ApplicationLifecycle } from "./lifecycle" import { BackgroundService } from "./service/background-service" import { DesktopCli } from "./service/desktop-cli" import { UpdaterLive } from "./updater/live" +import { marks } from "./lifecycle/marks" + +// Everything above has been loaded and evaluated by now; the layers start below. +marks.bundle = Date.now() const runIpc = Effect.fn("Desktop.runIpc")(function* () { const lifecycle = yield* ApplicationLifecycle.Service diff --git a/packages/desktop/src/main/index.ts b/packages/desktop/src/main/index.ts index 8e24a3f5e2d..b3d2cb1b107 100644 --- a/packages/desktop/src/main/index.ts +++ b/packages/desktop/src/main/index.ts @@ -1,3 +1,5 @@ +// Imported first so its evaluation stamps the moment Electron handed control to this module. +import { marks } from "./lifecycle/marks" import { app } from "electron" import { acquireApplicationLock, configureApplication } from "./lifecycle/configure" import { createEarlyWindow } from "./windows/early" @@ -13,7 +15,9 @@ if (acquireApplicationLock()) { // Window first, then the bundle: starting the import before ready delays ready itself, because the // module graph evaluates on the same thread Chromium needs to finish initialising. void app.whenReady().then(() => { + marks.ready = Date.now() createEarlyWindow() + marks.window = Date.now() return import("./desktop") }) } diff --git a/packages/desktop/src/main/lifecycle/marks.ts b/packages/desktop/src/main/lifecycle/marks.ts new file mode 100644 index 00000000000..72b767bc78e --- /dev/null +++ b/packages/desktop/src/main/lifecycle/marks.ts @@ -0,0 +1,4 @@ +// Startup marks, epoch ms. The entry module records them before any logger exists; the logging +// layer reports them with "app starting" so the startup benchmark can split the time before the +// first log line into Electron's own initialisation, our entry, and the main bundle. +export const marks: { entry: number; ready?: number; window?: number; bundle?: number } = { entry: Date.now() } diff --git a/packages/desktop/src/main/native/logging.ts b/packages/desktop/src/main/native/logging.ts index 845b0b67f04..24d8b03b815 100644 --- a/packages/desktop/src/main/native/logging.ts +++ b/packages/desktop/src/main/native/logging.ts @@ -5,6 +5,7 @@ import { app, crashReporter, netLog, shell } from "electron" import { Context, Effect, FileSystem, Layer, Logger, Option, Path, References, Stream } from "effect" import { homedir } from "node:os" import { VERSION } from "../constants" +import { marks } from "../lifecycle/marks" const MAX_LOG_AGE_DAYS = 7 const TAIL_LINES = 1000 @@ -34,6 +35,7 @@ const serviceLayer = Layer.effect( version: VERSION, packaged: app.isPackaged, onboardingTest: process.env.OPENCODE_TEST_ONBOARDING === "1", + marks, }) const exportDebug = exportDebugLogsEffect(fs, path).pipe(Effect.orDie) return Service.of({