From 9663e229a6f4a0126552afb5486cb61d1b7fd8f4 Mon Sep 17 00:00:00 2001 From: megumin Date: Sat, 5 Nov 2022 10:09:05 +0000 Subject: [PATCH] feat(plugins): add Startup Timings (#189) --- .../startupTimings/StartupTimingPage.tsx | 152 ++++++++++++++++++ src/plugins/startupTimings/index.tsx | 36 +++++ 2 files changed, 188 insertions(+) create mode 100644 src/plugins/startupTimings/StartupTimingPage.tsx create mode 100644 src/plugins/startupTimings/index.tsx diff --git a/src/plugins/startupTimings/StartupTimingPage.tsx b/src/plugins/startupTimings/StartupTimingPage.tsx new file mode 100644 index 0000000..f864138 --- /dev/null +++ b/src/plugins/startupTimings/StartupTimingPage.tsx @@ -0,0 +1,152 @@ +/* + * Vencord, a modification for Discord's desktop app + * Copyright (c) 2022 Vendicated and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . +*/ + +import ErrorBoundary from "../../components/ErrorBoundary"; +import { Flex } from "../../components/Flex"; +import { lazyWebpack } from "../../utils"; +import { filters } from "../../webpack"; +import { Forms, React } from "../../webpack/common"; + +interface AppStartPerformance { + prefix: string; + logs: Log[]; + logGroups: LogGroup[]; + endTime_: number; + isTracing_: boolean; +} + +interface LogGroup { + index: number; + timestamp: number; + logs: Log[]; + nativeLogs: any[]; + serverTrace: string; +} + +interface Log { + emoji: string; + prefix: string; + log: string; + timestamp?: number; + delta?: number; +} + +const AppStartPerformance = lazyWebpack(filters.byProps("markWithDelta", "markAndLog", "markAt")) as AppStartPerformance; + +interface TimerItemProps extends Log { + instance: { + sinceStart: number; + sinceLast: number; + }; +} + +function TimerItem({ emoji, prefix, log, delta, instance }: TimerItemProps) { + return ( + + {instance.sinceStart.toFixed(3)}s + {instance.sinceLast.toFixed(3)}s + {delta?.toFixed(0) ?? ""} +
{emoji} {prefix ?? " "}{log}
+
+ ); +} + +interface TimingSectionProps { + title: string; + logs: Log[]; + traceEnd?: number; +} + +function TimingSection({ title, logs, traceEnd }: TimingSectionProps) { + const startTime = logs.find(l => l.timestamp)?.timestamp ?? 0; + + let lastTimestamp = startTime; + const timings = logs.map(log => { + // Get last log entry with valid timestamp + const timestamp = log.timestamp ?? lastTimestamp; + + const sinceStart = (timestamp - startTime) / 1000; + const sinceLast = (timestamp - lastTimestamp) / 1000; + + lastTimestamp = timestamp; + + return { sinceStart, sinceLast }; + }); + + return ( + + + {traceEnd && ( +
+ Trace ended at: {(new Date(traceEnd)).toTimeString()} +
+ )} +
+ Start + Interval + Delta + Event + {AppStartPerformance.logs.map((log, i) => ( + + ))} +
+
+
+ ); +} + +interface ServerTraceProps { + trace: string; +} + +function ServerTrace({ trace }: ServerTraceProps) { + const lines = trace.split("\n"); + + return ( + + + + {lines.map(line => ( + {line} + ))} + + + + ); +} + +function StartupTimingPage() { + if (!AppStartPerformance?.logs) return
Loading...
; + + const serverTrace = AppStartPerformance.logGroups.find(g => g.serverTrace)?.serverTrace; + + return ( + + + {/* Lazy Divider */} +
 
+ {serverTrace && } +
+ ); +} + +export default ErrorBoundary.wrap(StartupTimingPage); diff --git a/src/plugins/startupTimings/index.tsx b/src/plugins/startupTimings/index.tsx new file mode 100644 index 0000000..3f121f7 --- /dev/null +++ b/src/plugins/startupTimings/index.tsx @@ -0,0 +1,36 @@ +/* + * Vencord, a modification for Discord's desktop app + * Copyright (c) 2022 Vendicated and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . +*/ + + +import { LazyComponent } from "../../utils"; +import { Devs } from "../../utils/constants"; +import definePlugin from "../../utils/types"; + +export default definePlugin({ + name: "StartupTimings", + description: "Adds Startup Timings to the Settings menu", + authors: [Devs.Megu], + patches: [{ + find: "PAYMENT_FLOW_MODAL_TEST_PAGE,", + replacement: { + match: /({section:[\w.]+?\.PAYMENT_FLOW_MODAL_TEST_PAGE,)/, + replace: '{section:"StartupTimings",label:"Startup Timings",element:Vencord.Plugins.plugins.StartupTimings.StartupTimingPage},$1' + } + }], + StartupTimingPage: LazyComponent(() => require("./StartupTimingPage").default) +});