Performance 2
This commit is contained in:
parent
d48831aa9e
commit
14e147924d
57
others/surge.mjs
Normal file
57
others/surge.mjs
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
import { execSync } from "child_process";
|
||||||
|
import os from "os";
|
||||||
|
import fs from "fs";
|
||||||
|
import path from "path";
|
||||||
|
|
||||||
|
const log = (msg) => console.log(`[SURGE] ${msg}`);
|
||||||
|
const surgePath = path.resolve("surge.config.json");
|
||||||
|
|
||||||
|
function generateSurgeFlags() {
|
||||||
|
const cpus = os.cpus().length;
|
||||||
|
const flags = [
|
||||||
|
"--max-old-space-size=4096",
|
||||||
|
"--optimize_for_size",
|
||||||
|
"--gc-global",
|
||||||
|
"--max-semi-space-size=256",
|
||||||
|
"--no-warnings",
|
||||||
|
"--expose-gc"
|
||||||
|
];
|
||||||
|
|
||||||
|
if (process.platform !== "win32") {
|
||||||
|
flags.push("--use-largepages");
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
uvThreadpoolSize: cpus * 2,
|
||||||
|
nodeFlags: flags
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function writeSurgeFile(flags) {
|
||||||
|
fs.writeFileSync(surgePath, JSON.stringify(flags, null, 2));
|
||||||
|
log("Surge config saved.");
|
||||||
|
}
|
||||||
|
|
||||||
|
function launchSurgedBackend(flags) {
|
||||||
|
const args = [
|
||||||
|
...flags.nodeFlags,
|
||||||
|
"index.mjs",
|
||||||
|
"--surged"
|
||||||
|
];
|
||||||
|
log(`Launching backend: node ${args.join(" ")}`);
|
||||||
|
execSync(`node ${args.join(" ")}`, { stdio: "inherit" });
|
||||||
|
}
|
||||||
|
|
||||||
|
function isSurgedRun() {
|
||||||
|
return process.argv.includes("--surged");
|
||||||
|
}
|
||||||
|
|
||||||
|
(async () => {
|
||||||
|
if (isSurgedRun()) return;
|
||||||
|
|
||||||
|
log("Detecting best settings...");
|
||||||
|
const flags = generateSurgeFlags();
|
||||||
|
writeSurgeFile(flags);
|
||||||
|
launchSurgedBackend(flags);
|
||||||
|
process.exit(0);
|
||||||
|
})();
|
Loading…
Reference in New Issue
Block a user