Created
January 28, 2014 10:57
-
-
Save marcj/8665646 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$loader = require_once __DIR__ . '/../vendor/autoload.php'; | |
include __DIR__ . '/generated-conf/config.php'; | |
$loader->add('', __DIR__ . '/generated-classes'); | |
\Propel\Runtime\Propel::disableInstancePooling(); | |
$con = \Propel\Runtime\Propel::getConnection('bookstore'); | |
$lastTime = 0; | |
$lastMemory = 0; | |
function debugPrint($text = ''){ | |
global $lastTime, $lastMemory; | |
if (!$lastTime) $lastTime = microtime(true); | |
if (!$lastMemory) $lastMemory = memory_get_usage(); | |
echo sprintf(" %11s %11s | %6.3fms | %s\n", number_format(memory_get_usage()), number_format(memory_get_usage() - $lastMemory), (microtime(true) - $lastTime)*1000, $text); | |
$lastTime = microtime(true); | |
$lastMemory = memory_get_usage(); | |
} | |
function debugStop($text = '') { | |
debugPrint($text); | |
exit; | |
} | |
try { | |
$con->exec('DROP TABLE [book]'); | |
$con->exec('DROP TABLE [author]'); | |
} catch (PDOException $e) { | |
// do nothing - the tables probably don't exist yet | |
} | |
$con->exec('CREATE TABLE [book] | |
( | |
[id] INTEGER NOT NULL PRIMARY KEY, | |
[title] VARCHAR(255) NOT NULL, | |
[isbn] VARCHAR(24) NOT NULL, | |
[price] FLOAT, | |
[author_id] INTEGER | |
)'); | |
$con->exec('CREATE TABLE [author] | |
( | |
[id] INTEGER NOT NULL PRIMARY KEY, | |
[first_name] VARCHAR(128) NOT NULL, | |
[last_name] VARCHAR(128) NOT NULL, | |
[email] VARCHAR(128) | |
)'); | |
$time = microtime(true); | |
$memory = memory_get_usage(true); | |
//xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY); | |
//\Author::TABLE_MAP; | |
//\Map\AuthorTableMap::CLASS_NAME; | |
debugPrint('start'); | |
//1,048,576 | 0.241 | |
$authors = []; | |
for ($i = 0; $i < 1900; $i++) { | |
// $stmt = $con->prepare('INSERT INTO author ([first_name],[last_name]) VALUES (?, ?)'); | |
// $stmt->bindValue(1, 'John '.$i, PDO::PARAM_STR); | |
// $stmt->bindValue(2, 'Doe '.$i, PDO::PARAM_STR); | |
// $stmt->execute(); | |
//$query = sprintf('INSERT INTO author ([first_name],[last_name]) VALUES (\'John%s\',\'Doe%s\')', $i, $i); | |
// $query = sprintf('INSERT INTO author ([first_name],[last_name]) VALUES (%s, %s)', $con->quote('John '.$i), $con->quote('Doe '.$i)); | |
// $con->exec($query); | |
$author = new Author(); | |
$author->setFirstName('John ' . $i); | |
$author->setLastName('Doe' . $i); | |
$author->save($con); | |
$authors[] = $author->getId(); | |
} | |
debugPrint('insert author done'); | |
for ($i = 0; $i < 1700; $i++) { | |
$book = new Book(); | |
$book->setTitle('Hello' . $i); | |
$book->setAuthorId($authors[array_rand($authors)]); | |
$book->setISBN('1234'); | |
$book->setPrice($i); | |
$book->save($con); | |
$books[] = $book->getId(); | |
} | |
debugPrint('insert book done'); | |
for ($i = 0; $i < 1900; $i++) { | |
$author = AuthorQuery::create() | |
->findPk($authors[array_rand($authors)], $con); | |
} | |
debugPrint('findPk done'); | |
for ($i = 0; $i < 190; $i++) { | |
$authors2 = AuthorQuery::create() | |
->where('Author.Id > ?', $authors[array_rand($authors)]) | |
->_or() | |
->Where('(Author.FirstName || Author.LastName) = ?', 'John Doe') | |
->count($con); | |
} | |
debugPrint('complex done'); | |
for ($i = 0; $i < 710; $i++) { | |
$books = BookQuery::create() | |
//->setFormatter(new \Propel\Runtime\Formatter\OnDemandFormatter()) | |
->filterByPrice(array('min' => $i)) | |
->limit(5) | |
->find($con); | |
foreach ($books as $book) { | |
#var_dump($book->getTitle()); | |
} | |
} | |
//var_dump($books); | |
debugPrint('hydrate done'); | |
for ($i = 0; $i < 700; $i++) { | |
$books = BookQuery::create() | |
->filterByTitle('Hello' . $i) | |
->leftJoinWith('Book.Author') | |
->findOne($con); | |
} | |
debugPrint('join search done'); | |
echo sprintf(" %11s | %6.3f |\n", number_format(memory_get_usage(true) - $memory), (microtime(true) - $time)); | |
return; | |
stop(); | |
function stop() { | |
global $memory, $time; | |
echo sprintf(" %11s | %6.3f |\n", number_format(memory_get_usage(true) - $memory), (microtime(true) - $time)); | |
$xhprof_data = xhprof_disable(); | |
$XHPROF_ROOT = "/Users/marc/bude/xhprof/"; | |
include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php"; | |
include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_runs.php"; | |
$xhprof_runs = new XHProfRuns_Default(); | |
$run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_testing"); | |
echo "http://bude/xhprof/xhprof_html/index.php?run={$run_id}&source=xhprof_testing\n"; | |
exit; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment