Skip to content
This repository has been archived by the owner on Jun 30, 2022. It is now read-only.

Decrease command execution overhead #47

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ node_modules
*.pem
.env
git
log.txt
runner_stdout.txt
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
73 changes: 28 additions & 45 deletions bench.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const fs = require("fs")
const cp = require("child_process")
const path = require("path")

Expand All @@ -8,6 +9,8 @@ function errorResult(message, error) {
let cwd = process.cwd();
console.log(`process cwd: ${cwd}`);

const runnerOutput = path.join(__dirname, "runner_stdout.txt")

const Mutex = require('async-mutex').Mutex;
const mutex = new Mutex();
var shell = require('shelljs');
Expand All @@ -19,7 +22,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)
}
Expand All @@ -29,41 +32,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)
Expand Down Expand Up @@ -125,33 +108,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);
}

Expand All @@ -178,7 +161,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
});
Expand Down Expand Up @@ -408,7 +391,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
});
Expand All @@ -418,15 +401,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")}`
}
Expand Down
2 changes: 1 addition & 1 deletion run
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down