|
17 | 17 | [download-image]: https://img.shields.io/npm/dm/electrom.svg |
18 | 18 | [download-url]: https://npmjs.org/package/electrom |
19 | 19 |
|
20 | | -> Electrom is a resource management solution for Electron applications, which is convenient for performance management and friendly debugging of multiple windows. |
| 20 | +<p align="center"><img src="./demo.gif" width="600px" /></p> |
21 | 21 |
|
22 | | - |
| 22 | +> Electrom is a resource management solution for Electron applications, which is convenient for performance inspection and friendly debugging of multiple webContents. |
23 | 23 |
|
24 | | -<!-- GITCONTRIBUTOR_START --> |
| 24 | +## Installation |
25 | 25 |
|
26 | | -## Contributors |
| 26 | +```bash |
| 27 | +$ npm i electrom --save |
| 28 | +``` |
27 | 29 |
|
28 | | -|[<img src="https://avatars.githubusercontent.com/u/1011681?v=4" width="100px;"/><br/><sub><b>xudafeng</b></sub>](https://github.com/xudafeng)<br/>|[<img src="https://avatars.githubusercontent.com/u/2226423?v=4" width="100px;"/><br/><sub><b>yantze</b></sub>](https://github.com/yantze)<br/>|[<img src="https://avatars.githubusercontent.com/u/30524126?v=4" width="100px;"/><br/><sub><b>z0gSh1u</b></sub>](https://github.com/z0gSh1u)<br/>|[<img src="https://avatars.githubusercontent.com/u/17586742?v=4" width="100px;"/><br/><sub><b>sriting</b></sub>](https://github.com/sriting)<br/>|[<img src="https://avatars.githubusercontent.com/u/52845048?v=4" width="100px;"/><br/><sub><b>snapre</b></sub>](https://github.com/snapre)<br/>|[<img src="https://avatars.githubusercontent.com/u/11213298?v=4" width="100px;"/><br/><sub><b>WynterDing</b></sub>](https://github.com/WynterDing)<br/>| |
29 | | -| :---: | :---: | :---: | :---: | :---: | :---: | |
| 30 | +## How It Works |
30 | 31 |
|
| 32 | +Electrom consists of three parts: |
31 | 33 |
|
32 | | -This project follows the git-contributor [spec](https://github.com/xudafeng/git-contributor), auto updated at `Thu Jun 05 2025 11:33:16 GMT+0800`. |
| 34 | +1. **Main process (`Monitor`)** — collects CPU/memory metrics from all Electron processes at a configurable interval and emits data events. |
| 35 | +2. **Preload script (`BROWSER_WINDOW_PRELOAD_PATH`)** — a built-in preload script that bridges IPC between main and renderer. Must be set as `webPreferences.preload` on your `BrowserWindow`. |
| 36 | +3. **Renderer UI** — a built-in HTML dashboard (`BROWSER_WINDOW_ASSETS_PATH`) or an embeddable React component (`PerfBoard`) you can mount in your own frontend. |
33 | 37 |
|
34 | | -<!-- GITCONTRIBUTOR_END --> |
| 38 | +## Integration Guide |
| 39 | + |
| 40 | +### Step 1 — Open a dedicated monitor window (Recommended) |
| 41 | + |
| 42 | +This is the simplest approach: electrom provides a fully built dashboard page you can load directly into a `BrowserWindow`. |
35 | 43 |
|
36 | | -## Installment |
| 44 | +```typescript |
| 45 | +// main process |
| 46 | +const { BrowserWindow } = require('electron'); |
| 47 | +const { Monitor, BROWSER_WINDOW_PRELOAD_PATH, BROWSER_WINDOW_ASSETS_PATH, EVENT_DATA_CHANNEL_NAME } = require('electrom'); |
| 48 | + |
| 49 | +module.exports = (app) => { |
| 50 | + // 1. Create the monitor instance (default polling interval: 5000ms) |
| 51 | + const monitor = new Monitor(); |
| 52 | + |
| 53 | + // 2. Create a dedicated BrowserWindow for the dashboard |
| 54 | + // IMPORTANT: set the built-in preload script here |
| 55 | + const win = new BrowserWindow({ |
| 56 | + width: 1280, |
| 57 | + height: 600, |
| 58 | + webPreferences: { |
| 59 | + preload: BROWSER_WINDOW_PRELOAD_PATH, |
| 60 | + }, |
| 61 | + }); |
| 62 | + |
| 63 | + // 3. Load the built-in dashboard UI |
| 64 | + win.loadURL(BROWSER_WINDOW_ASSETS_PATH); |
| 65 | + |
| 66 | + // 4. Start monitoring after the page is ready |
| 67 | + win.webContents.on('dom-ready', () => { |
| 68 | + // Forward metrics data from main process to the renderer |
| 69 | + monitor.removeAllListeners(EVENT_DATA_CHANNEL_NAME); |
| 70 | + monitor.on(EVENT_DATA_CHANNEL_NAME, (data) => { |
| 71 | + if (win && !win.isDestroyed() && win.webContents && !win.webContents.isDestroyed()) { |
| 72 | + win.webContents.send(EVENT_DATA_CHANNEL_NAME, data); |
| 73 | + } |
| 74 | + }); |
| 75 | + // Allow the dashboard to send actions back (open DevTools, kill process, etc.) |
| 76 | + monitor.bindEventToWindow(win); |
| 77 | + monitor.start(); |
| 78 | + }); |
37 | 79 |
|
38 | | -```shell |
39 | | -npm i electrom --save-dev |
| 80 | + win.once('ready-to-show', () => win.show()); |
| 81 | +}; |
40 | 82 | ``` |
41 | 83 |
|
42 | | -## How to use |
| 84 | +> **Full example:** [example/main.ts](./example/main.ts) · [electron-modules-sample/src/electrom.ts](https://github.com/electron-modules/electron-modules-sample/blob/main/src/electrom.ts) |
43 | 85 |
|
44 | | -Please visit the demo code |
| 86 | +--- |
45 | 87 |
|
46 | | -- [./example/main.ts](./example/main.ts) |
47 | | -- [electron-modules/electron-modules-sample/main/src/electrom.ts](https://github.com/electron-modules/electron-modules-sample/blob/main/src/electrom.ts) |
| 88 | +### Step 2 — Embed the monitor into your existing main window |
| 89 | + |
| 90 | +If you prefer to show metrics inside your own page instead of a separate window, forward the data channel to your main window: |
48 | 91 |
|
49 | 92 | ```typescript |
50 | | -// main process: import electrom |
51 | | -import { |
52 | | - EVENT_DATA_CHANNEL_NAME, |
53 | | - BROWSER_WINDOW_PRELOAD_PATH, |
54 | | - Monitor, |
55 | | -} from 'electrom'; |
| 93 | +import { Monitor, EVENT_DATA_CHANNEL_NAME, BROWSER_WINDOW_PRELOAD_PATH } from 'electrom'; |
| 94 | + |
| 95 | +// Your existing BrowserWindow must also use the preload script |
| 96 | +const mainWindow = new BrowserWindow({ |
| 97 | + webPreferences: { |
| 98 | + preload: BROWSER_WINDOW_PRELOAD_PATH, |
| 99 | + }, |
| 100 | +}); |
56 | 101 |
|
57 | 102 | const monitor = new Monitor(); |
58 | | -/** |
59 | | - * Please set this script's path as `webPreferences.preload` of `BrowserWindow`. |
60 | | - * { |
61 | | - * preload: BROWSER_WINDOW_PRELOAD_PATH |
62 | | - * } |
63 | | - */ |
| 103 | + |
64 | 104 | mainWindow.webContents.on('dom-ready', () => { |
65 | | - monitor.on(EVENT_DATA_CHANNEL_NAME, (data: any) => { |
| 105 | + monitor.on(EVENT_DATA_CHANNEL_NAME, (data) => { |
66 | 106 | mainWindow.webContents.send(EVENT_DATA_CHANNEL_NAME, data); |
67 | 107 | }); |
68 | 108 | monitor.bindEventToWindow(mainWindow); |
69 | 109 | monitor.start(); |
70 | 110 | }); |
71 | 111 | ``` |
72 | 112 |
|
73 | | -## Perf Board |
| 113 | +--- |
| 114 | + |
| 115 | +### Monitor options |
| 116 | + |
| 117 | +```typescript |
| 118 | +const monitor = new Monitor({ |
| 119 | + threshold: 5000, // polling interval in ms (default: 5000) |
| 120 | + dumpDir: null, // directory to write JSON snapshots; null = disabled |
| 121 | + dumpThreshold: 10000, // minimum interval between snapshot writes in ms (default: 10000) |
| 122 | +}); |
| 123 | +``` |
| 124 | + |
| 125 | +To change options after construction: |
| 126 | + |
| 127 | +```typescript |
| 128 | +monitor.setOptions({ dumpDir: '/tmp/electrom-dumps' }); |
| 129 | +``` |
| 130 | + |
| 131 | +--- |
| 132 | + |
| 133 | +### Exported constants |
| 134 | + |
| 135 | +| Constant | Description | |
| 136 | +| ----------------------------- | ------------------------------------------------------------------------------- | |
| 137 | +| `BROWSER_WINDOW_PRELOAD_PATH` | Absolute path to the built-in preload script. Use as `webPreferences.preload`. | |
| 138 | +| `BROWSER_WINDOW_ASSETS_PATH` | `file://` URL of the built-in dashboard HTML page. Use with `win.loadURL(...)`. | |
| 139 | +| `EVENT_DATA_CHANNEL_NAME` | IPC channel name for metrics data (`'electrom:monitor:data'`). | |
| 140 | +| `EVENT_ACTION_CHANNEL_NAME` | IPC channel name for UI actions (`'electrom:monitor:action'`). | |
| 141 | + |
| 142 | +--- |
74 | 143 |
|
75 | | -You can use the Perf-Board standalone in your front-end. |
| 144 | +## PerfBoard — Embed FPS & memory stats in your renderer |
76 | 145 |
|
77 | | -```javascript |
| 146 | +`PerfBoard` is a standalone React component that shows an FPS counter and memory usage overlay directly inside your own renderer page. It does **not** require a separate window. |
| 147 | + |
| 148 | +```tsx |
78 | 149 | import React from 'react'; |
79 | 150 | import PerfBoard from 'electrom/src/PerfBoard'; |
80 | 151 |
|
81 | | -function() { |
| 152 | +export default function App() { |
82 | 153 | return ( |
83 | | - <PerfBoard /> |
| 154 | + <div style={{ position: 'relative' }}> |
| 155 | + {/* PerfBoard overlays itself at the top-left corner */} |
| 156 | + <PerfBoard /> |
| 157 | + {/* ...the rest of your app */} |
| 158 | + </div> |
84 | 159 | ); |
85 | 160 | } |
86 | 161 | ``` |
87 | 162 |
|
| 163 | +--- |
| 164 | + |
| 165 | +## PerfTracing — Chrome tracing dumps |
| 166 | + |
| 167 | +`PerfTracing` uses Electron's `contentTracing` API to continuously capture Chromium performance traces and write them as `.json` files. The files can be opened in `chrome://tracing` or Perfetto. |
| 168 | + |
| 169 | +```typescript |
| 170 | +import { PerfTracing } from 'electrom'; |
| 171 | + |
| 172 | +// Call once in app 'ready'. It loops automatically. |
| 173 | +PerfTracing({ |
| 174 | + dumpTargetDir: path.join(process.cwd(), '.electrom'), // where to write trace files |
| 175 | + partitionThreshold: 30_000, // ms per trace segment (default: 10000) |
| 176 | + memoryDumpConfig: { |
| 177 | + triggers: [{ mode: 'light', periodic_interval_ms: 10_000 }], |
| 178 | + }, |
| 179 | +}); |
| 180 | +``` |
| 181 | + |
| 182 | +--- |
| 183 | + |
| 184 | +## Offline Reporter |
| 185 | + |
| 186 | +If you enabled `dumpDir` on the `Monitor`, you can post-process the collected JSON snapshots into a human-readable report: |
| 187 | + |
| 188 | +```typescript |
| 189 | +import { pickDataFromDir, genTextReporter, renderHtmlReporter } from 'electrom'; |
| 190 | + |
| 191 | +const data = pickDataFromDir('/tmp/electrom-dumps'); |
| 192 | + |
| 193 | +// Plain-text summary (avg/max/min CPU & memory per process) |
| 194 | +const { str } = genTextReporter(data); |
| 195 | +console.log(str); |
| 196 | + |
| 197 | +// HTML report — write to a file and open in a browser |
| 198 | +import fs from 'fs'; |
| 199 | +fs.writeFileSync('report.html', renderHtmlReporter(data)); |
| 200 | +``` |
| 201 | + |
| 202 | +--- |
| 203 | + |
88 | 204 | ## TODO |
89 | 205 |
|
90 | 206 | - [ ] heapdump |
91 | 207 |
|
92 | 208 | ## License |
93 | 209 |
|
94 | 210 | The MIT License (MIT) |
| 211 | + |
| 212 | +<!-- GITCONTRIBUTOR_START --> |
| 213 | + |
| 214 | +## Contributors |
| 215 | + |
| 216 | +| [<img src="https://avatars.githubusercontent.com/u/1011681?v=4" width="100px;"/><br/><sub><b>xudafeng</b></sub>](https://github.com/xudafeng)<br/> | [<img src="https://avatars.githubusercontent.com/u/2226423?v=4" width="100px;"/><br/><sub><b>yantze</b></sub>](https://github.com/yantze)<br/> | [<img src="https://avatars.githubusercontent.com/u/30524126?v=4" width="100px;"/><br/><sub><b>z0gSh1u</b></sub>](https://github.com/z0gSh1u)<br/> | [<img src="https://avatars.githubusercontent.com/u/17586742?v=4" width="100px;"/><br/><sub><b>sriting</b></sub>](https://github.com/sriting)<br/> | [<img src="https://avatars.githubusercontent.com/u/52845048?v=4" width="100px;"/><br/><sub><b>snapre</b></sub>](https://github.com/snapre)<br/> | [<img src="https://avatars.githubusercontent.com/u/11213298?v=4" width="100px;"/><br/><sub><b>WynterDing</b></sub>](https://github.com/WynterDing)<br/> | |
| 217 | +| :------------------------------------------------------------------------------------------------------------------------------------------------: | :--------------------------------------------------------------------------------------------------------------------------------------------: | :-----------------------------------------------------------------------------------------------------------------------------------------------: | :-----------------------------------------------------------------------------------------------------------------------------------------------: | :---------------------------------------------------------------------------------------------------------------------------------------------: | :-----------------------------------------------------------------------------------------------------------------------------------------------------: | |
| 218 | + |
| 219 | +This project follows the git-contributor [spec](https://github.com/xudafeng/git-contributor), auto updated at `Thu Jun 05 2025 11:33:16 GMT+0800`. |
| 220 | + |
| 221 | +<!-- GITCONTRIBUTOR_END --> |
0 commit comments