Skip to content

Commit 2b3d72b

Browse files
authored
Update measure-memory.mjs (#17116)
1 parent 3205eb6 commit 2b3d72b

File tree

1 file changed

+34
-10
lines changed

1 file changed

+34
-10
lines changed

packages/backend/scripts/measure-memory.mjs

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,11 @@ import { dirname, join } from 'node:path';
1818
const __filename = fileURLToPath(import.meta.url);
1919
const __dirname = dirname(__filename);
2020

21+
const SAMPLE_COUNT = 3; // Number of samples to measure
2122
const STARTUP_TIMEOUT = 120000; // 120 seconds timeout for server startup
2223
const MEMORY_SETTLE_TIME = 10000; // Wait 10 seconds after startup for memory to settle
2324

2425
async function measureMemory() {
25-
const startTime = Date.now();
26-
2726
// Start the Misskey backend server using fork to enable IPC
2827
const serverProcess = fork(join(__dirname, '../built/boot/entry.js'), ['expose-gc'], {
2928
cwd: join(__dirname, '..'),
@@ -107,12 +106,7 @@ async function measureMemory() {
107106
vmSize: null,
108107
};
109108
} catch {
110-
memoryInfo = {
111-
rss: null,
112-
heapUsed: null,
113-
vmSize: null,
114-
error: 'Could not measure memory',
115-
};
109+
throw new Error('Failed to get memory usage via ps command');
116110
}
117111
}
118112

@@ -137,15 +131,45 @@ async function measureMemory() {
137131

138132
const result = {
139133
timestamp: new Date().toISOString(),
140-
startupTimeMs: startupTime,
141134
memory: memoryInfo,
142135
};
143136

137+
return result;
138+
}
139+
140+
async function main() {
141+
// 直列の方が時間的に分散されて正確そうだから直列でやる
142+
const results = [];
143+
for (let i = 0; i < SAMPLE_COUNT; i++) {
144+
const res = await measureMemory();
145+
results.push(res);
146+
}
147+
148+
// Calculate averages
149+
const avgMemory = {
150+
rss: 0,
151+
heapUsed: 0,
152+
vmSize: 0,
153+
};
154+
for (const res of results) {
155+
avgMemory.rss += res.memory.rss ?? 0;
156+
avgMemory.heapUsed += res.memory.heapUsed ?? 0;
157+
avgMemory.vmSize += res.memory.vmSize ?? 0;
158+
}
159+
avgMemory.rss = Math.round(avgMemory.rss / SAMPLE_COUNT);
160+
avgMemory.heapUsed = Math.round(avgMemory.heapUsed / SAMPLE_COUNT);
161+
avgMemory.vmSize = Math.round(avgMemory.vmSize / SAMPLE_COUNT);
162+
163+
const result = {
164+
timestamp: new Date().toISOString(),
165+
memory: avgMemory,
166+
};
167+
144168
// Output as JSON to stdout
145169
console.log(JSON.stringify(result, null, 2));
146170
}
147171

148-
measureMemory().catch((err) => {
172+
main().catch((err) => {
149173
console.error(JSON.stringify({
150174
error: err.message,
151175
timestamp: new Date().toISOString(),

0 commit comments

Comments
 (0)