speed.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. <?php
  2. /**
  3. * This file is part of the Propel package.
  4. * For the full copyright and license information, please view the LICENSE
  5. * file that was distributed with this source code.
  6. *
  7. * @license MIT License
  8. */
  9. $conf_path = realpath(dirname(__FILE__) . '/fixtures/bookstore/build/conf/bookstore-conf.php');
  10. if (!file_exists($conf_path)) {
  11. throw new Exception('Bookstore project must be built');
  12. }
  13. // Add build/classes/ and classes/ to path
  14. set_include_path(
  15. realpath(dirname(__FILE__) . '/fixtures/bookstore/build/classes') . PATH_SEPARATOR .
  16. dirname(__FILE__) . '/../runtime/lib' . PATH_SEPARATOR .
  17. get_include_path()
  18. );
  19. require_once 'Propel.php';
  20. $conf = include $conf_path;
  21. $conf['log'] = null;
  22. Propel::setConfiguration($conf);
  23. Propel::initialize();
  24. include_once 'tools/helpers/bookstore/validator/ISBNValidator.php';
  25. class PropelSpeedTest
  26. {
  27. public $iterations;
  28. public function __construct($iterations = 100)
  29. {
  30. $this->iterations = $iterations;
  31. }
  32. public function run()
  33. {
  34. $timers = array();
  35. fwrite(STDOUT, "Running scenario");
  36. // perform tests
  37. for ($i=0; $i < $this->iterations; $i++) {
  38. fwrite(STDOUT, '.');
  39. $this->setUp();
  40. $t = microtime(true);
  41. $this->testSpeed();
  42. $timers[]= microtime(true) - $t;
  43. $this->tearDown();
  44. }
  45. fwrite(STDOUT, " done\n");
  46. // sort tests
  47. sort($timers);
  48. // eliminate first and last
  49. array_shift($timers);
  50. array_pop($timers);
  51. return array_sum($timers) / count($timers);
  52. }
  53. protected function emptyTables()
  54. {
  55. $res1 = AuthorPeer::doDeleteAll();
  56. $res2 = PublisherPeer::doDeleteAll();
  57. $res3 = AuthorPeer::doDeleteAll();
  58. $res4 = ReviewPeer::doDeleteAll();
  59. $res5 = MediaPeer::doDeleteAll();
  60. $res6 = BookClubListPeer::doDeleteAll();
  61. $res7 = BookListRelPeer::doDeleteAll();
  62. }
  63. public function setUp()
  64. {
  65. $this->con = Propel::getConnection(BookPeer::DATABASE_NAME);
  66. $this->con->beginTransaction();
  67. $this->emptyTables();
  68. }
  69. public function tearDown()
  70. {
  71. $this->emptyTables();
  72. $this->con->commit();
  73. }
  74. public function testSpeed()
  75. {
  76. // Add publisher records
  77. // ---------------------
  78. $scholastic = new Publisher();
  79. $scholastic->setName("Scholastic");
  80. // do not save, will do later to test cascade
  81. $morrow = new Publisher();
  82. $morrow->setName("William Morrow");
  83. $morrow->save();
  84. $morrow_id = $morrow->getId();
  85. $penguin = new Publisher();
  86. $penguin->setName("Penguin");
  87. $penguin->save();
  88. $penguin_id = $penguin->getId();
  89. $vintage = new Publisher();
  90. $vintage->setName("Vintage");
  91. $vintage->save();
  92. $vintage_id = $vintage->getId();
  93. // Add author records
  94. // ------------------
  95. $rowling = new Author();
  96. $rowling->setFirstName("J.K.");
  97. $rowling->setLastName("Rowling");
  98. // no save()
  99. $stephenson = new Author();
  100. $stephenson->setFirstName("Neal");
  101. $stephenson->setLastName("Stephenson");
  102. $stephenson->save();
  103. $stephenson_id = $stephenson->getId();
  104. $byron = new Author();
  105. $byron->setFirstName("George");
  106. $byron->setLastName("Byron");
  107. $byron->save();
  108. $byron_id = $byron->getId();
  109. $grass = new Author();
  110. $grass->setFirstName("Gunter");
  111. $grass->setLastName("Grass");
  112. $grass->save();
  113. $grass_id = $grass->getId();
  114. // Add book records
  115. // ----------------
  116. $phoenix = new Book();
  117. $phoenix->setTitle("Harry Potter and the Order of the Phoenix");
  118. $phoenix->setISBN("043935806X");
  119. // cascading save (Harry Potter)
  120. $phoenix->setAuthor($rowling);
  121. $phoenix->setPublisher($scholastic);
  122. $phoenix->save();
  123. $phoenix_id = $phoenix->getId();
  124. $qs = new Book();
  125. $qs->setISBN("0380977427");
  126. $qs->setTitle("Quicksilver");
  127. $qs->setAuthor($stephenson);
  128. $qs->setPublisher($morrow);
  129. $qs->save();
  130. $qs_id = $qs->getId();
  131. $dj = new Book();
  132. $dj->setISBN("0140422161");
  133. $dj->setTitle("Don Juan");
  134. $dj->setAuthor($byron);
  135. $dj->setPublisher($penguin);
  136. $dj->save();
  137. $dj_id = $qs->getId();
  138. $td = new Book();
  139. $td->setISBN("067972575X");
  140. $td->setTitle("The Tin Drum");
  141. $td->setAuthor($grass);
  142. $td->setPublisher($vintage);
  143. $td->save();
  144. $td_id = $td->getId();
  145. // Add review records
  146. // ------------------
  147. $r1 = new Review();
  148. $r1->setBook($phoenix);
  149. $r1->setReviewedBy("Washington Post");
  150. $r1->setRecommended(true);
  151. $r1->setReviewDate(time());
  152. $r1->save();
  153. $r1_id = $r1->getId();
  154. $r2 = new Review();
  155. $r2->setBook($phoenix);
  156. $r2->setReviewedBy("New York Times");
  157. $r2->setRecommended(false);
  158. $r2->setReviewDate(time());
  159. $r2->save();
  160. $r2_id = $r2->getId();
  161. // Perform a "complex" search
  162. // --------------------------
  163. $results = BookQuery::create()
  164. ->filterByTitle('Harry%')
  165. ->find();
  166. $results = BookQuery::create()
  167. ->where('Book.ISBN IN ?', array("0380977427", "0140422161"))
  168. ->find();
  169. // Perform a "limit" search
  170. // ------------------------
  171. $results = BookQuery::create()
  172. ->limit(2)
  173. ->offset(1)
  174. ->orderByTitle()
  175. ->find();
  176. // Perform a lookup & update!
  177. // --------------------------
  178. $qs_lookup = BookQuery::create()->findPk($qs_id);
  179. $new_title = "Quicksilver (".crc32(uniqid(rand())).")";
  180. $qs_lookup->setTitle($new_title);
  181. $qs_lookup->save();
  182. $qs_lookup2 = BookQuery::create()->findPk($qs_id);
  183. // Test some basic DATE / TIME stuff
  184. // ---------------------------------
  185. // that's the control timestamp.
  186. $control = strtotime('2004-02-29 00:00:00');
  187. // should be two in the db
  188. $r = ReviewQuery::create()->findOne();
  189. $r_id = $r->getId();
  190. $r->setReviewDate($control);
  191. $r->save();
  192. $r2 = ReviewQuery::create()->findPk($r_id);
  193. // Testing the DATE/TIME columns
  194. // -----------------------------
  195. // that's the control timestamp.
  196. $control = strtotime('2004-02-29 00:00:00');
  197. // should be two in the db
  198. $r = ReviewQuery::create()->findOne();
  199. $r_id = $r->getId();
  200. $r->setReviewDate($control);
  201. $r->save();
  202. $r2 = ReviewQuery::create()->findPk($r_id);
  203. // Testing the column validators
  204. // -----------------------------
  205. $bk1 = new Book();
  206. $bk1->setTitle("12345"); // min length is 10
  207. $ret = $bk1->validate();
  208. // Unique validator
  209. $bk2 = new Book();
  210. $bk2->setTitle("Don Juan");
  211. $ret = $bk2->validate();
  212. // Now trying some more complex validation.
  213. $auth1 = new Author();
  214. $auth1->setFirstName("Hans");
  215. // last name required; will fail
  216. $bk1->setAuthor($auth1);
  217. $rev1 = new Review();
  218. $rev1->setReviewDate("08/09/2001");
  219. // will fail: reviewed_by column required
  220. $bk1->addReview($rev1);
  221. $ret2 = $bk1->validate();
  222. $bk2 = new Book();
  223. $bk2->setTitle("12345678901"); // passes
  224. $auth2 = new Author();
  225. $auth2->setLastName("Blah"); //passes
  226. $auth2->setEmail("some@body.com"); //passes
  227. $auth2->setAge(50); //passes
  228. $bk2->setAuthor($auth2);
  229. $rev2 = new Review();
  230. $rev2->setReviewedBy("Me!"); // passes
  231. $rev2->setStatus("new"); // passes
  232. $bk2->addReview($rev2);
  233. $ret3 = $bk2->validate();
  234. // Testing doCount() functionality
  235. // -------------------------------
  236. $count = BookQuery::create()->count();
  237. // Testing many-to-many relationships
  238. // ----------------------------------
  239. // init book club list 1 with 2 books
  240. $blc1 = new BookClubList();
  241. $blc1->setGroupLeader("Crazyleggs");
  242. $blc1->setTheme("Happiness");
  243. $brel1 = new BookListRel();
  244. $brel1->setBook($phoenix);
  245. $brel2 = new BookListRel();
  246. $brel2->setBook($dj);
  247. $blc1->addBookListRel($brel1);
  248. $blc1->addBookListRel($brel2);
  249. $blc1->save();
  250. // init book club list 2 with 1 book
  251. $blc2 = new BookClubList();
  252. $blc2->setGroupLeader("John Foo");
  253. $blc2->setTheme("Default");
  254. $brel3 = new BookListRel();
  255. $brel3->setBook($phoenix);
  256. $blc2->addBookListRel($brel3);
  257. $blc2->save();
  258. // re-fetch books and lists from db to be sure that nothing is cached
  259. $phoenix = BookQuery::create()
  260. ->filterById($phoenix->getId())
  261. ->findOne();
  262. $blc1 = BookClubListQuery::create()
  263. ->filterById($blc1->getId())
  264. ->findOne();
  265. $blc2 = BookClubListQuery::create()
  266. ->filterbyId($blc2->getId())
  267. ->findOne();
  268. $relCount = $phoenix->countBookListRels();
  269. $relCount = $blc1->countBookListRels();
  270. $relCount = $blc2->countBookListRels();
  271. // Removing books that were just created
  272. // -------------------------------------
  273. $hp = BookQuery::create()->findPk($phoenix_id);
  274. $c = new Criteria();
  275. $c->add(BookPeer::ID, $hp->getId());
  276. // The only way for cascading to work currently
  277. // is to specify the author_id and publisher_id (i.e. the fkeys
  278. // have to be in the criteria).
  279. $c->add(AuthorPeer::ID, $hp->getId());
  280. $c->add(PublisherPeer::ID, $hp->getId());
  281. $c->setSingleRecord(true);
  282. BookPeer::doDelete($c);
  283. // Attempting to delete books by complex criteria
  284. BookQuery::create()
  285. ->filterByISBN("043935806X")
  286. ->orWhere('Book.ISBN = ?', "0380977427")
  287. ->orWhere('Book.ISBN = ?', "0140422161")
  288. ->delete();
  289. $td->delete();
  290. AuthorQuery::create()->filterById($stephenson_id)->delete();
  291. AuthorQuery::create()->filterById($byron_id)->delete();
  292. $grass->delete();
  293. PublisherQuery::create()->filterById($morrow_id)->delete();
  294. PublisherQuery::create()->filterById($penguin_id)->delete();
  295. $vintage->delete();
  296. // These have to be deleted manually also since we have onDelete
  297. // set to SETNULL in the foreign keys in book. Is this correct?
  298. $rowling->delete();
  299. $scholastic->delete();
  300. $blc1->delete();
  301. $blc2->delete();
  302. }
  303. }
  304. $test = new PropelSpeedTest(100);
  305. echo "Test speed: {$test->run()} ({$test->iterations} iterations)\n";