Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: run jobs in parallel #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .changeset/weak-ducks-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@zazuko/shacl-test": patch
---

Test cases are running in parallel. Defaults to 4 concurrent jobs. Use `--concurrency=X` to change.
107 changes: 65 additions & 42 deletions run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
SCRIPT_PATH="$(dirname "$(realpath "$0")")"
WORKING_DIR="$(pwd)"
FAILED=0
MAX_JOBS=4
CURRENT_JOBS=0

filter=''
debug=false
Expand Down Expand Up @@ -36,6 +38,9 @@ while [ $# -gt 0 ]; do
--command=*)
command="${1#*=}"
;;
--concurrency=*)
MAX_JOBS="${1#*=}"
;;
*)
printf "*******************************************\n"
printf "* Error: Invalid argument: %s *\n" "$1"
Expand All @@ -57,6 +62,7 @@ fi
command="$command $shapesPath"
if [ "$debug" = true ]; then
echo "🐞 Command: $command"
echo "🐞 Concurrency: $MAX_JOBS"
echo "🐞 Filter: $filter"
echo "🐞 Approval flags: $approvalsFlags"
echo ""
Expand All @@ -66,54 +72,71 @@ loadFullShape() {
"$SCRIPT_PATH"/load-graph.js "$1" | "$SCRIPT_PATH"/pretty-print.js --prefixes "${prefixes[@]}"
}

# Semaphore function to limit parallel jobs
semaphore() {
while [ "$CURRENT_JOBS" -ge "$MAX_JOBS" ]; do
wait -n
CURRENT_JOBS=$((CURRENT_JOBS - 1))
done
CURRENT_JOBS=$((CURRENT_JOBS + 1))
}

# iterate over valid cases, run validation and monitor exit code
for file in $validCases; do
name=$(basename "$file")
relativePath=$(node -e "console.log(require('path').relative('$WORKING_DIR', '$file'))")

# check if filter is set and skip if not matching
if [ -n "$filter" ] && ! echo "$file" | grep -iq "$filter"; then
echo "ℹ️SKIP - $relativePath"
continue
fi

{
sh -c "$command" > "$file.log" 2>&1
success=$?
} < "$file"

if [ $success -ne 0 ] ; then
"$SCRIPT_PATH"/report-failure.sh "$file" "$(loadFullShape "$shapesPath")" "$(cat "$file")"
FAILED=1
else
echo "✅ PASS - $relativePath"
fi
semaphore
(
name=$(basename "$file")
relativePath=$(node -e "console.log(require('path').relative('$WORKING_DIR', '$file'))")

# check if filter is set and skip if not matching
if [ -n "$filter" ] && ! echo "$file" | grep -iq "$filter"; then
echo "ℹ️SKIP - $relativePath"
exit 0
fi

{
sh -c "$command" > "$file.log" 2>&1
success=$?
} < "$file"

if [ $success -ne 0 ] ; then
"$SCRIPT_PATH"/report-failure.sh "$file" "$(loadFullShape "$shapesPath")" "$(cat "$file")"
FAILED=1
else
echo "✅ PASS - $relativePath"
fi
) &
done

# iterate over invalid cases
for file in $invalidCases; do
name=$(basename "$file")
relativePath=$(node -e "console.log(require('path').relative('$WORKING_DIR', '$file'))")

# skip if file does not exist
if [ ! -f "$file" ]; then
continue
fi

# check if pattern is set and skip if not matching
if [ -n "$filter" ] && ! echo "$file" | grep -iq "$filter"; then
echo "ℹ️SKIP - $relativePath"
continue
fi

report=$(sh -c "$command" < "$file" 2> "$file.log" | "$SCRIPT_PATH"/pretty-print.js --prefixes "${prefixes[@]}")

if ! echo "$report" | npx approvals "$name" --outdir "$(dirname "$file")" "$approvalsFlags" > /dev/null 2>&1 ; then
"$SCRIPT_PATH"/report-failure.sh "$file" "$(loadFullShape "$shapesPath")" "$(cat "$file")" "check results"
FAILED=1
else
echo "✅ PASS - $name"
fi
semaphore
(
name=$(basename "$file")
relativePath=$(node -e "console.log(require('path').relative('$WORKING_DIR', '$file'))")

# skip if file does not exist
if [ ! -f "$file" ]; then
exit 0
fi

# check if pattern is set and skip if not matching
if [ -n "$filter" ] && ! echo "$file" | grep -iq "$filter"; then
echo "ℹ️SKIP - $relativePath"
exit 0
fi

report=$(sh -c "$command" < "$file" 2> "$file.log" | "$SCRIPT_PATH"/pretty-print.js --prefixes "${prefixes[@]}")

if ! echo "$report" | npx approvals "$name" --outdir "$(dirname "$file")" "$approvalsFlags" > /dev/null 2>&1 ; then
"$SCRIPT_PATH"/report-failure.sh "$file" "$(loadFullShape "$shapesPath")" "$(cat "$file")" "check results"
FAILED=1
else
echo "✅ PASS - $name"
fi
) &
done

wait

exit $FAILED