From d75f3e286d41f8d8c17077a4a90b51c783e59f0e Mon Sep 17 00:00:00 2001 From: joao-paulo-parity Date: Mon, 12 Jul 2021 23:11:41 -0300 Subject: [PATCH] decrease command execution overhead --- .gitignore | 3 ++- README.md | 2 -- bench.js | 43 ++++++++++++++----------------------------- run | 2 +- 4 files changed, 17 insertions(+), 33 deletions(-) diff --git a/.gitignore b/.gitignore index eb84e7e..7211f75 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ node_modules *.pem .env git -log.txt +runner_stdout.txt +runner_exitcode.txt diff --git a/README.md b/README.md index e5128e7..bb48068 100644 --- a/README.md +++ b/README.md @@ -88,8 +88,6 @@ After it's running, the logs will be to the systemd journal: `sudo journalctl -u benchbot.service` -As well as to `./log.txt`. - # Github Settings ## Permissions diff --git a/bench.js b/bench.js index 121f831..941b33f 100644 --- a/bench.js +++ b/bench.js @@ -1,3 +1,4 @@ +const fs = require("fs") const cp = require("child_process") const path = require("path") @@ -8,6 +9,10 @@ function errorResult(message, error) { let cwd = process.cwd(); console.log(`process cwd: ${cwd}`); +const wrapCmd = path.join(__dirname, "wrap_cmd.sh") +const runnerOutput = path.join(__dirname, "runner_stdout.txt") +const runnerExitCode = path.join(__dirname, "runner_exitcode.txt") + const Mutex = require('async-mutex').Mutex; const mutex = new Mutex(); var shell = require('shelljs'); @@ -29,41 +34,21 @@ function BenchContext(app, config) { try { if (shouldLogOutput) { console.log(`<=== Start command output (cwd: ${process.cwd()})`) + cp.execFileSync("/bin/dash", ["-c", `${cmd} | tee ${runnerOutput}`], { stdio: "inherit" }) + stdout = fs.readFileSync(runnerOutput).toString() + } else { + stdout = cp.execSync(cmd, { stdio: "pipe", shell: true }).toString() } - - await new Promise(function (resolve) { - const proc = cp.spawn("/bin/bash", ["-c", cmd], { stdio: "pipe" }) - - proc.stdout.on("data", function (data) { - data = data.toString() - - if (data && shouldLogOutput) { - console.log(data.trim()) - } - - stdout += data - }) - - proc.stderr.on("data", function (data) { - data = data.toString() - - if (data && shouldLogOutput) { - console.log(data.trim()) - } - - stderr += data - }) - - proc.on("close", function (code) { - error = !!code - resolve() - }) - }) } catch (err) { error = true if (err.code) { + app.log(`Command ${cmd} failed with error code ${error.code}`); stdout = err.stdout.toString() + stderr = err.stderr.toString() + if (stderr) { + app.log(`stderr: ${stderr.trim()}`); + } } else { app.log.error("Caught exception in command execution") app.log.error(err) diff --git a/run b/run index 9a83af6..3eb06ae 100755 --- a/run +++ b/run @@ -139,7 +139,7 @@ main() { source ~/.cargo/env && \ cd "$install_location" && \ yarn && \ - yarn start 2>&1 | tee -a log.txt + yarn start 2>&1 } follow_service_logs() {