From fa44d797e8c637e0f3c8e38f1e589026ad9d9205 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=ABck=20V=C3=A9zien?= Date: Tue, 6 Feb 2018 19:00:11 +0100 Subject: [PATCH] Add event for cpu usage --- flow-defs/process.js | 10 +--------- src/components/DevToolbar.js | 28 ++++++++++++++++++++++++++- src/internals/index.js | 37 ++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 10 deletions(-) diff --git a/flow-defs/process.js b/flow-defs/process.js index 37ad8d23..799dfd2a 100644 --- a/flow-defs/process.js +++ b/flow-defs/process.js @@ -1,11 +1,3 @@ /* eslint-disable */ -declare var process: { - send(args: any): void, - on(event: string, args: any): void, - nextTick(callback: Function): void, - setMaxListeners(any): void, - removeListener(string, Function): void, - title: string, - env: Object, -} +declare var process: Object diff --git a/src/components/DevToolbar.js b/src/components/DevToolbar.js index 293d9f6d..dc2cb145 100644 --- a/src/components/DevToolbar.js +++ b/src/components/DevToolbar.js @@ -1,6 +1,7 @@ // @flow import React, { PureComponent } from 'react' +import { ipcRenderer } from 'electron' import reduce from 'lodash/fp/reduce' import flow from 'lodash/fp/flow' import filter from 'lodash/fp/filter' @@ -99,21 +100,46 @@ const Color = ({ color }: { color: ColorType }) => ( type State = { isOpened: boolean, + cpuUsage: Object, } class DevToolbar extends PureComponent { state = { isOpened: false, + cpuUsage: {}, + } + + componentDidMount() { + ipcRenderer.on('msg', this.handleMessage) + } + + handleMessage = (e: any, { type, data }: Object) => { + if (type === 'usage.cpu') { + this.setState(prev => ({ + cpuUsage: { + ...prev.cpuUsage, + [data.name]: data.value, + }, + })) + } } handleToggle = () => this.setState({ isOpened: !this.state.isOpened }) render() { - const { isOpened } = this.state + const { isOpened, cpuUsage } = this.state + return ( {'DEV'} {colors.map(color => )} + + {Object.keys(cpuUsage).map(k => ( + + {k}: {cpuUsage[k]} + + ))} + ) } diff --git a/src/internals/index.js b/src/internals/index.js index 1e5bae99..b231dc60 100644 --- a/src/internals/index.js +++ b/src/internals/index.js @@ -32,3 +32,40 @@ const onMessage = payload => { } process.on('message', onMessage) + +if (__DEV__) { + let startTime = process.hrtime() + let startUsage = process.cpuUsage() + + setInterval(() => { + const now = Date.now() + + while (Date.now() - now < 500); + + const newStartTime = process.hrtime() + const newStartUsage = process.cpuUsage() + + const elapTime = process.hrtime(startTime) + const elapUsage = process.cpuUsage(startUsage) + + startTime = newStartTime + startUsage = newStartUsage + + const elapTimeMS = elapTime[0] * 1e3 + elapTime[1] / 1e6 + + const elapUserMS = elapUsage.user / 1000 // microseconds to milliseconds + const elapSystMS = elapUsage.system / 1000 + const cpuPercent = (100 * (elapUserMS + elapSystMS) / elapTimeMS).toFixed(1) + + sendEvent( + 'usage.cpu', + { + name: process.title, + value: cpuPercent, + }, + { + kill: false, + }, + ) + }, 5000) +}