public function benchmark($mainCategoryNum, $subCategoryNum, $productsNum) { $totalIterations = $mainCategoryNum * $subCategoryNum * $productsNum; $this->setupDummyData($mainCategoryNum, $subCategoryNum, $productsNum); Product::enableLazyLoading(); Category::enableLazyLoading(); $time = microtime(true); $products = Product::find(); /** @var Product $product */ foreach ($products as $product) { $subCategory = $product->getCategory(); if ($subCategory) { $mainCategory = $subCategory->getCategory(); } $productName = $product->getName(); } echo $totalIterations . ' iterations with Lazy Loading enabled: ' . (microtime(true) - $time) . PHP_EOL; Product::disableLazyLoading(); Category::disableLazyLoading(); $time = microtime(true); $products = Product::find(); /** @var Product $product */ foreach ($products as $product) { $product->map(); $subCategory = $product->getCategory(); if ($subCategory) { $subCategory->map(); $mainCategory = $subCategory->getCategory(); } $productName = $product->getName(); } echo $totalIterations . ' iterations with Lazy Loading disabled: ' . (microtime(true) - $time) . PHP_EOL; }
public function testShouldReturnLazyLoadingCursor() { $parentCategory = new Category(); $parentCategory->setName('Parent category'); $parentCategory->setDesc('Parent category'); $parentCategory->save(); for ($i = 0; $i < 10; $i++) { $category = new Category(); $category->setName('Category ' . $i); $category->setDesc('Category ' . $i . ' desc'); $category->setCategory($parentCategory); $category->save(); } Category::enableLazyLoading(); $categories = Category::find([['category' => ['$ne' => null]]]); $this->assertInstanceOf('\\Vegas\\Odm\\Collection\\LazyLoadingCursor', $categories); foreach ($categories as $category) { $this->assertInstanceOf('\\Fixtures\\Collection\\Category', $category); $this->assertInstanceOf('\\Fixtures\\Collection\\Category', $category->getCategory()); } $categories = Category::find([['category' => ['$ne' => null]]]); $this->assertInstanceOf('\\Vegas\\Odm\\Collection\\LazyLoadingCursor', $categories); foreach ($categories as $category) { $this->assertInstanceOf('\\Fixtures\\Collection\\Category', $category); $reflectionClass = new \ReflectionClass(get_class($category)); $categoryProperty = $reflectionClass->getProperty('category'); $categoryProperty->setAccessible(true); $this->assertTrue(\MongoDBRef::isRef($categoryProperty->getValue($category))); } }