#!{{ device.binaries_directory.rstrip('/') }}/sh


{% for sensor in sensors %}
GET_{{ sensor.label }}="cat {{ sensor.filepath }}"
{% endfor %}

LAUNCH_COMMAND="am start -W -n {{ package }}/{{ activity }} {{ options }}"
STOP_COMMAND="am force-stop {{ package }}"
TEMP_FILE=tmp.txt

TIME_RESULT=""
{% for sensor in sensors %}
{{ sensor.label }}=""
{% endfor %}

cd {{ device.working_directory }}

# esc esc down down down ENTER (this should bring up the apps menu)
input keyevent 111
sleep 1
input keyevent 111
sleep 1
input keyevent 20
sleep 1
input keyevent 20
sleep 1
input keyevent 20
sleep 1
input keyevent 66
sleep 1

# Warm up caches.
$LAUNCH_COMMAND
$STOP_COMMAND
$LAUNCH_COMMAND
$STOP_COMMAND
$LAUNCH_COMMAND
$STOP_COMMAND

{% if io_scheduler != None %}
echo {{ io_scheduler }} > /sys/block/mmcblk0/queue/scheduler
{% endif %}

for i in $({{ busybox }} seq 1 {{ iterations }})
do
    {% for sensor in sensors %}
    {{ sensor.label }}="${{ sensor.label }} `$GET_{{ sensor.label }}`"
    {% endfor %}

    {% if io_stress %}
    # Drop caches to get a cold start.
    sync; echo 3 > /proc/sys/vm/drop_caches
    # Run IO stress during App launch.
    {{ busybox }} dd if=/dev/zero of=write.img bs=1048576 count=2000 conv=fsync > dd_write.txt 2>&1 &
    io_write=$!
    {{ busybox }} dd if=/dev/block/mmcblk0 of=/dev/null bs=1048576 > dd_read.txt 2>&1 &
    io_read=$!
    {% endif %}

    $LAUNCH_COMMAND > $TEMP_FILE
    
    {% for sensor in sensors %}
    {{ sensor.label }}="${{ sensor.label }} `$GET_{{ sensor.label }}`"
    {% endfor %}

    TIME=`{{ busybox }} awk '{if($1~"TotalTime") print $2}' $TEMP_FILE`
    TIME_RESULT="$TIME_RESULT $TIME"
    {% if cleanup %}
    rm $TEMP_FILE
    {% if io_stress %}
    kill $io_write
    kill $io_read
    rm -f write.img
    {% endif %}
    {% endif %}

    $STOP_COMMAND
    sleep 2
done

{% for sensor in sensors %}
echo ${{ sensor.label }} > {{ sensor.label }}.result
{% endfor %}
echo $TIME_RESULT > time.result
# esc esc down down down ENTER (this should bring up the apps menu)
input keyevent 111
sleep 1
