Skip to content

Commit b7744c6

Browse files
committed
docs: update docs
1 parent 0ad79c2 commit b7744c6

File tree

3 files changed

+12979
-32
lines changed

3 files changed

+12979
-32
lines changed

README.md

Lines changed: 158 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,74 +21,201 @@
2121
2222
![](./demo.gif)
2323

24-
<!-- GITCONTRIBUTOR_START -->
24+
## Installation
2525

26-
## Contributors
26+
```shell
27+
npm i electrom --save-dev
28+
```
2729

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
3031

32+
Electrom consists of three parts:
3133

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.
3337

34-
<!-- GITCONTRIBUTOR_END -->
38+
## Integration Guide
3539

36-
## Installment
40+
### Step 1 — Open a dedicated monitor window (Recommended)
3741

38-
```shell
39-
npm i electrom --save-dev
42+
This is the simplest approach: electrom provides a fully built dashboard page you can load directly into a `BrowserWindow`.
43+
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+
});
79+
80+
win.once('ready-to-show', () => win.show());
81+
};
4082
```
4183

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)
4385
44-
Please visit the demo code
86+
---
4587

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:
4891

4992
```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+
});
56101

57102
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+
64104
mainWindow.webContents.on('dom-ready', () => {
65-
monitor.on(EVENT_DATA_CHANNEL_NAME, (data: any) => {
105+
monitor.on(EVENT_DATA_CHANNEL_NAME, (data) => {
66106
mainWindow.webContents.send(EVENT_DATA_CHANNEL_NAME, data);
67107
});
68108
monitor.bindEventToWindow(mainWindow);
69109
monitor.start();
70110
});
71111
```
72112

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
74134

75-
You can use the Perf-Board standalone in your front-end.
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'`). |
76141

77-
```javascript
142+
---
143+
144+
## PerfBoard — Embed FPS & memory stats in your renderer
145+
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
78149
import React from 'react';
79150
import PerfBoard from 'electrom/src/PerfBoard';
80151

81-
function() {
152+
export default function App() {
82153
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>
84159
);
85160
}
86161
```
87162

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+
88204
## TODO
89205

90206
- [ ] heapdump
91207

92208
## License
93209

94210
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 -->

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,5 +113,10 @@
113113
"dist"
114114
]
115115
},
116-
"license": "MIT"
116+
"license": "MIT",
117+
"pnpm": {
118+
"onlyBuiltDependencies": [
119+
"electron"
120+
]
121+
}
117122
}

0 commit comments

Comments
 (0)