Skip to content

Instantly share code, notes, and snippets.

@hosszukalman
Created October 3, 2019 06:48
Show Gist options
  • Save hosszukalman/e6ef7f22bb16e77417dbbf398a8421ea to your computer and use it in GitHub Desktop.
Save hosszukalman/e6ef7f22bb16e77417dbbf398a8421ea to your computer and use it in GitHub Desktop.
Randomised curl requests in bash.
#!/bin/bash
# The url to post.
url="http://example.com"
# Token for your user.
bearer=""
start_event_body() {
cat <<EOF
{
"event_type": "game_started",
"data": {}
}
EOF
}
timeout_event_body() {
cat <<EOF
{
"event_type": "timeout",
"data": {}
}
EOF
}
numerical_event_body() {
correct="false"
rand=$((1 + RANDOM % 5))
if [ $rand -gt 2 ]
then
correct="true"
fi
cat <<EOF
{
"event_type": "problem_solved",
"data": {
"exercise": "4*1 ? 10*1",
"difficulty": 2,
"answer": "<",
"correct": ${correct}
}
}
EOF
}
moving_shape_event_body() {
if [ $((1 + RANDOM % 10)) -gt 8 ]
then
nanoseconds=$(((6700 + RANDOM % 3000) * 999999))
cat <<EOF
{
"event_type": "target_missed",
"data": {
"difficulty": 2,
"missed_target_time_of_disappear": ${nanoseconds}
}
}
EOF
else
correct="false"
if [ $((1 + RANDOM % 5)) -gt 2 ]
then
correct="true"
fi
nanoseconds=$(((3700 + RANDOM % 6000) * 999999))
cat <<EOF
{
"event_type": "target_clicked",
"data": {
"difficulty": 2,
"correct": ${correct},
"reaction_time": ${nanoseconds}
}
}
EOF
fi
}
send_event() {
# echo "Request body: $($1)"
echo "Sendging a request..."
curl \
-H "Content-type: application/json" \
-H "Authorization: Bearer ${bearer}" \
--data "$($1)" \
-k \
${url}
echo "Request sent"
sleep_timer=$(awk -v min=1 -v max=3 'BEGIN{srand(); print min+rand()*(max-min+1)}')
sleep ${sleep_timer}
}
for (( i=0; i<=50; i++ ))
do
if [ $i -eq 0 ]
then
send_event "start_event_body"
continue
fi
if [ $((1 + RANDOM % 2)) -eq 1 ]
then
send_event "numerical_event_body"
else
send_event "moving_shape_event_body"
fi
if [ $i -eq 50 ]
then
send_event "timeout_event_body"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment