Skip to content

Commit 91443a3

Browse files
authored
Harden benchmark script against pull request changes (#185)
Require a new `TARGET` variable specifying the expected SHA of the commit at the HEAD of the pull request when running the benchmark script for pull requests. Use shallow clone in benchmark script. For consistency, rename the BRANCH parameter from use case 1 in the benchmark script to BASE.
1 parent 353fc6a commit 91443a3

File tree

1 file changed

+29
-5
lines changed
  • benchmarks/community-benchmark

1 file changed

+29
-5
lines changed

benchmarks/community-benchmark/run.sh

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,23 @@
11
#!/bin/bash
2+
function assertShaLike() {
3+
local shaVarName="${1}"
4+
local sha="${!1}"
5+
if [ -z "${sha}" ] || ! echo "${sha}" | grep -qE '^[0-9a-fA-F]+$'; then
6+
echo "${shaVarName} (${sha}) does not look like a commit SHA"
7+
exit
8+
fi
9+
}
10+
11+
function assertMatchesPullRequest() {
12+
local shaVarName="${1}"
13+
local sha="${!1}"
14+
local pr_id="${!2}"
15+
if [[ ! "$(git ls-remote origin refs/pull/${pr_id}/head)" =~ ^${sha}.*refs/pull/${pr_id}/head$ ]]; then
16+
echo "${shaVarName} (${sha}) does not match HEAD for pull request ${pr_id}"
17+
exit
18+
fi
19+
}
20+
221
function mandatory() {
322
if [ -z "${!1}" ]; then
423
echo "${1} not set"
@@ -25,8 +44,9 @@ echo "Use case 1: We want to test the impact of a PR on a branch."
2544
echo "To run this, declare:"
2645
echo "The script expects the following variables to be set:"
2746
echo "CATEGORY = a category of tests to run - folders in benchmark/"
28-
echo "BRANCH = the branch the test should be based off. e.g. master"
47+
echo "BASE = the branch the test should be based off. e.g. master"
2948
echo "PULL_ID = the pull request that contains changes to test"
49+
echo "TARGET = the SHA of the commit HEAD that contains changes to test"
3050
echo "-------------------------------------------------------------"
3151
echo "Use case 2: We want to compare two branches, tags or commits."
3252
echo "To run this, declare:"
@@ -48,8 +68,10 @@ if [ -z $PULL_ID ]; then
4868
mandatory TARGET
4969
else
5070
export USE_CASE=1
51-
mandatory BRANCH
71+
mandatory BASE
5272
mandatory PULL_ID
73+
mandatory TARGET
74+
assertShaLike TARGET
5375
fi
5476
mandatory CATEGORY
5577
optional RUNS
@@ -59,11 +81,13 @@ getMACHINE_THREADS=`cat /proc/cpuinfo |grep processor|tail -n1|awk {'print $3'}`
5981
let getMACHINE_THREADS=getMACHINE_THREADS+1 #getting threads this way is 0 based. Add one
6082
optional MACHINE_THREADS $getMACHINE_THREADS
6183
rm -rf node
62-
git clone https://github.com/nodejs/node.git
84+
git clone --depth=1 https://github.com/nodejs/node.git
6385
cd node
6486
case $USE_CASE in
6587
1)
66-
git checkout $BRANCH
88+
# Validate TARGET and pull request HEAD are consistent
89+
assertMatchesPullRequest TARGET PULL_ID
90+
git checkout $BASE
6791
;;
6892
2)
6993
git checkout $BASE
@@ -81,7 +105,7 @@ mv out/Release/node ./node-master
81105
# build pr
82106
case $USE_CASE in
83107
1)
84-
curl -L https://github.com/nodejs/node/pull/${PULL_ID}.patch|git apply -3
108+
curl -L "https://github.com/nodejs/node/compare/$(git rev-parse HEAD)...${TARGET}.patch"|git apply -3
85109
;;
86110
2)
87111
git checkout $TARGET

0 commit comments

Comments
 (0)