Skip to content

Commit 57593c9

Browse files
authored
fix: some fixes to the docker setup (#76)
1 parent 65f3869 commit 57593c9

File tree

10 files changed

+99
-55
lines changed

10 files changed

+99
-55
lines changed

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
registry=https://registry.npmjs.org

packages/cli/bin/expf.mjs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,20 @@ const { values, positionals } = parseArgs({
5252
duration: {
5353
type: 'string',
5454
short: 'd'
55+
},
56+
57+
'force-rebuild': {
58+
type: 'boolean'
5559
}
5660
}
5761
});
5862

63+
// Convert cli friendly values to JS friendly values
64+
values.repoRef = values['repo-ref'];
65+
delete values['repo-ref'];
66+
values.forceRebuild = values['force-rebuild'];
67+
delete values['force-rebuild'];
68+
5969
switch (positionals[2]) {
6070
case 'compare':
6171
try {

packages/cli/load.mjs

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ export function help (opts = {}) {
1919
--overrides='{"express":"latest"}'
2020
--config=./expf.config.json
2121
--[no-]write
22+
23+
Runners:
24+
- @expressjs/perf-runner-vanilla: local docker based runner
25+
- Flags: --force-rebuild
26+
- @expressjs/perf-runner-nsolid: local docker based runner with nsolid
27+
- Flags: --force-rebuild
2228
`
2329
}
2430

@@ -104,31 +110,34 @@ export default function main (_opts = {}) {
104110
// Import and start the runner
105111
const runner = (await import(opts.runner)).default;
106112

113+
let vers;
107114
try {
108-
const vers = await nv(opts.node, {
115+
vers = await nv(opts.node, {
109116
latestOfMajorOnly: true
110117
});
111-
112-
const results = await runner({
113-
cwd: opts.cwd,
114-
repo: opts.repo,
115-
test: opts.test,
116-
node: vers?.[0]?.version,
117-
overrides: opts.overrides,
118-
duration: opts.duration,
119-
signal: ac.signal
120-
});
121-
122-
if (opts.write !== false) {
123-
const outputFile = join(dirname(import.meta.resolve(opts.test).replace(/^file:/, '')), 'results', 'result-' + Date.now() + '.json');
124-
await mkdir(dirname(outputFile), { recursive: true });
125-
await writeFile(outputFile, JSON.stringify(results, null, 2));
126-
console.log(`written to: ${outputFile}`);
118+
} catch (e) {
119+
// If offline or cannt load node versions, use
120+
// the option as passed in without a default
121+
if (e.code === 'ENOTFOUND' && _opts.node) {
122+
vers = [{ version: _opts.node}];
127123
} else {
128-
console.log(results);
124+
throw e;
129125
}
130-
} catch (e) {
131-
console.error(e);
126+
}
127+
128+
const results = await runner({
129+
...opts,
130+
node: vers?.[0]?.version,
131+
signal: ac.signal
132+
});
133+
134+
if (opts.write !== false) {
135+
const outputFile = join(dirname(import.meta.resolve(opts.test).replace(/^file:/, '')), 'results', 'result-' + Date.now() + '.json');
136+
await mkdir(dirname(outputFile), { recursive: true });
137+
await writeFile(outputFile, JSON.stringify(results, null, 2));
138+
console.log(`written to: ${outputFile}`);
139+
} else {
140+
console.log(results);
132141
}
133142
completed = true;
134143

packages/runner-local-docker/index.mjs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,24 @@ export function createRunner (runnerConfig = {}) {
1717
const { node, os = 'bookworm' } = opts;
1818

1919
return new Promise((resolve, reject) => {
20+
const args = [
21+
node,
22+
os,
23+
name
24+
];
25+
if (opts.forceRebuild) {
26+
args.push('--force');
27+
}
2028
const cp = execFile(
2129
join(import.meta.dirname, 'scripts', 'build.sh'),
22-
[
23-
node,
24-
os,
25-
name
26-
],
30+
args,
2731
{ cwd: import.meta.dirname }
2832
);
2933

3034
cp.on('exit', () => {
3135
resolve({
3236
tag: `expf-runner-${name}:${node}-${os}`,
33-
node: node,
37+
node,
3438
name,
3539
runtime,
3640
apm

packages/runner-local-docker/scripts/build.sh

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,21 @@ if [ -z "$3" ]; then
1414
exit 1;
1515
fi;
1616

17+
# cd to the root of the repo
18+
SOURCE=${BASH_SOURCE[0]:-$0}
19+
while [ -L "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
20+
DIR=$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )
21+
SOURCE=$(readlink "$SOURCE")
22+
[[ $SOURCE != /* ]] && SOURCE=$DIR/$SOURCE # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
23+
done
24+
DIR=$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )
25+
cd "${DIR}/../../.."
26+
1727
# Build metadata bundle (from runner-base directory)
18-
cd "$(dirname "$0")/.."
28+
pushd ./packages/runner-local-docker
29+
pwd
1930
rollup metadata.mjs --file metadata-bundle.mjs --plugin @rollup/plugin-node-resolve
31+
popd
2032

2133
# Start docker daemon if not running
2234
if (! docker stats --no-stream >/dev/null 2>&1 ); then
@@ -36,7 +48,7 @@ RUNNER_TYPE=$3
3648
TAG="expf-runner-${RUNNER_TYPE}:${NODE_VERSION}-${NODE_BASE}"
3749

3850
# Check if runner directory exists (relative to packages dir)
39-
RUNNER_DIR="../runner-${RUNNER_TYPE}"
51+
RUNNER_DIR="packages/runner-${RUNNER_TYPE}"
4052
if [ ! -d "$RUNNER_DIR" ]; then
4153
echo "Runner directory not found: $RUNNER_DIR"
4254
exit 1
@@ -56,9 +68,7 @@ done
5668

5769
if [ -n "$FORCE" ] || [ -z "$(docker images -q $TAG 2> /dev/null)" ]; then
5870
echo "Building container: $TAG"
59-
# Use packages directory as build context, specify dockerfile relative to build context
60-
cd ..
61-
docker build -f "runner-${RUNNER_TYPE}/Dockerfile" . \
71+
docker build -f "packages/runner-${RUNNER_TYPE}/Dockerfile" . \
6272
--build-arg NODE_VERSION=${NODE_VERSION} \
6373
--build-arg OS=${NODE_BASE} \
6474
--build-arg RUNNER_TYPE=${RUNNER_TYPE} \

packages/runner-local-docker/scripts/start.sh

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ if [ -z "$TEST" ]; then
77
exit 1;
88
fi;
99

10+
RESULTS_DIR="$(pwd)/results"
11+
1012
#
1113
# Write out metadata file
1214
#
13-
node metadata.mjs > ./results/metadata.json
15+
node metadata.mjs > $RESULTS_DIR/metadata.json
1416

1517
# We can mount the path for local dev,
1618
# If not, require a repo and ref
@@ -138,26 +140,26 @@ $RUNTIME_CMD $NODE_OPTIONS \
138140
--perf-basic-prof-only-functions \
139141
--inspect=0.0.0.0:9229 \
140142
"$MAIN" \
141-
&> ./results/output.txt &
143+
&> $RESULTS_DIR/output.txt &
142144
SERVER_PID=$!
143145

144146
# Start perf
145-
perf record -F 99 -g -o ./results/perf.data -p $SERVER_PID &> ./results/perf.txt &
147+
perf record -F 99 -g -o $RESULTS_DIR/perf.data -p $SERVER_PID &> $RESULTS_DIR/perf.txt &
146148
PERF_PID=$!
147149

148150
on_sigint()
149151
{
150152
echo
151153
echo "Capture ending heap snapshot"
152-
npx -q @mmarchini/observe heap-snapshot -p ${SERVER_PID} --file ./results/end.heapsnapshot
154+
npx -q @mmarchini/observe heap-snapshot -p ${SERVER_PID} --file $RESULTS_DIR/end.heapsnapshot
153155

154156
echo "Closing server"
155157
kill -2 "$SERVER_PID" || true
156158
wait "$PERF_PID" || true
157159
local EX=$?
158160

159161
echo "Generating flamegraph"
160-
perf script -i ./results/perf.data | ./FlameGraph/stackcollapse-perf.pl | ./FlameGraph/flamegraph.pl --colors=js > ./results/profile.svg
162+
perf script -i $RESULTS_DIR/perf.data | /home/node/FlameGraph/stackcollapse-perf.pl | /home/node/FlameGraph/flamegraph.pl --colors=js > $RESULTS_DIR/profile.svg
161163

162164
EXIT_CODE=${EX:-0}
163165
}
@@ -172,7 +174,7 @@ cleanup() {
172174

173175
if [ -f "package-lock.json" ]; then
174176
echo "moving package-lock.json to results"
175-
mv package-lock.json ./results/package-lock.json
177+
mv package-lock.json $RESULTS_DIR/package-lock.json
176178
fi
177179
}
178180

@@ -185,13 +187,13 @@ while true; do
185187
if ! [ -d "/proc/${SERVER_PID}" ]; then
186188
echo "Exited prematurely (pid ${SERVER_PID}, code ${EXIT_CODE-unknown})"
187189
wait $SERVER_PID || true
188-
cat ./results/output.txt
190+
cat $RESULTS_DIR/output.txt
189191
EXIT_CODE=1
190192
break
191193
fi
192-
if grep -q "startup: " <(head ./results/output.txt); then
194+
if grep -q "startup: " <(head $RESULTS_DIR/output.txt); then
193195
echo "Capture starting heap snapshot"
194-
npx -q @mmarchini/observe heap-snapshot -p ${SERVER_PID} --file ./results/start.heapsnapshot
196+
npx -q @mmarchini/observe heap-snapshot -p ${SERVER_PID} --file $RESULTS_DIR/start.heapsnapshot
195197
break
196198
fi
197199
done

packages/runner-nsolid/Dockerfile

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,31 @@ RUN if [ ! -f /usr/bin/perf ] && [ -f /usr/bin/perf_5.10 ]; then \
2020
ln -s /usr/bin/perf_5.10 /usr/bin/perf; \
2121
fi
2222

23+
USER node
24+
WORKDIR /home/node
25+
26+
COPY .npmrc /home/node/.npmrc
27+
2328
# Setup @mmarchini/observe
2429
RUN <<-EOF
2530
npm install -g @mmarchini/observe@^3.0.0
2631
chown -R node:node /usr/local/lib/node_modules
2732
chown -R node:node /usr/local/bin/observe
2833
EOF
2934

30-
USER node
31-
WORKDIR /home/node
32-
3335
# Setup FlameGraph tools
3436
RUN git clone https://github.com/brendangregg/FlameGraph.git
3537

3638
VOLUME ["/home/node/repo"]
3739
VOLUME ["/home/node/results"]
38-
EXPOSE 3000 9229
40+
EXPOSE 3000 9229 1337
3941

4042
# Copy utilities directly from runner-base
41-
COPY runner-local-docker/metadata-bundle.mjs metadata.mjs
42-
COPY runner-local-docker/overrides.mjs overrides.mjs
43-
COPY runner-local-docker/scripts/start.sh start.sh
43+
COPY packages/runner-local-docker/metadata-bundle.mjs metadata.mjs
44+
COPY packages/runner-local-docker/overrides.mjs overrides.mjs
45+
COPY packages/runner-local-docker/scripts/start.sh start.sh
4446

4547
# Set runtime type for this container
4648
ENV RUNTIME_TYPE=nsolid
4749

48-
4950
ENTRYPOINT ["./start.sh"]

packages/runner-nsolid/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
"lint:fix": "semistandard --fix",
1010
"build": "node --run shared:build",
1111
"run": "node --run shared:run",
12-
"shared:build": "../runner-base/scripts/build.sh iron latest nsolid",
13-
"shared:run": "../runner-base/scripts/run.sh"
12+
"shared:build": "../runner-local-docker/scripts/build.sh iron latest nsolid",
13+
"shared:run": "../runner-local-docker/scripts/run.sh"
1414
},
1515
"keywords": [
1616
"express",

packages/runner-vanilla/Dockerfile

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ RUN apt-get update && \
1111
jq && \
1212
rm -rf /var/lib/apt/lists/*
1313

14+
COPY .npmrc /root/.npmrc
15+
16+
# Pointing at a local registry proxy
17+
RUN sed -i 's/127.0.0.1/host.docker.internal/' /root/.npmrc
18+
1419
# Setup @mmarchini/observe
1520
RUN <<-EOF
1621
npm install -g @mmarchini/observe@^3.0.0
@@ -21,6 +26,8 @@ EOF
2126
USER node
2227
WORKDIR /home/node
2328

29+
COPY .npmrc /home/node/.npmrc
30+
2431
# Setup FlameGraph tools
2532
RUN git clone https://github.com/brendangregg/FlameGraph.git
2633

@@ -29,9 +36,9 @@ VOLUME ["/home/node/results"]
2936
EXPOSE 3000 9229
3037

3138
# Copy utilities directly from runner-base
32-
COPY runner-local-docker/metadata-bundle.mjs metadata.mjs
33-
COPY runner-local-docker/overrides.mjs overrides.mjs
34-
COPY runner-local-docker/scripts/start.sh start.sh
39+
COPY packages/runner-local-docker/metadata-bundle.mjs metadata.mjs
40+
COPY packages/runner-local-docker/overrides.mjs overrides.mjs
41+
COPY packages/runner-local-docker/scripts/start.sh start.sh
3542

3643
# Set runtime type for this container
3744
ENV RUNTIME_TYPE=vanilla

packages/runner-vanilla/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
"lint:fix": "semistandard --fix",
1010
"build": "node --run shared:build",
1111
"run": "node --run shared:run",
12-
"shared:build": "../runner-base/scripts/build.sh 22.18.0 bookworm vanilla",
13-
"shared:run": "../runner-base/scripts/run.sh"
12+
"shared:build": "../runner-local-docker/scripts/build.sh 24 bookworm vanilla --force",
13+
"shared:run": "../runner-local-docker/scripts/run.sh"
1414
},
1515
"keywords": [
1616
"express",

0 commit comments

Comments
 (0)