#!/bin/bash

TIMEFORMAT=' %3lR'

function do_tests
{

echo "Base test:"

echo "Dropping caches..."
sync; echo 1 >/proc/sys/vm/drop_caches

echo -n "1st execution:"
time ./large_app_load_simul /tmp/375M_file
echo -n "2nd execution:"
time ./large_app_load_simul /tmp/375M_file
echo -n "3rd execution:"
time ./large_app_load_simul /tmp/375M_file
echo -n "4th execution:"
time ./large_app_load_simul /tmp/375M_file

echo "Reading a large file test:"

echo "Dropping caches..."
sync; echo 1 >/proc/sys/vm/drop_caches

echo -n "1st execution:"
time ./large_app_load_simul /tmp/375M_file
echo -n "2nd execution:"
time ./large_app_load_simul /tmp/375M_file
echo "Reading large file..."
cp /tmp/5G_file /dev/null
echo -n "3rd execution:"
time ./large_app_load_simul /tmp/375M_file
echo -n "4th execution:"
time ./large_app_load_simul /tmp/375M_file

echo "Copying (read+write) a large file test:"

echo "Dropping caches..."
sync; echo 1 >/proc/sys/vm/drop_caches

echo -n "1st execution:"
time ./large_app_load_simul /tmp/375M_file
echo -n "2nd execution:"
time ./large_app_load_simul /tmp/375M_file
echo "Copying large file..."
cp /tmp/5G_file /tmp/copy_of_5G_file
echo -n "3rd execution:"
time ./large_app_load_simul /tmp/375M_file
echo -n "4th execution:"
time ./large_app_load_simul /tmp/375M_file
rm /tmp/copy_of_5G_file

}

if [ $UID != 0 ]; then
	echo "Must be root."
	exit 0
fi

if [ ! -e "/tmp/5G_file" ]; then
	echo "Creating test files..."
	dd if=/dev/zero of=/tmp/375M_file bs=1M count=375
	dd if=/dev/zero of=/tmp/5G_file bs=1M count=5120
fi

echo "Tests with stock kernel (drop behind disabled):"
echo 0 >/proc/sys/vm/drop_behind

do_tests

echo "Tests with drop behind enabled:"
echo 1 >/proc/sys/vm/drop_behind

do_tests

exit 0

