From a3a3db53635523db5359b49bd7b63ec1282218bf Mon Sep 17 00:00:00 2001 From: Michel Weststrate Date: Wed, 22 Jan 2020 07:08:47 -0800 Subject: [PATCH] Improve stats collection for plugins in the background Summary: This diff improves two things: 1. Stats are now gathered on every `trackUsage` tick, rather than only when there is a selection 2. The stats now include a delta to compare it with the previous tick Reviewed By: passy Differential Revision: D19514231 fbshipit-source-id: 1854c1dc03c63a03db8c7040c185d2629e1b9ea2 --- src/dispatcher/tracking.tsx | 9 ++++++-- src/utils/messageQueue.tsx | 44 ++++++++++++++++++++++++++++++------- 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/src/dispatcher/tracking.tsx b/src/dispatcher/tracking.tsx index 1798b3947..0e5d5e167 100644 --- a/src/dispatcher/tracking.tsx +++ b/src/dispatcher/tracking.tsx @@ -13,7 +13,10 @@ import {performance} from 'perf_hooks'; import {Store} from '../reducers/index'; import {Logger} from '../fb-interfaces/Logger'; import Client from '../Client'; -import {getPluginBackgroundStats} from '../utils/messageQueue'; +import { + getPluginBackgroundStats, + resetPluginBackgroundStatsDelta, +} from '../utils/messageQueue'; import { clearTimeline, TrackingEvent, @@ -90,6 +93,9 @@ export default (store: Store, logger: Logger) => { }), ); + logger.track('usage', 'plugin-stats', getPluginBackgroundStats()); + resetPluginBackgroundStatsDelta(); + if ( !state.application.windowIsFocused || !selectedDevice || @@ -115,7 +121,6 @@ export default (store: Store, logger: Logger) => { os: selectedDevice.os, device: selectedDevice.title, plugin: selectedPlugin, - pluginStats: getPluginBackgroundStats(), app, sdkVersion, isForeground: state.application.windowIsFocused, diff --git a/src/utils/messageQueue.tsx b/src/utils/messageQueue.tsx index 2b64a0a46..f58594a35 100644 --- a/src/utils/messageQueue.tsx +++ b/src/utils/messageQueue.tsx @@ -22,13 +22,22 @@ import {pluginIsStarred, getSelectedPluginKey} from '../reducers/connections'; const MAX_BACKGROUND_TASK_TIME = 25; type StatEntry = { - cpuTime: number; // Total time spend in persisted Reducer - messages: number; // amount of message received for this plugin + cpuTimeTotal: number; // Total time spend in persisted Reducer + cpuTimeDelta: number; // Time spend since previous tracking tick + messageCountTotal: number; // amount of message received for this plugin + messageCountDelta: number; // amout of messages received since previous tracking tick maxTime: number; // maximum time spend in a single reducer call }; const pluginBackgroundStats = new Map(); +export function resetPluginBackgroundStatsDelta() { + pluginBackgroundStats.forEach(stat => { + stat.cpuTimeDelta = 0; + stat.messageCountDelta = 0; + }); +} + export function getPluginBackgroundStats(): {[plugin: string]: StatEntry} { return Array.from(Object.entries(pluginBackgroundStats)).reduce( (aggregated, [pluginName, data]) => { @@ -44,10 +53,21 @@ if (window) { window.flipperPrintPluginBackgroundStats = () => { console.table( Array.from(pluginBackgroundStats.entries()).map( - ([plugin, {cpuTime, messages, maxTime}]) => ({ + ([ plugin, - cpuTime, - messages, + { + cpuTimeDelta, + cpuTimeTotal, + messageCountDelta, + messageCountTotal, + maxTime, + }, + ]) => ({ + plugin, + cpuTimeTotal, + messageCountTotal, + cpuTimeDelta, + messageCountDelta, maxTime, }), ), @@ -57,11 +77,19 @@ if (window) { function addBackgroundStat(plugin: string, cpuTime: number) { if (!pluginBackgroundStats.has(plugin)) { - pluginBackgroundStats.set(plugin, {cpuTime: 0, messages: 0, maxTime: 0}); + pluginBackgroundStats.set(plugin, { + cpuTimeDelta: 0, + cpuTimeTotal: 0, + messageCountDelta: 0, + messageCountTotal: 0, + maxTime: 0, + }); } const stat = pluginBackgroundStats.get(plugin)!; - stat.cpuTime += cpuTime; - stat.messages += 1; + stat.cpuTimeDelta += cpuTime; + stat.cpuTimeTotal += cpuTime; + stat.messageCountDelta += 1; + stat.messageCountTotal += 1; stat.maxTime = Math.max(stat.maxTime, cpuTime); if (cpuTime > MAX_BACKGROUND_TASK_TIME) { console.warn(