<commit_before>#!/usr/bin/env bash

# after docker images have been built, this script spins up a yar
# deployment and runs a load test against that deployment

# :TODO: add check that docker images have been built

SCRIPT_DIR_NAME="$( cd "$( dirname "$0" )" && pwd )"

get_deployment_config() {
    local KEY=${1:-}
    local VALUE_IF_NOT_FOUND=${2:-}
    local VALUE=`grep "^\\s*$KEY\\s*=" ~/.yar.creds | \
        sed -e "s/^[[:space:]]*$KEY[[:space:]]*=[[:space:]]*//"`
    if [ "$VALUE" == "" ]; then
        echo $VALUE_IF_NOT_FOUND
    else
        echo $VALUE
    fi
}

# given an input file (geneated by apache benchmark), generate
# an output file which represents the nth percentile of the
# input file based on the 5th column (request time) in the
# input file.
take_percentile() {
    local PERCENTILE=${1:-}
    local INPUT_FILENAME=${2:-}
    local OUTPUT_FILENAME=${3:-}
	x=$(tail -n +2 $INPUT_FILENAME | wc -l)
	n=$(python -c "print int($x * $PERCENTILE)")
	sort \
        --field-separator=$'\t' \
        --key=5 \
        -n $INPUT_FILENAME | \
    head \
        -n $n | \
    sort \
        --field-separator=$'\t' \
        --key=2 \
        -n > $OUTPUT_FILENAME
}

# :TODO: what if either of these scripts fail?
# $SCRIPT_DIR_NAME/rm_all_containers.sh
# $SCRIPT_DIR_NAME/spin_up_deployment.sh

API_KEY=$(get_deployment_config "API_KEY")
# :TODO: what if API_KEY doesn't exist?

rm -f load_test_results* >& /dev/null

NUMBER_OF_REQUESTS=5000
PERCENTILE=98

CONCURRENCY=1
LOAD_TEST_RESULTS_DATA=$SCRIPT_DIR_NAME/load_test_results-$CONCURRENCY-$NUMBER_OF_REQUESTS.tsv
LOAD_TEST_RESULTS_DATA_PERCENTILE=$SCRIPT_DIR_NAME/load_test_results-$CONCURRENCY-$NUMBER_OF_REQUESTS-$PERCENTILE.tsv
LOAD_TEST_RESULTS_PLOT=$SCRIPT_DIR_NAME/load_test_results-$CONCURRENCY-$NUMBER_OF_REQUESTS.jpg

ab \
    -c $CONCURRENCY \
    -n $NUMBER_OF_REQUESTS \
    -A $API_KEY: \
    -g $LOAD_TEST_RESULTS_DATA \
    http://172.17.0.7:8000/dave.html

take_percentile \
    $PERCENTILE \
    $LOAD_TEST_RESULTS_DATA \
    $LOAD_TEST_RESULTS_DATA_PERCENTILE

gnuplot \
    -e "input_filename='$LOAD_TEST_RESULTS_DATA_PERCENTILE'" \
    -e "output_filename='$LOAD_TEST_RESULTS_PLOT'" \
    -e "title='Concurrency = $CONCURRENCY; Number of Requests = $NUMBER_OF_REQUESTS; $PERCENTILE th Percentile'" \
    $SCRIPT_DIR_NAME/plot_load_test_results

convert \
    $SCRIPT_DIR_NAME/*.jpg \
    $SCRIPT_DIR_NAME/load_test_results.pdf
exit 0

ab -c 10 -n 2500 -A $API_KEY: -g load_test_results.tsv http://172.17.0.7:8000/dave.html
take_percentile
$SCRIPT_DIR_NAME/plot_load_test_results
mv $SCRIPT_DIR_NAME/load_test_results.jpg $SCRIPT_DIR_NAME/load_test_results_10x2500.jpg<commit_msg>move all load test functionality into single func<commit_after>#!/usr/bin/env bash

# after docker images have been built, this script spins up a yar
# deployment and runs a load test against that deployment

# :TODO: add check that docker images have been built

SCRIPT_DIR_NAME="$( cd "$( dirname "$0" )" && pwd )"

get_deployment_config() {
    local KEY=${1:-}
    local VALUE_IF_NOT_FOUND=${2:-}
    local VALUE=`grep "^\\s*$KEY\\s*=" ~/.yar.creds | \
        sed -e "s/^[[:space:]]*$KEY[[:space:]]*=[[:space:]]*//"`
    if [ "$VALUE" == "" ]; then
        echo $VALUE_IF_NOT_FOUND
    else
        echo $VALUE
    fi
}

# given an input file (geneated by apache benchmark), generate
# an output file which represents the nth percentile of the
# input file based on the 5th column (request time) in the
# input file.
take_percentile() {
    local PERCENTILE=${1:-}
    local INPUT_FILENAME=${2:-}
    local OUTPUT_FILENAME=${3:-}
	x=$(tail -n +2 $INPUT_FILENAME | wc -l)
	n=$(python -c "print int($x * $PERCENTILE)")
	sort \
        --field-separator=$'\t' \
        --key=5 \
        -n $INPUT_FILENAME | \
    head \
        -n $n | \
    sort \
        --field-separator=$'\t' \
        --key=2 \
        -n > $OUTPUT_FILENAME
}

run_load_test() {
    local NUMBER_OF_REQUESTS=${1:-}
    local CONCURRENCY=${2:-}
    local PERCENTILE=${3:-}

    LOAD_TEST_RESULTS_DATA=$SCRIPT_DIR_NAME/load_test_results-$CONCURRENCY-$NUMBER_OF_REQUESTS.tsv
    LOAD_TEST_RESULTS_DATA_PERCENTILE=$SCRIPT_DIR_NAME/load_test_results-$CONCURRENCY-$NUMBER_OF_REQUESTS-$PERCENTILE.tsv
    LOAD_TEST_RESULTS_PLOT=$SCRIPT_DIR_NAME/load_test_results-$CONCURRENCY-$NUMBER_OF_REQUESTS.jpg

    ab \
        -c $CONCURRENCY \
        -n $NUMBER_OF_REQUESTS \
        -A $API_KEY: \
        -g $LOAD_TEST_RESULTS_DATA \
        http://172.17.0.7:8000/dave.html

    take_percentile \
        $PERCENTILE \
        $LOAD_TEST_RESULTS_DATA \
        $LOAD_TEST_RESULTS_DATA_PERCENTILE

    gnuplot \
        -e "input_filename='$LOAD_TEST_RESULTS_DATA_PERCENTILE'" \
        -e "output_filename='$LOAD_TEST_RESULTS_PLOT'" \
        -e "title='Concurrency = $CONCURRENCY; Number of Requests = $NUMBER_OF_REQUESTS; $PERCENTILE th Percentile'" \
        $SCRIPT_DIR_NAME/plot_load_test_results
}

# :TODO: what if either of these scripts fail?
# $SCRIPT_DIR_NAME/rm_all_containers.sh
# $SCRIPT_DIR_NAME/spin_up_deployment.sh

API_KEY=$(get_deployment_config "API_KEY")
# :TODO: what if API_KEY doesn't exist?

rm -f load_test_results* >& /dev/null

NUMBER_OF_REQUESTS=5000
PERCENTILE=98

run_load_test $NUMBER_OF_REQUESTS 1 $PERCENTILE

convert \
    $SCRIPT_DIR_NAME/*.jpg \
    $SCRIPT_DIR_NAME/load_test_results.pdf
exit 0

ab -c 10 -n 2500 -A $API_KEY: -g load_test_results.tsv http://172.17.0.7:8000/dave.html
take_percentile
$SCRIPT_DIR_NAME/plot_load_test_results
mv $SCRIPT_DIR_NAME/load_test_results.jpg $SCRIPT_DIR_NAME/load_test_results_10x2500.jpg
{'fraction_non_alphanumeric': 0.13032626136566233, 'fraction_numerical': 0.019076484221786413, 'mean_word_length': 3.491593274619696, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3848866', 'n_tokens_mistral': 2254, 'n_tokens_neox': 2103, 'n_words': 414}