cvs: pear /DB_DBA/tests benchmark.php benchmark_driver.php empSchema.php graph_template hatSchema.php simple.php test_relational.php test_simple.php
test_table.php

From: Date: Fri, 21 Jun 2002 05:21:30 +0000
Subject: cvs: pear /DB_DBA/tests benchmark.php benchmark_driver.php empSchema.php graph_template hatSchema.php simple.php test_relational.php test_simple.php
test_table.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-4717@lists.php.net to get a copy of this message
busterb Fri Jun 21 01:21:30 2002 EDT Added files: /pear/DB_DBA/tests benchmark_driver.php empSchema.php hatSchema.php test_relational.php test_simple.php test_table.php Removed files: /pear/DB_DBA/tests benchmark.php graph_template simple.php Log: Updated test files. Renamed driver tests so that we have good names for the other tests too.

Index: pear/DB_DBA/tests/benchmark_driver.php +++ pear/DB_DBA/tests/benchmark_driver.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2002 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.02 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Brent Cook <busterb@mail.utexas.edu> | // +----------------------------------------------------------------------+ // // $Id: benchmark_driver.php,v 1.1 2002/06/21 05:21:30 busterb Exp $ // ini_set('include_path',ini_get('include_path').':../../'); require_once 'DB_DBA/DBA.php'; require_once 'PEAR.php'; $testDataArray = array ('11111111111111111111', '222222222222222222222222', '3333333333333333333333333333', '44444444444444444444444444444444', '555555555555555555555555555555555555', '6666666666666666666666666666666666666666', '77777777777777777777777777777777777777777777', '888888888888888888888888888888888888888888888888', '9999999999999999999999999999999999999999999999999999'); $maxDataIndex = sizeof ($testDataArray)-1; $maxTestKeys = array(1600, 3200, 6400, 12800, 25600); $transactionsInterval = 2000; $maxTransactions = $transactionsInterval * 8; $driver = 'db3'; $testDB = DBA::create($driver); function getmicrotime(){ list($usec, $sec) = explode(" ",microtime()); return ((float)$usec + (float)$sec); } foreach ($maxTestKeys as $maxTestKey) { $dat_fp = fopen("{$driver}_{$maxTestKey}.dat", 'w'); for ($transactions=$transactionsInterval; $transactions <= $maxTransactions; $transactions+=$transactionsInterval) { $result = $testDB->open('benchmark_db', 'n'); if (PEAR::isError($result)) { echo $result->getMessage()."\n"; } else { // only measure successful transactions $actualTransactions = 0; // begin stopwatch $start = getmicrotime(); for ($i=0; $i<$transactions; ++$i) { $testKey = rand (0, $maxTestKey); $testData = $testDataArray[rand(0, $maxDataIndex)]; switch (rand(0, 3)) { case 0: $result = @$testDB->insert($testKey, $testData); break; case 1: $result = @$testDB->delete($testKey); break; case 2: $result = @$testDB->replace($testKey, $testData); break; case 3: $result = @$testDB->fetch($testKey); } if ($result) { ++$actualTransactions; } } $testDB->close(); } // end stopwatch $stop = getmicrotime(); $line = $actualTransactions.' '.($stop - $start)."\n"; echo "Keys: $maxTestKey Transactions: $line"; fwrite($dat_fp, $line); } fclose($dat_fp); } // make a gnuplot data file $graph_data = <<<EOT set size 1.0, 1.0 set terminal postscript portrait "Helvetica" 12 set title "driver: $driver" set xlabel "# of transactions" set ylabel "time in seconds" set out "$driver.ps" plot EOT; foreach ($maxTestKeys as $maxTestKey) { $graph_data .= " \"{$driver}_{$maxTestKey}.dat\" using 1:2 title '". "$driver, $maxTestKey keys' with lines,\\\n"; } // write the gnuplot data file, trimming off that last comma :P $graph_fp = fopen($driver.'_graph', 'w'); fwrite($graph_fp, substr($graph_data, 0, -3)); fclose($graph_fp); ?> Index: pear/DB_DBA/tests/empSchema.php +++ pear/DB_DBA/tests/empSchema.php <?php $empSchema = array ( 'emp' => array('id' => array('type' => 'integer', 'autoincrement' => True, 'default' => 0), 'empname' => array('type' => 'varchar', 'default' => 'No Name', 'size' => 45), 'job' => array('type' => 'enum', 'domain' => array('intern', 'clerk', 'salesman', 'manager', 'analyst'), 'default' => 'intern'), 'manager' => array('type' => 'integer', 'default' => 0), 'hiredate' => array('type' => 'TIMESTAMP', 'format' => 'D M j, Y'), 'salary' => array('type' => 'integer', 'primarykey' => True), 'comm' => array('type' => 'integer'), 'deptno' => array('type' => 'integer') ), 'dept' => array('deptno' => array('type' => 'integer'), 'deptname' => array('type' => 'varchar'), 'manager' => array('type' => 'integer') ), 'location' => array('locno' => array('type' => 'integer'), 'locname' => array('type' => 'varchar') ), 'deptloc' => array('deptno' => array ('type' => 'integer'), 'locno' => array ('type' => 'integer') ), ); // some test data $emp_data = array( array ('id' => '7369', 'empname' => 'Smith', 'job' => 'clerk', 'manager' => 7782, 'hiredate' => 'May 29, 1978', 'salary' => 800, 'comm' => 0, 'deptno' => 20), array (7499, 'Allen', 'salesman', 7782, 'FEB 20, 1981', 1600, 300, 30), array (7521, 'Ward', 'salesman', 7782, 'FEB 22, 1981', 1250, 500, 30), array (7782, 'Clark', 'manager', 7788, 'JUN 9,1981', 2450, 0, 10), array (7788, 'Scott', 'analyst', NULL, 'DEC 9, 1982', 3000, 0, 20)); $dept_data = array ( array (10, 'ACCOUNTING', 7782), array (20, 'RESEARCH', 7788), array (30, 'SALES', 7369), array (40, 'OPERATIONS', 7782)); $location_data = array ( array (1, 'New York'), array (2, 'Austin'), array (3, 'Chicago')); $deptloc_data = array ( array (10, 1), array (20, 2), array (30, 3), array (40, 3), array (10, 2)); ?> Index: pear/DB_DBA/tests/hatSchema.php +++ pear/DB_DBA/tests/hatSchema.php <?php $hatTableStruct = array ( 'type' => array ('type' => 'enum', 'domain' => array ('fedora', 'stocking cap', 'top hat', 'bowler', 'beanie'), 'default' => 'fedora'), 'quantity' => array ('type' => 'integer', 'default' => 0), 'brand' => array ('type' => 'varchar', 'default' => 'No Name', 'size' => 45), 'sizes' => array ('type' => 'set', 'domain' => array ('small', 'medium', 'large', 'xlarge'), 'notnull' => 'true'), 'lastshipment' => array ('type' => 'TIMESTAMP', 'format' => 'D M j, Y'), 'hat_id' => array ('type' => 'integer', 'autoincrement' => True, 'primarykey' => True) ); $new_hats[0] = array ('type' => 'bowler', 'quantity' => 20, 'brand' => "Jill's Hats", 'sizes' => array ('small', 'large'), 'lastshipment' => 'May 29, 1978'); $new_hats[1] = array ('type' => 'stocking cap', 'sizes' => array ('xlarge', 'large'), 'brand' => "Brent's Hats", 'lastshipment' => time()); $new_hats[2] = array ('type' => 'fedora', 'quantity' => 800, 'brand' => "Shilanda's Hats", 'sizes' => array ('small', 'medium', 'large'), 'lastshipment' => 'June 30, 2001'); $new_hats[3] = array ('type' => 'top hat', 'quantity' => 10, 'brand' => "Laurie's Hats", 'sizes' => array ('small', 'medium', 'large'), 'lastshipment' => 'April 22, 2000'); $new_hats[4] = array ('type' => 'top hat', 'quantity' => 60, 'brand' => "Travis' Hats", 'sizes' => array ('small'), 'lastshipment' => 'January 1, 2000'); $new_hats[5] = array ('type' => 'beanie', 'quantity' => 600, 'brand' => "Elizabeth's Hats", 'sizes' => array ('small', 'large'), 'lastshipment' => 'May 18, 2002'); ?> Index: pear/DB_DBA/tests/test_relational.php +++ pear/DB_DBA/tests/test_relational.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2002 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.02 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Brent Cook <busterb@mail.utexas.edu> | // +----------------------------------------------------------------------+ // // $Id: test_relational.php,v 1.1 2002/06/21 05:21:30 busterb Exp $ // // test functionality of the dba table layer ini_set('include_path',ini_get('include_path').':../../'); require_once 'DB_DBA/DBA_Relational.php'; require_once 'empSchema.php'; // set the working directory and driver $db=new DBA_Relational('./', 'simple'); // generate and populate the tables foreach ($empSchema as $tableName=>$tableSchema) { $data = "{$tableName}_data"; echo "Creating table: $tableName\n"; if (!$db->createTable($tableName, $tableSchema)) { echo "Could not create the table\n"; } foreach ($$data as $row) { if (!$db->insertRow($tableName, $row)) { echo "Could not insert a row\n"; } } $results = $db->select($tableName, '*'); echo "Query: \$db->select($tableName, '*');\n"; echo $db->formatResults($db->finalizeRows($tableName, $results)); echo "------------------------------------------------\n\n"; } // closes all open tables - testing auto-open feature $db->close(); $queries = array( '$db->select("nothere", "pigs == fly")', '$db->select("emp", "salary >= 1500")', '$db->sort("emp", "empname", "a", $db->select("emp", "(job != analyst) and (job != intern)"))', '$db->sort("emp", "empname", "d", $db->select("emp", "(job != analyst) and (job != intern)"))', '$db->join("emp", "dept", "emp.deptno == dept.deptno")', '$db->join("location", $db->join("dept", "deptloc", "dept.deptno == deptloc.deptno"), "location.locno == B.locno")', '$db->sort("manager", "a", $db->join("location", $db->join("dept", "deptloc", "dept.deptno == deptloc.deptno"), "location.locno == B.locno"))' ); foreach ($queries as $query) { echo "Query: $query\n"; eval ('$results = '.$query.';'); if ($results === False) echo " Query failed.\n"; else echo $db->formatResults ($results); echo "------------------------------------------------\n\n"; } $db->close(); ?> Index: pear/DB_DBA/tests/test_simple.php +++ pear/DB_DBA/tests/test_simple.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2002 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.02 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Brent Cook <busterb@mail.utexas.edu> | // +----------------------------------------------------------------------+ // // $Id: test_simple.php,v 1.1 2002/06/21 05:21:30 busterb Exp $ // // test functionality of the simple dba layer ini_set('include_path',ini_get('include_path').':../../'); include 'PEAR.php'; include 'DB_DBA/DBA_Simple.php'; $testDataArray = array ('1', '22', '333', '4444', '55555', '6666666', '7777777', '88888888', '999999999'); $maxDataIndex = sizeof ($testDataArray)-1; $testDB = new DBA_Simple(); if (PEAR::isError($error=$testDB->open('test', 'c'))) { echo $error->getMessage()."\n"; } else { for ($i=0; $i<1000; ++$i) { $testKey = rand (0, 99); $testData = $testDataArray[rand(0, 8)]; switch (rand(0, 3)) { case 0: if (!$testDB->exists($testKey)) { $result = $testDB->insert($testKey, $testData); } break; case 1: if ($testDB->exists($testKey)) { $result = $testDB->delete($testKey); } break; case 2: $result = $testDB->replace($testKey, $testData); break; case 3: if ($testDB->exists($testKey)) { $result = $testDB->fetch($testKey); } } if (PEAR::isError($result)) { echo $result->getMessage()."\n"; } } $testDB->close(); } $testDB->open('test', 'r'); $key = $testDB->firstkey(); while ($key !== FALSE) { echo "$key = ".$testDB->fetch($key)."\n"; $key = $testDB->nextkey($key); } $testDB->close(); ?> Index: pear/DB_DBA/tests/test_table.php +++ pear/DB_DBA/tests/test_table.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2002 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.02 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Brent Cook <busterb@mail.utexas.edu> | // +----------------------------------------------------------------------+ // // $Id: test_table.php,v 1.1 2002/06/21 05:21:30 busterb Exp $ // // test functionality of the dba table layer ini_set('include_path',ini_get('include_path').':../../'); require_once 'DB_DBA/DBA_Table.php'; require_once 'hatSchema.php'; function printQueryResults($results, $fields=null) { if (is_array($results) && sizeof($results)) { if (is_null($fields)) $fields = array_keys(current($results)); // get the maximum length of each field foreach ($fields as $key=>$field) { $longest[$key] = strlen($field); foreach ($results as $result) { $resultLen = strlen($result[$field]); if ($resultLen > $longest[$key]) $longest[$key] = $resultLen; } } // print fields foreach ($fields as $key=>$field) echo str_pad($field, $longest[$key]).' '; echo "\n"; // print dividers foreach ($longest as $length) echo str_repeat('-',$length).' '; echo "\n"; // print rows foreach ($results as $result) { foreach ($fields as $key=>$field) echo str_pad($result[$field], $longest[$key]).' '; echo "\n"; } } else { echo "\tEmpty!\n"; } echo "\n"; echo "------------------------------------------\n"; } $table=new DBA_Table(); $table->create ('hats', $hatTableStruct); $table->open ('hats', 'w'); for ($i=0; $i < 2; ++$i) { foreach ($new_hats as $hat) { $table->insertRow($hat); } } $table->close(); $table->open ('hats', 'r'); $query = '(type != bowler) and (type != fedora)'; $results = $table->select($query); echo "Query: $query\n\n"; printQueryResults ($results, array('brand', 'quantity')); $query = 'quantity <= 50'; $results = $table->select($query); echo "Query: $query\n\n"; printQueryResults ($results, array('brand', 'quantity')); $query = 'quantity >= 50'; $results = $table->select($query); echo "Query: $query\n\n"; printQueryResults ($results, array('brand', 'quantity')); $sortFields = 'quantity, hat_id'; $results = $table->sort($sortFields); echo "Sorting by quantity, hat_id, ascending order\n"; printQueryResults ($results, array('brand', 'quantity', 'hat_id')); $sortField = 'quantity'; $results = array_reverse ($table->sort($sortField)); echo "Sorting by $sortField, descending order\n"; printQueryResults ($results, array('brand', 'quantity')); $sortField = 'quantity'; $results = $table->unique( $table->project('brand, quantity', $table->sort('quantity', $table->select($query)))); echo "Sorting by: $sortField, descending order\n"; echo "Query: $query\n\n"; printQueryResults ($results, array('brand', 'quantity')); $table->close(); ?>
« previous php.pear.cvs (#4717) next »