-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(gatsby): Move memory benchmark to generic benchmark, add suppor…
…t for develop and custom site (#35703) * Move memory to generic benchmark, add support for develop * Fix develop build * Fix stats command * Rename benchmark and minor updates * Fix memory script * Stop container at end again
- Loading branch information
Showing
22 changed files
with
206 additions
and
119 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
benchmarks/memory/scripts/docker-connect → ...arks/docker-runner/scripts/docker-connect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
benchmarks/memory/scripts/docker-get-id → ...marks/docker-runner/scripts/docker-get-id
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
DOCKER_ID=$(\ | ||
docker ps --format '{{.Image}}:{{.ID}}' | \ | ||
grep "gatsby-memory" | \ | ||
grep "gatsby-generic-benchmark" | \ | ||
head -n 1 | \ | ||
sed 's/gatsby\-memory://'\ | ||
sed 's/gatsby\-generic\-benchmark://'\ | ||
) | ||
|
||
echo $DOCKER_ID |
9 changes: 5 additions & 4 deletions
9
benchmarks/memory/scripts/docker-start → ...hmarks/docker-runner/scripts/docker-start
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,26 @@ | ||
DOCKER_ID=$(./scripts/docker-get-id) | ||
if [ -n "$DOCKER_ID" ]; then | ||
echo "\nA gatsby-memory container is already running with id $DOCKER_ID." | ||
echo "\nA gatsby-generic-benchmark container is already running with id $DOCKER_ID." | ||
echo "Please use that container, or run \`yarn docker:stop\` to stop it.\n" | ||
return 1 | ||
fi | ||
|
||
MEMORY_LIMIT="${DOCKER_MEMORY_LIMIT:-2g}" | ||
BENCHMARK_SITE="${DOCKER_BENCHMARK_SITE:-$(pwd)}" | ||
|
||
DOCKER_ID=$(\ | ||
docker run -td \ | ||
--mount type=bind,source="$(pwd)/../..",target=/usr/src/gatsby \ | ||
--mount type=bind,source="$(pwd)",target=/usr/src/app \ | ||
--mount type=bind,source="${BENCHMARK_SITE}",target=/usr/src/app \ | ||
--publish 9229:9229 \ | ||
--publish 9000:9000 \ | ||
--memory="${MEMORY_LIMIT}" \ | ||
--memory-swap="${MEMORY_LIMIT}" \ | ||
gatsby-memory \ | ||
gatsby-generic-benchmark \ | ||
| head -c 12 \ | ||
) | ||
|
||
sleep 1 | ||
docker exec $DOCKER_ID bash -c "/usr/src/app/scripts/docker-write-memory &" | ||
docker exec $DOCKER_ID bash -c "/usr/src/gatsby/benchmarks/docker-runner/scripts/docker-write-memory &" | ||
|
||
echo "\nStarted container id ${DOCKER_ID} with ${MEMORY_LIMIT} of memory! Run \`yarn docker:connect\` to connect to the container.\n" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
benchmarks/memory/scripts/docker-stop → benchmarks/docker-runner/scripts/docker-stop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
const yargs = require(`yargs`) | ||
const util = require('util') | ||
const { exit } = require('process') | ||
const exec = require('child_process').exec | ||
const execPromise = util.promisify(exec) | ||
|
||
const args = yargs | ||
.option(`memory`, { | ||
default: '2g', | ||
describe: `The memory limit for the docker container`, | ||
}) | ||
.option(`num-nodes`, { | ||
default: '100', | ||
describe: `The number of nodes to generate in the build`, | ||
}) | ||
.option(`node-size`, { | ||
default: '1k', | ||
describe: `The rough size of each node generated in the build`, | ||
}) | ||
.option(`command`, { | ||
default: 'build', | ||
choices: ['build', 'develop'], | ||
describe: `The gatsby command to run for measurement`, | ||
}) | ||
.option(`site`, { | ||
default: process.cwd(), | ||
describe: `The path to the site to use for testing`, | ||
}) | ||
.option(`status`, { | ||
default: 'cold', | ||
choices: ['cold'], | ||
describe: `The status of the build`, | ||
}).parse() | ||
|
||
const hostExec = (cmd, promise = true) => { | ||
console.log(`\x1b[2m$ ${cmd}\x1b[0m`) | ||
return promise ? execPromise(cmd) : exec(cmd) | ||
} | ||
|
||
const getDockerExec = (dockerId) => { | ||
return (cmd, env = {}, promise = true) => { | ||
const envVars = Object.keys(env).map(key => `-e ${key}=${env[key]}`) | ||
return hostExec(`docker exec ${envVars.join(' ')} ${dockerId} ${cmd}`, promise) | ||
} | ||
} | ||
|
||
async function build(dockerExec) { | ||
let code = 139 | ||
let start = 0 | ||
|
||
// there's something buggy with the node/exec/docker-exec integration | ||
// we're getting seg faults, so this loop is just a patch for that | ||
// so we don't have to fix it right now | ||
|
||
while(code === 139) { | ||
try { | ||
console.log(` - clearing cache`) | ||
await dockerExec(`rm -rf .cache`) | ||
|
||
console.log(` - running build with ${args.numNodes} nodes of size ${args.nodeSize}`) | ||
|
||
start = Date.now() | ||
const { err } = await dockerExec( | ||
`yarn gatsby build`, | ||
{ | ||
BUILD_NUM_NODES: args.numNodes, | ||
BUILD_STRING_NODE_SIZE: args.nodeSize, | ||
NUM_KEYS_IN_LARGE_SIZE_OBJ: 1, | ||
} | ||
) | ||
code = err.code | ||
|
||
} catch (e) { | ||
code = e.code | ||
} | ||
|
||
if (code === 139) { | ||
console.log(` \x1b[31mSeg fault, trying again\x1b[0m`) | ||
} | ||
} | ||
|
||
return { | ||
code, | ||
time: Math.round((Date.now() - start) / 10) / 100 | ||
} | ||
} | ||
|
||
async function develop(dockerExec) { | ||
let code = undefined | ||
let message = undefined | ||
let start = 0 | ||
|
||
while(code === undefined) { | ||
console.log(` - clearing cache`) | ||
await dockerExec(`rm -rf .cache`) | ||
|
||
console.log(` - running develop with ${args.numNodes} nodes of size ${args.nodeSize}`) | ||
|
||
start = Date.now() | ||
const process = dockerExec( | ||
`yarn gatsby develop -H 0.0.0.0 -p 9000`, | ||
{ | ||
BUILD_NUM_NODES: args.numNodes, | ||
BUILD_STRING_NODE_SIZE: args.nodeSize, | ||
NUM_KEYS_IN_LARGE_SIZE_OBJ: 1 | ||
}, | ||
false | ||
) | ||
|
||
// wait until we see development bundle was successfully built, then exit | ||
process.stdout.on('data', (data) => { | ||
const line = data.toString() | ||
if (line.indexOf("Building development bundle -") >= 0) { | ||
message = line.trim() | ||
code = line.indexOf("success") >= 0 ? 0 : 1 | ||
process.kill() | ||
} | ||
}) | ||
|
||
process.on('exit', (c) => { | ||
if (!message) { | ||
code = c | ||
} | ||
}) | ||
|
||
while(code === undefined) { | ||
await new Promise(r => setTimeout(r, 100)); | ||
} | ||
|
||
if (code === 139) { | ||
console.log(` \x1b[31mSeg fault, trying again\x1b[0m`) | ||
code = undefined | ||
} | ||
} | ||
|
||
console.log(`\nFinal development message: ${message}`) | ||
|
||
return { | ||
code, | ||
time: Math.round((Date.now() - start) / 10) / 100 | ||
} | ||
} | ||
|
||
async function runTest() { | ||
|
||
const { stdout: testDockerId } = await hostExec(`./scripts/docker-get-id`) | ||
if (testDockerId.trim()) { | ||
console.log(`Docker container ${testDockerId.trim()} is currently running, shutting it down.`) | ||
await hostExec(`./scripts/docker-stop`); | ||
} | ||
|
||
console.log(`\nStarting container with ${args.memory} memory.`) | ||
const { stderr: dockerStartError } = await hostExec(`DOCKER_MEMORY_LIMIT=${args.memory} DOCKER_BENCHMARK_SITE=${args.site} ./scripts/docker-start`) | ||
if (dockerStartError) { | ||
console.log("Encountered an error:") | ||
console.log(dockerStartError) | ||
exit(1) | ||
} | ||
|
||
let { stdout: dockerId } = await hostExec(`./scripts/docker-get-id`) | ||
dockerId = dockerId.trim() | ||
|
||
const dockerExec = getDockerExec(dockerId) | ||
|
||
console.log(`\nStarting test using container ${dockerId}.`) | ||
|
||
const command = args.command === 'build' ? build : develop | ||
let {code, time} = await command(dockerExec) | ||
|
||
console.log(`Finished test in ${time}s`) | ||
|
||
if (code) { | ||
console.log(`\nFailed with code ${code}\n`) | ||
} else { | ||
console.log(`\nSuccess!\n`) | ||
} | ||
|
||
console.log(`Stopping container ${dockerId}`) | ||
await hostExec(`./scripts/docker-stop`) | ||
|
||
exit(code) | ||
} | ||
|
||
|
||
runTest(); |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.