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
This commit is contained in:
Michel Weststrate
2020-01-22 07:08:47 -08:00
committed by Facebook Github Bot
parent c8b9dd949b
commit a3a3db5363
2 changed files with 43 additions and 10 deletions

View File

@@ -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,

View File

@@ -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<string, StatEntry>();
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(