#!/usr/bin/php
<?

$server = mysql_connect("mysql.cs.orst.edu","kerob", "whoops");
@mysql_select_db("kerob2");

$num_records = array(100, 1000, 10000, 100000);
$index_types = array('none', 'btree');

$purgequery = "DELETE from names";
mysql_query($purgequery);

for ($h=0; $h < count($index_types); $h++) {
    for ($i=0; $i < count($num_records); $i++) {
     
        for ($j=0; $j < $num_records[$i]; $j++) {
            $data1 = rand(97, 122);
            $data2 = rand(97, 122);
            $data3 = rand(97, 122);
            $data4 = rand(97, 122);
            $data5 = rand(97, 122);
            $string = chr($data1) . chr($data2) . chr($data3) . chr($data4) . chr($data5);
            $query = "INSERT INTO names (id, data) VALUES ('$j', '$string')";
            $result = mysql_query($query);
        }
        $lowermiddle = ($num_records[$i]/2)-25;
        $uppermiddle = ($num_records[$i]/2)+25;
        if ($h == 'none') {
            $rangedquery = "SELECT SQL_NO_CACHE * FROM names WHERE id > $lowermiddle AND id < $uppermiddle;";
            $singlequery = "SELECT SQL_NO_CACHE * FROM names WHERE id = $uppermiddle;";
        }
        else {
            $indexquery = "CREATE INDEX id_idx USING btree ON names(id);";
            mysql_query($indexquery);
            $rangedquery = "SELECT SQL_NO_CACHE * FROM id_idx WHERE id > $lowermiddle AND id < $uppermiddle;";
            $singlequery = "SELECT SQL_NO_CACHE * FROM id_idx WHERE id = $uppermiddle;";
        }
        unset($start);
        unset($end);
        $start = explode(' ',microtime());
        mysql_query($rangedquery);
        $end = explode(' ',microtime());
        $exec_time = $end[1]+$end[0] - $start[1] - $start[0];
        print "Ranged Query Time for $num_records[$i] with index type $index_types[$h]: $exec_time sec\n";

        unset($start);
        unset($end);
        $start = explode(' ',microtime());
        mysql_query($singlequery);
        $end = explode(' ',microtime());
        $exec_time = $end[1]+$end[0] - $start[1] - $start[0];
        print "Single Query Time for $num_records[$i] with index type $index_types[$h]: $exec_time sec\n";
        
        if ($h == 'btree') {
            mysql_query("drop index id_idx ON names;");
        }
        mysql_query($purgequery);
    }
}
?>