<?php
    include_once ('config.php');
    include_once ('lib/ddllib.php');

/// Define table test_rs to be created
    $table = new XMLDBTable('test_rs');

/// Launch drop table for test_rs
    drop_table($table, false, false);

/// Adding fields to table test_rs
    $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
    $table->addFieldInfo('name', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
    $table->addFieldInfo('value', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null, null, null);
    $table->addFieldInfo('anothervalue', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null, null, null);

/// Adding keys to table test_rs
    $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id'));

/// Launch create table for test_rs
    create_table($table, false, false);

/// We can need a lot of time
    set_time_limit(0);

/// Let's try with this number of records
    $num_recs = 50000;

/// Insert $num_recs records
    for ($n = 1; $n<=$num_recs; $n++) {
        $rec->name=$n;
        $rec->value="$n";
        $rec->anothervalue='Did you know that Moodle is a great piece of software? Tests like this are performed in order to achive maximum performance handling long resultsets. What conbination will be the quickest?';
        insert_record('test_rs', $rec);
    }

/// Conunting records
    $micro = microtime(true);
    $rs = get_recordset('test_rs');
    $countrecs = $rs->RecordCount();
    $rs->close();
    echo '<h1>'. $countrecs . ' records recordset under ' . $CFG->dbtype . '</h1>';
    echo 'RecordCount secs: ' . (microtime(true) - $micro) . '<br>';

/// Get recordset
    echo 'With FetchNextObj (auto Next)';
    $micro = microtime(true);
    $rs = get_recordset('test_rs');
    while ($o = $rs->FetchNextObj()) {
    }
    $rs->close();
    echo ' secs: ' . (microtime(true) - $micro) . '<br>';

/// Get recordset
    echo 'With FetchNextObj using eof (auto Next)';
    $micro = microtime(true);
    $rs = get_recordset('test_rs');
    while (!$rs->EOF) {
        $o = $rs->FetchNextObj();
    }
    $rs->close();
    echo ' secs: ' . (microtime(true) - $micro) . '<br>';

/// Get recordset
    echo 'With FetchRow (auto Next)';
    $micro = microtime(true);
    $rs = get_recordset('test_rs');
    while (!$rs->EOF) {
        $o = (object)$rs->FetchRow();
    }
    $rs->close();
    echo ' secs: ' . (microtime(true) - $micro) . '<br>';

/// Get recordset
    echo 'With FetchObj & Next';
    $micro = microtime(true);
    $rs = get_recordset('test_rs');
    while (!$rs->EOF) {
        $o = $rs->FetchObj();
        $rs->MoveNext();
    }
    $rs->close();
    echo ' secs: ' . (microtime(true) - $micro) . '<br>';

/// Get recordset
    echo 'With Fields() & Next';
    $micro = microtime(true);
    $rs = get_recordset('test_rs');
    while (!$rs->EOF) {
        $o1 = $rs->Fields('value');
        $o2= $rs->Fields('value');
        $o3= $rs->Fields('value');
        $rs->MoveNext();
    }
    $rs->close();
    echo ' secs: ' . (microtime(true) - $micro) . '<br>';

/// Get recordset
    echo 'With fields[] & Next';
    $micro = microtime(true);
    $rs = get_recordset('test_rs');
    while (!$rs->EOF) {
        $o = (object)$rs->fields;
        $rs->MoveNext();
    }
    $rs->close();
    echo ' secs: ' . (microtime(true) - $micro) . '<br>';
?>

