#!/bin/bash
P=sqlite-bench
DEFAULT_VERSION=3090200
. $SHELLPACK_INCLUDE/common.sh
TIME_CMD=`which time`
if [ "$TIME_CMD" = "" ]; then
        TIMEFORMAT="%2Uuser %2Ssystem %Relapsed %P%%CPU"
        TIME_CMD="time"
fi

# Basic argument parser
TASKSET_SERVER=
TASKSET_CLIENT=
SERVERSIDE_COMMAND=none
SERVERSIDE_NAME=`date +%Y%m%d-%H%M-%S`

while [ "$1" != "" ]; do
	case "$1" in
	-v)
		VERSION=$2
		shift 2
		;;
	--serverside-command)
		SERVERSIDE_COMMAND=$2
		shift 2
		;;
	--serverside-name)
		SERVERSIDE_NAME=$2
		shift 2
		;;
	--install-only)
		INSTALL_ONLY=yes
		shift
		;;
	--install-force)
		INSTALL_FORCE=yes
		shift
		;;
	--size)
		SQLITE_SIZE=$2
		shift 2
		;;
	*)
		echo Unrecognised option: $1
		shift
	esac
done
if [ "$TASKSET_SERVER" != "" ]; then
	echo TASKSET_SERVER: $TASKSET_SERVER
	echo TASKSET_CLIENT: $TASKSET_CLIENT
fi
if [ -z "$VERSION" ]; then
	VERSION=$DEFAULT_VERSION
fi

if [ "$INSTALL_FORCE" = "yes" ]; then
	rm -rf $SHELLPACK_SOURCES/sqlite-${VERSION}
fi
if [ ! -d $SHELLPACK_SOURCES/sqlite-${VERSION}-installed ]; then
	mmtests_activity source-install
	$SHELLPACK_INCLUDE/shellpack-install-sqlite -v ${VERSION}  || die sqlite install script returned error
	mmtests_activity source-installed
fi
cd $SHELLPACK_SOURCES/sqlite-${VERSION}-installed || die Failed to cd to sqlite install directory
if [ "$INSTALL_ONLY" = "yes" ]; then
	echo sqlite installed only as requested.
	exit $SHELLPACK_SUCCESS
fi
install-depends util-linux

# Include monitor hooks
. $SHELLPACK_INCLUDE/include-monitor.sh
LINESTART=`grep -n "==== BEGIN perl-trans.pl" $0 | tail -1 | awk -F : '{print $1}'`
LINEEND=`grep -n "==== END perl-trans.pl" $0 | tail -1 | awk -F : '{print $1}'`
if [ "$LINEEND" = "" ]; then
	LINECOUNT=`wc -l $0 | awk '{print $1}'`
fi
if [ "$LINESTART" = "" ]; then
	die Failed to find start of file perl-trans.pl
fi
echo Extracting $SHELLPACK_TEMP/perl-trans.pl
sed -n $((LINESTART+1)),$((LINEEND-1))p $0 > $SHELLPACK_TEMP/perl-trans.pl
chmod a+x $SHELLPACK_TEMP/perl-trans.pl

cd $SHELLPACK_SOURCES/sqlite-${VERSION}-installed || die Failed to cd to sqlite install directory

echo Creating insert script for $SQLITE_SIZE entries
cat /dev/urandom | base64 -w 20 | head -$SQLITE_SIZE | sed "s/\(.\{4\}\)\(.\{16\}\)/INSERT INTO 'mmtests' ('SmallInt', 'DateTime', 'ShortString', 'LongString') VALUES ('10', CURRENT_TIMESTAMP, '\1', '\2');/" > basic-insert.script
cp basic-insert.script $LOGDIR_RESULTS/

mmtests_activity sqlite-insert
monitor_pre_hook $LOGDIR_RESULTS 1

echo Creating table
rm -f $SHELLPACK_DATA/benchmark.db
./bin/sqlite3 $SHELLPACK_DATA/benchmark.db "CREATE TABLE mmtests
	('SmallInt'    SMALLINT NOT NULL,
	 'DateTime'    TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
	 'ShortString' VARCHAR(4) NOT NULL,
	 'LongString'  VARCHAR(16) NOT NULL
	);" || die Failed to create table

echo Inserting rows
cat basic-insert.script |							\
	$TIME_CMD -o $LOGDIR_RESULTS/sqlite.time				\
		$SHELLPACK_TEMP/perl-trans.pl $SHELLPACK_DATA/benchmark.db	\
		| tee -a $LOGDIR_RESULTS/sqlite.log
	ls -lh $SHELLPACK_DATA/benchmark.db
monitor_post_hook $LOGDIR_RESULTS 1

exit $SHELLPACK_SUCCESS

==== BEGIN perl-trans.pl ====
#!/usr/bin/perl

use strict;
use Time::HiRes qw/ time sleep /;

open(SQLITE, "|./bin/sqlite3 $ARGV[0]") || die("Failed to exec sqlite3");

my $threshold = 10;
my $nr_trans = 0;
my $last_trans = 0;
my $last_time;
my $current_time = $last_time = time;

$SIG{ALRM} = sub {
	alarm 1;

	my $type = "execute";
	if ($threshold > 0) {
		$threshold--;
		$type = "warmup ";
	}
	my $current_time = time;
	my $time_diff = $current_time - $last_time;
	my $seconds_trans = ($nr_trans - $last_trans) / $time_diff;
	print "$type $seconds_trans\n";
	$last_time = $current_time;
	$last_trans = $nr_trans;
};

alarm 1;
while (!eof(STDIN)) {
	my $line = <STDIN>;
	print SQLITE $line;
	$nr_trans++;
}

alarm 0;
close(PIPE);
==== END perl-trans.pl ====
#### Description sqlite
#### Details sqlite-bench 27
