Example #1
0
 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;
 }