use perf_hooks

Summary:
We were using `window.performance` to measure performance. This was because the equivalent node.js API `perf_hooks` wasn't available in the electron version we were using back then.
However, `perf_hooks` has landed in electron meanwhile, so I am moving to this API, as it works for the headless version and jest tests, too.

This allows us to delete the babel transform that was used for node-based tests, where the browser API was replaced with the node API

Reviewed By: jknoxville

Differential Revision: D13860133

fbshipit-source-id: cf1395004fac046dd55751ff465af494173b2cbf
This commit is contained in:
Daniel Büchele
2019-01-29 09:26:21 -08:00
committed by Facebook Github Bot
parent 26266bc607
commit 500007ccca
7 changed files with 8 additions and 49 deletions

View File

@@ -14,6 +14,8 @@ import type {OS} from './devices/BaseDevice.js';
import {FlipperDevicePlugin} from './plugin.js'; import {FlipperDevicePlugin} from './plugin.js';
import {setPluginState} from './reducers/pluginStates.js'; import {setPluginState} from './reducers/pluginStates.js';
import {ReactiveSocket, PartialResponder} from 'rsocket-core'; import {ReactiveSocket, PartialResponder} from 'rsocket-core';
// $FlowFixMe perf_hooks is a new API in node
import {performance} from 'perf_hooks';
const EventEmitter = (require('events'): any); const EventEmitter = (require('events'): any);
const invariant = require('invariant'); const invariant = require('invariant');

View File

@@ -6,6 +6,8 @@
*/ */
import {ipcRenderer} from 'electron'; import {ipcRenderer} from 'electron';
// $FlowFixMe perf_hooks is a new API in node
import {performance} from 'perf_hooks';
import type {Store} from '../reducers/index.js'; import type {Store} from '../reducers/index.js';
import type Logger from '../fb-stubs/Logger.js'; import type Logger from '../fb-stubs/Logger.js';

View File

@@ -27,6 +27,8 @@ import {
ToggleButton, ToggleButton,
SidebarExtensions, SidebarExtensions,
} from 'flipper'; } from 'flipper';
// $FlowFixMe perf_hooks is a new API in node
import {performance} from 'perf_hooks';
import type {TrackType} from '../../fb-stubs/Logger.js'; import type {TrackType} from '../../fb-stubs/Logger.js';

View File

@@ -11,6 +11,8 @@ import type {UninitializedClient} from '../UninitializedClient';
import {isEqual} from 'lodash'; import {isEqual} from 'lodash';
import {RecurringError} from '../utils/errors'; import {RecurringError} from '../utils/errors';
import iosUtil from '../fb-stubs/iOSContainerUtility'; import iosUtil from '../fb-stubs/iOSContainerUtility';
// $FlowFixMe perf_hooks is a new API in node
import {performance} from 'perf_hooks';
export type State = {| export type State = {|
devices: Array<BaseDevice>, devices: Array<BaseDevice>,

View File

@@ -1,24 +0,0 @@
/**
* Copyright 2018-present Facebook.
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
* @format
*/
let debugId = 0;
export function mark(): string {
const id = String(debugId++);
if (typeof performance === 'object') {
performance.mark(id);
}
return id;
}
export function measure(id: string, name: string) {
if (typeof performance === 'object') {
performance.measure(name, id);
performance.clearMeasures(id);
performance.clearMarks(id);
}
}

View File

@@ -6,8 +6,6 @@
*/ */
import {transform} from '@babel/core'; import {transform} from '@babel/core';
import generate from '@babel/generator';
import electronStubs from '../electron-stubs'; import electronStubs from '../electron-stubs';
const babelOptions = { const babelOptions = {
@@ -23,14 +21,3 @@ test('transform electron requires to inlined stubs', () => {
expect(body.type).toBe('ExpressionStatement'); expect(body.type).toBe('ExpressionStatement');
expect(body.expression.properties.map(p => p.key.name)).toContain('remote'); expect(body.expression.properties.map(p => p.key.name)).toContain('remote');
}); });
test('transform performance calls to requires', () => {
for (const method of ['mark', 'measure']) {
const src = `performance.${method}('something')`;
const transformed = transform(src, babelOptions).ast;
const {code} = generate(transformed);
expect(code).toBe(
`require('perf_hooks').performance.${method}('something');`,
);
}
});

View File

@@ -11,7 +11,6 @@ var fs = require('fs');
var electronStubs = babylon.parseExpression( var electronStubs = babylon.parseExpression(
fs.readFileSync('static/electron-stubs.notjs').toString(), fs.readFileSync('static/electron-stubs.notjs').toString(),
); );
var perfHooks = babylon.parseExpression("require('perf_hooks').performance");
module.exports = function(babel) { module.exports = function(babel) {
return { return {
@@ -28,17 +27,6 @@ module.exports = function(babel) {
path.replaceWith(electronStubs); path.replaceWith(electronStubs);
} }
} }
if (
path.node.type === 'CallExpression' &&
path.node.callee.type === 'MemberExpression' &&
path.node.callee.object.name === 'performance'
) {
// 'perf_hooks' was added in Node 8.5.0 but doesn't appear to be
// present in electron. We can remove this and switch to using
// interval when it is. Until then, continue using browser.performance
// for real and swap in node's perf_hooks when we dont have electron.
path.node.callee.object = perfHooks;
}
}, },
}, },
}; };