Skip to content

Instantly share code, notes, and snippets.

@jindrichmynarz
Created June 24, 2016 20:48
Show Gist options
  • Select an option

  • Save jindrichmynarz/9bfa589e85171d84170b9af108a14a0b to your computer and use it in GitHub Desktop.

Select an option

Save jindrichmynarz/9bfa589e85171d84170b9af108a14a0b to your computer and use it in GitHub Desktop.
Compute average response time for given SPARQL query and endpoint
#!/bin/bash
#
# Compute average response time for given SPARQL query and endpoint
#
# Note: For proper benchmarking use SPARQL Query Benchmarker instead.
# <https://sourceforge.net/p/sparql-query-bm/wiki/Introduction/>
set -e
die () {
echo >&2 "$@"
exit 1
}
usage () {
echo "Computes average response time for given SPARQL query and endpoint.
Usage: $(basename "$0") [-vh] -q query -e endpoint [-n runs] [-p pause]
[query] = Path to file with a SPARQL query (required)
[endpoint] = URL of a SPARQL endpoint (required)
[runs] = Number of times the query should be run (defaults to 10)
[pause] = Number of seconds to pause between queries (defaults to 1)
-v = Print verbose output
-h = Print this help message"
}
# Argument defaults
n=10
pause=1
verbose='false'
while getopts :q:e:n:p:vh opt
do
case $opt in
q) query="$OPTARG" ;;
e) endpoint="$OPTARG" ;;
n) n=$OPTARG ;;
p) pause=$OPTARG ;;
v) verbose='true' ;;
h) usage; exit ;;
:) die "Missing argument for -$OPTARG" ;;
\?) die "Illegal option: -$OPTARG" ;;
esac
done
# Validate arguments
[ $query ] || die "Missing SPARQL query!"
[ $endpoint ] || die "Missing SPARQL endpoint!"
! [ -f "$query" ] && die "File $query doesn't exist!"
! [ -r "$query" ] && die "File $query cannot be read!"
echo $n | grep -E -q "^[0-9]+$" || die "Number of runs must be a number!"
echo $pause | grep -E -q "^[0-9]+(\.[0-9]+)?$" || die "Pause must be a number!"
[ $(curl -o /dev/null -s -I --write-out "%{http_code}" "$endpoint") -eq 200 ] || \
die "SPARQL endpoint $endpoint is unreachable!"
rtimes=()
$verbose && printf "Response times:\n"
for (( i=1; i<=$n; i++ ))
do
rtime=$(curl -s -G --data-urlencode query@"$query" "$endpoint" -o /dev/null -w "%{time_starttransfer}")
rtimes+=($rtime)
$verbose && echo "$rtime"
sleep $pause
done
# Compute average and standard deviation
avg=$(IFS=+; echo "scale=2;(${rtimes[*]})/${#rtimes[@]}" | bc | awk '{printf "%.2f", $0}')
stdev=$(for rtime in ${rtimes[@]}; do echo "$rtime"; done | \
awk '{sum+=$0; sumsq+=$0 * $0} END {print sqrt(sumsq/NR - (sum/NR)**2)}')
echo "Average response time: $avg"
echo "Standard deviation: $stdev"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment