From b98edc669a7811cc10656271cf5bf6526e87022c Mon Sep 17 00:00:00 2001 From: Lorenzo Blasa Date: Mon, 26 Jun 2023 12:12:51 -0700 Subject: [PATCH] Dynamic performance stats Summary: PerformanceStatsEvent declares the metrics we track for performance. DynamicPerformanceStatsEvent is a new type which extends it by allowing arbitraty key/value pairs to be reported and visualised. Reviewed By: antonk52 Differential Revision: D47023248 fbshipit-source-id: fadfad79561fca9ae48d0668da3cc62f0d0391d8 --- .../ui-debugger/components/PerfStats.tsx | 42 ++++++++++++++++--- desktop/plugins/public/ui-debugger/types.tsx | 4 ++ 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/desktop/plugins/public/ui-debugger/components/PerfStats.tsx b/desktop/plugins/public/ui-debugger/components/PerfStats.tsx index 41ebd80bf..9f14a03b9 100644 --- a/desktop/plugins/public/ui-debugger/components/PerfStats.tsx +++ b/desktop/plugins/public/ui-debugger/components/PerfStats.tsx @@ -7,17 +7,41 @@ * @format */ -import {PerformanceStatsEvent} from '../types'; -import React from 'react'; +import {PerformanceStatsEvent, DynamicPerformanceStatsEvent} from '../types'; +import React, {useMemo} from 'react'; import {DataSource, DataTable, DataTableColumn} from 'flipper-plugin'; export function PerfStats(props: { - events: DataSource; + events: DataSource; }) { + const allColumns = useMemo(() => { + if (props.events.size > 0) { + const row = props.events.get(0); + + const unknownKeys = Object.keys(row).filter( + (property) => !knownKeys.has(property), + ); + + const unknownColumns = unknownKeys.map((unknwonKey) => ({ + key: unknwonKey, + title: formatKey(unknwonKey), + onRender: (row: DynamicPerformanceStatsEvent) => { + if (unknwonKey.endsWith('MS')) { + return formatDiff(row[unknwonKey]); + } + return row[unknwonKey]; + }, + })); + + return columns.concat(...unknownColumns); + } + return columns; + }, [props.events]); + return ( dataSource={props.events} - columns={columns} + columns={allColumns} /> ); } @@ -30,12 +54,17 @@ function formatSize(bytes: number): string { return `${(bytes / 1000).toFixed()}`; } -const columns: DataTableColumn[] = [ +function formatKey(key: string): string { + const pascalCase = key.replace(/([a-z])([A-Z])/g, '$1 $2'); + return pascalCase.charAt(0).toUpperCase() + pascalCase.slice(1); +} + +const columns: DataTableColumn[] = [ { key: 'txId', title: 'TXID', onRender: (row: PerformanceStatsEvent) => { - return row.txId; + return row.txId.toFixed(0); }, }, { @@ -106,3 +135,4 @@ const columns: DataTableColumn[] = [ }, }, ]; +const knownKeys = new Set(columns.map((column) => column.key)); diff --git a/desktop/plugins/public/ui-debugger/types.tsx b/desktop/plugins/public/ui-debugger/types.tsx index 1e2dcf09a..167245a84 100644 --- a/desktop/plugins/public/ui-debugger/types.tsx +++ b/desktop/plugins/public/ui-debugger/types.tsx @@ -113,6 +113,10 @@ export type PerformanceStatsEvent = { payloadSize?: number; }; +export type DynamicPerformanceStatsEvent = PerformanceStatsEvent & { + [key: string]: any; +}; + export type UpdateMetadataEvent = { attributeMetadata: Record; };