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
Copy link
Member

@ludovicm67 ludovicm67 Nov 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to run it using the following command:

npx . --shapes="../cube-link/validation/$1.ttl" \
 --valid-cases="../cube-link/test/$1/valid*.ttl" \
 --invalid-cases="../cube-link/test/$1/invalid*.ttl" \
 --command="npx b59 cube check-metadata --profile" \
 --prefixes=schema,cube=https://cube.link/ \
 --concurrency=8 --debug

(I cloned the cube-link repo next to the shacl-test repo).

And here are the errors I can see from my side (translated from French):

line 108: ../cube-link/test//valid*.ttl: No such file or directory
line 102 : [:  : integer expression expected

So am I doing something wrong while invocating the command?
Could you provide a working example, so that I can try it?

I took some inspiration from here: https://github.com/zazuko/cube-link/blob/7c2e6db06051b2234d85b82116dd6d16f00c62d5/test/check-metadata.sh

Also, is there any reason to keep this as a shell script instead of a NodeJS script?
That way this would be able to work on more systems, including ones that doesn't bring bash by default.

"$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