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

Add benchmark script #161

Merged
merged 1 commit into from
Sep 14, 2021
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
__pycache__
resources
*.kate-swp
benchmark.*.txt
89 changes: 89 additions & 0 deletions benchmark.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#!/bin/sh
set -eu

width=1920
height=1080
fps=60
timeout=180
video_url="https://google.github.io/mediapipe/images/selfie_segmentation_web.mp4"

for ffmpeg in ffmpeg avconv ""; do
test -n "$ffmpeg" || {
echo "This script needs ffmpeg or avconv."
exit 1
}
command -v "$ffmpeg" >/dev/null && break || :
done

# get sudo password and keep it active
sudo -v
( while sleep 1; do sudo -v; done; ) &
pids="$!"

# create file for test video
video="$(mktemp)"

# registister cleanup function
cleanup() {
rm -f -- "$video"
kill $pids; wait
sudo -- $SHELL -c 'modprobe -r v4l2loopback; modprobe v4l2loopback'
}
trap cleanup EXIT

# create the necessary v4l2loopback devices
sudo -- $SHELL -s <<EOF
modprobe -r v4l2loopback
modprobe v4l2loopback devices=2 exclusive_caps=1,1 video_nr=99,100
EOF

# download video and loop it to /dev/video99
wget -qO "$video" "$video_url"
"$ffmpeg" -hide_banner -loglevel quiet -hwaccel auto \
-re -stream_loop -1 -i "$video" \
-vf scale="$width:$height" -r "$fps" \
-f v4l2 -an -vcodec rawvideo -pix_fmt yuyv422 /dev/video99 &
pids="$pids $!"
sleep 3

git_head="$(git rev-parse HEAD)"
{
echo "${width}x${height}@$fps for $timeout seconds"
test $# -eq 0 || echo "extra arguments: $*"
echo "HEAD: $git_head"; echo

# system information
uname -srvmo
sed -n 's/^model name\s*:\s*//p' /proc/cpuinfo | sort | uniq -c; echo

# python information
python3 -c '
import importlib.metadata, sys
venv = (hasattr(sys, "real_prefix") or
(hasattr(sys, "base_prefix") and sys.base_prefix != sys.prefix))
print("python", sys.version)
print("virtualenv", venv)
print()
for module in sys.argv[1:]:
try:
print(module, importlib.metadata.version(module))
except importlib.metadata.PackageNotFoundError:
print(module, "not found")
' $(sed -rn 's/^([A-Z0-9][A-Z0-9._-]*[A-Z0-9]).*/\1/pi' requirements.txt); echo

# run fake.py for specified timeout and gather statistics
export PYTHONUNBUFFERED=yes
timeout --foreground --preserve-status -s QUIT "$timeout" \
./fake.py -w /dev/video99 -v /dev/video100 \
-W "$width" -H "$height" -F "$fps" --no-ondemand "$@" |
stdbuf -oL tr '\r' '\n' |
python3 -c '
import sys, statistics
fps = []
for line in sys.stdin:
if line.startswith("FPS:"):
fps.append(float(line.split(":")[-1].strip()))
print(fps[-1], end="\r", file=sys.stderr)
print("avg %.2f, stdev %.2f" %(statistics.mean(fps), statistics.stdev(fps)))
'
} | tee "benchmark.$git_head.txt"