From 3373401b94eafdff48847c1c03b5893a59f53ced Mon Sep 17 00:00:00 2001 From: joao-paulo-parity Date: Mon, 12 Jul 2021 23:11:41 -0300 Subject: [PATCH 1/6] decrease command execution overhead --- .gitignore | 2 +- README.md | 2 -- bench.js | 42 +++++++++++++----------------------------- run | 2 +- 4 files changed, 15 insertions(+), 33 deletions(-) diff --git a/.gitignore b/.gitignore index eb84e7e..18cfd66 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,4 @@ node_modules *.pem .env git -log.txt +runner_stdout.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..a4b2d3f 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,9 @@ 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 Mutex = require('async-mutex').Mutex; const mutex = new Mutex(); var shell = require('shelljs'); @@ -29,41 +33,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() { From fdb04e799cc7f57e9d6411456bbcb98acb016929 Mon Sep 17 00:00:00 2001 From: joao-paulo-parity Date: Tue, 13 Jul 2021 04:13:55 -0300 Subject: [PATCH 2/6] inherit -> ignore --- bench.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bench.js b/bench.js index a4b2d3f..694da92 100644 --- a/bench.js +++ b/bench.js @@ -33,7 +33,7 @@ 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" }) + cp.execFileSync("/bin/dash", ["-c", `${cmd} | tee ${runnerOutput}`], { stdio: "ignore" }) stdout = fs.readFileSync(runnerOutput).toString() } else { stdout = cp.execSync(cmd, { stdio: "pipe", shell: true }).toString() From a860dd5710aa1298f22fc4754244a6848658dbbb Mon Sep 17 00:00:00 2001 From: joao-paulo-parity Date: Tue, 13 Jul 2021 04:22:10 -0300 Subject: [PATCH 3/6] execute the command through the shell --- bench.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/bench.js b/bench.js index 694da92..d899781 100644 --- a/bench.js +++ b/bench.js @@ -33,7 +33,7 @@ function BenchContext(app, config) { try { if (shouldLogOutput) { console.log(`<=== Start command output (cwd: ${process.cwd()})`) - cp.execFileSync("/bin/dash", ["-c", `${cmd} | tee ${runnerOutput}`], { stdio: "ignore" }) + cp.execFileSync("/bin/dash", ["-c", cmd], { stdio: "ignore" }) stdout = fs.readFileSync(runnerOutput).toString() } else { stdout = cp.execSync(cmd, { stdio: "pipe", shell: true }).toString() @@ -42,8 +42,6 @@ function BenchContext(app, config) { 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()}`); From 52d5518ca1b09f34ae6f9cd2db2299aec4042c38 Mon Sep 17 00:00:00 2001 From: joao-paulo-parity Date: Tue, 13 Jul 2021 04:29:16 -0300 Subject: [PATCH 4/6] reintroduce shelljs --- bench.js | 29 +++++------------------------ 1 file changed, 5 insertions(+), 24 deletions(-) diff --git a/bench.js b/bench.js index d899781..e7cf5cc 100644 --- a/bench.js +++ b/bench.js @@ -28,32 +28,13 @@ function BenchContext(app, config) { app.log(title) } - let stdout = "", stderr = "", error = false + const { stdout, stderr, code } = shell.exec(cmd, { silent: !shouldLogOutput }); + var error = false - try { - if (shouldLogOutput) { - console.log(`<=== Start command output (cwd: ${process.cwd()})`) - cp.execFileSync("/bin/dash", ["-c", cmd], { stdio: "ignore" }) - stdout = fs.readFileSync(runnerOutput).toString() - } else { - stdout = cp.execSync(cmd, { stdio: "pipe", shell: true }).toString() - } - } catch (err) { + if (code != 0) { + app.log(`Command failed with error code ${code}: ${cmd}`) + console.log(stderr) error = true - if (err.code) { - app.log(`Command ${cmd} failed with error code ${error.code}`); - stderr = err.stderr.toString() - if (stderr) { - app.log(`stderr: ${stderr.trim()}`); - } - } else { - app.log.error("Caught exception in command execution") - app.log.error(err) - } - } finally { - if (shouldLogOutput) { - console.log("===> Finished command output") - } } return { stdout, stderr, error }; From a8154180fa4fcc4485cb02a4d3e0d97c6bb1f452 Mon Sep 17 00:00:00 2001 From: joao-paulo-parity Date: Tue, 13 Jul 2021 04:32:56 -0300 Subject: [PATCH 5/6] make runTask synchronous again --- bench.js | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/bench.js b/bench.js index e7cf5cc..d5db7fb 100644 --- a/bench.js +++ b/bench.js @@ -23,7 +23,7 @@ function BenchContext(app, config) { self.app = app; self.config = config; - self.runTask = async function(cmd, { title, shouldLogOutput } = {}) { + self.runTask = function(cmd, { title, shouldLogOutput } = {}) { if (title) { app.log(title) } @@ -88,33 +88,33 @@ const prepareBranch = async function( const repositoryPath = path.join(gitDirectory, repo) var { url } = await getPushDomain() - await benchContext.runTask(`git clone ${url}/${owner}/${repo} ${repositoryPath}`); + benchContext.runTask(`git clone ${url}/${owner}/${repo} ${repositoryPath}`); shell.cd(repositoryPath) - var { error } = await benchContext.runTask(`git add . && git reset --hard HEAD`); + var { error } = benchContext.runTask(`git add . && git reset --hard HEAD`); if (error) return errorResult(stderr); - var { error, stdout } = await benchContext.runTask("git rev-parse HEAD"); + var { error, stdout } = benchContext.runTask("git rev-parse HEAD"); if (error) return errorResult(stderr); const detachedHead = stdout.trim() // Check out to the detached head so that any branch can be deleted - var { error, stderr } = await benchContext.runTask(`git checkout ${detachedHead}`); + var { error, stderr } = benchContext.runTask(`git checkout ${detachedHead}`); if (error) return errorResult(stderr); // Recreate PR remote - await benchContext.runTask(`git remote remove pr`); + benchContext.runTask(`git remote remove pr`); var { url } = await getPushDomain() - var { error, stderr } = await benchContext.runTask(`git remote add pr ${url}/${contributor}/${repo}.git`); + var { error, stderr } = benchContext.runTask(`git remote add pr ${url}/${contributor}/${repo}.git`); if (error) return errorResult(`Failed to add remote reference to ${owner}/${repo}`); // Fetch and recreate the PR's branch - await benchContext.runTask(`git branch -D ${branch}`); - var { error, stderr } = await benchContext.runTask(`git fetch pr ${branch} && git checkout --track pr/${branch}`, `Checking out ${branch}...`); + benchContext.runTask(`git branch -D ${branch}`); + var { error, stderr } = benchContext.runTask(`git fetch pr ${branch} && git checkout --track pr/${branch}`, `Checking out ${branch}...`); if (error) return errorResult(stderr); // Fetch and merge master - var { error, stderr } = await benchContext.runTask(`git pull origin ${baseBranch}`, `Merging branch ${baseBranch}`); + var { error, stderr } = benchContext.runTask(`git pull origin ${baseBranch}`, `Merging branch ${baseBranch}`); if (error) return errorResult(stderr); } @@ -141,7 +141,7 @@ function benchBranch(app, config) { var error = await prepareBranch(config, { benchContext }) if (error) return error - var { stderr, error, stdout } = await benchContext.runTask(benchCommand, { + var { stderr, error, stdout } = benchContext.runTask(benchCommand, { title: `Benching branch ${config.branch}...`, shouldLogOutput: true }); @@ -371,7 +371,7 @@ function benchmarkRuntime(app, config) { var error = await prepareBranch(config, { benchContext }) if (error) return error - var { stdout, stderr } = await benchContext.runTask(benchCommand, { + var { stdout, stderr } = benchContext.runTask(benchCommand, { title: `Benching runtime in branch ${config.branch}...`, shouldLogOutput: true }); @@ -381,15 +381,15 @@ function benchmarkRuntime(app, config) { if (benchCommand.includes("--output")) { const regex = /--output(?:=|\s+)(".+?"|\S+)/; const path = benchCommand.match(regex)[1]; - await benchContext.runTask(`git add ${path}`); - await benchContext.runTask(`git commit -m "${benchCommand}"`); + benchContext.runTask(`git add ${path}`); + benchContext.runTask(`git commit -m "${benchCommand}"`); const target = `${config.contributor}/${config.repo}` const { url, token } = await config.getPushDomain() try { - await benchContext.runTask(`git remote set-url pr ${url}/${target}.git`, "Setting up remote for PR"); - await benchContext.runTask(`git push pr HEAD`); + benchContext.runTask(`git remote set-url pr ${url}/${target}.git`, "Setting up remote for PR"); + benchContext.runTask(`git push pr HEAD`); } catch (error) { extraInfo = `NOTE: Failed to push commits to repository: ${error.toString().replace(token, "{secret}", "g")}` } From 975c8073b31d35698ce4a6a32d00c3e70160e74c Mon Sep 17 00:00:00 2001 From: joao-paulo-parity Date: Tue, 13 Jul 2021 04:53:43 -0300 Subject: [PATCH 6/6] reintroduce shell execution --- bench.js | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/bench.js b/bench.js index d5db7fb..2759e01 100644 --- a/bench.js +++ b/bench.js @@ -9,7 +9,6 @@ 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 Mutex = require('async-mutex').Mutex; @@ -28,13 +27,34 @@ function BenchContext(app, config) { app.log(title) } - const { stdout, stderr, code } = shell.exec(cmd, { silent: !shouldLogOutput }); - var error = false + let stdout = "", stderr = "", error = false - if (code != 0) { - app.log(`Command failed with error code ${code}: ${cmd}`) - console.log(stderr) + 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() + } + } 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) + } + } finally { + if (shouldLogOutput) { + console.log("===> Finished command output") + } } return { stdout, stderr, error };