#!/bin/bash
progress() {
    LINE="$( printf "$@" )"
    echo "$( date +'%Y-%m-%d %H:%M:%S') $LINE"
    psql -d template1 -U postgres -qAtX -c "SELECT ':PROGRESS: $LINE'" &>/dev/null
}

cat run.test.data | while read LINE
do
    table="$( echo "$LINE" | awk '{print $1}' | sed 's/^pages_/index/' )"
    words="$( echo "$LINE" | sed 's/^[^[:space:]]*[[:space:]]*//' )"
    progress 'Starting test of table %s' "$table"
    for word in $words
    do
        progress '    Searches (count(*)) for %s on %s' "$word" "$table"
        for i in $( seq 1 10 )
        do
            time search --config /usr/local/etc/sphinx.conf --index $table --limit 0 $word 2>&1 | grep . | tail -n 1
        done
        progress '    Searches (by created) for %s on %s' "$word" "$table"
        for i in $( seq 1 10 )
        do
            time search --config /usr/local/etc/sphinx.conf --index $table --sortby "created asc" --limit 50 $work 2>&1 | grep . | wc -l
        done
        progress '    Searches (by title) for %s on %s' "$word" "$table"
        for i in $( seq 1 10 )
        do
            time search --config /usr/local/etc/sphinx.conf --index $table --sortby "title asc" --limit 50 $work 2>&1 | grep . | wc -l
        done
    done
done
