#!/bin/bash

SOURCE_DIR="tmlt/core"
TAG_PREFIX="tmlt.core"

PYDOCSTYLE_ENABLE="true"

TEST_MIN_COVERAGE="70%"

SPHINX_EXTRA_OPTS="-n -W --keep-going"

function benchmark() {
    echo "=== Benchmarking Core ==="
    mkdir benchmark_output/

    declare -A benchmarks failed
    benchmarks=(
        [private_join]=17m
        [count_sum]=25m
        [quantile]=42m
        [adding_noise]=7m
        [sparkmap]=17m
        [sparkflatmap]=7m
        [public_join]=14m
    )
    set +e
    for b in ${!benchmarks[@]}; do
        echo "Running benchmark $b (timeout: ${benchmarks[$b]})..."
        # This is wrapped in a subshell because it forces the time output to
        # show up in the right order; otherwise, in the CI it can sometimes
        # print after following commands due to some oddities of how the Bash
        # built-in time works.
        (
            time timeout ${benchmarks[$b]} \
                 python benchmark/benchmarking_privacy_framework_$b.py \
                 > benchmark_output/$b.html
        )
        ret=$?
        if [[ $ret -eq 124 ]]; then
            failed+=( [$b]="timed out" )
        elif [[ $ret -ne 0 ]]; then
            failed+=( [$b]="script failed" )
        fi
    done
    set -e

    if [[ ${#failed[@]} -gt 0 ]]; then
        echo "Failed benchmarks:"
        for b in ${!failed[@]}; do
            echo "- $b: ${failed[$b]}"
        done
        return 1
    fi
}
