Guest User

Laravel fluent leak example - Leaktest.php

a guest
Jun 13th, 2013
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.81 KB | None | 0 0
  1. <?php
  2.  
  3. use Illuminate\Console\Command;
  4. use Symfony\Component\Console\Input\InputOption;
  5. use Symfony\Component\Console\Input\InputArgument;
  6.  
  7. class Leaktest extends Command {
  8.  
  9.     /**
  10.      * The console command name.
  11.      *
  12.      * @var string
  13.      */
  14.     protected $name = 'leaktest';
  15.  
  16.     /**
  17.      * The console command description.
  18.      *
  19.      * @var string
  20.      */
  21.     protected $description = 'Reproduction of possible memory leak in Fluent';
  22.  
  23.     /**
  24.      * Create a new command instance.
  25.      *
  26.      * @return void
  27.      */
  28.     public function __construct()
  29.     {
  30.         parent::__construct();
  31.     }
  32.  
  33.     /**
  34.      * Execute the console command.
  35.      *
  36.      * @return void
  37.      */
  38.     public function fire()
  39.     {
  40.  
  41.         switch ($type = $this->argument('type')) {
  42.  
  43.             case "leak_example":
  44.                 \DB::connection()->disableQueryLog();
  45.  
  46.                 for($i = 1; $i < 10; $i++) {
  47.                     for ($j = 0; $j < 10000; $j++) {
  48.                         $string = microtime();
  49.                         \DB::table('test')->insert(['somefield' => $string ]);
  50.                     }
  51.  
  52.                     echo "#executions = " . $i * 10000 . " - mem: " . memory_get_usage() . "\n";
  53.                 }
  54.  
  55.                 echo "READY\n";
  56.                 break;
  57.  
  58.             case "noleak_example":
  59.                 \DB::connection()->disableQueryLog();
  60.  
  61.                 for($i = 1; $i < 10; $i++) {
  62.                     for ($j = 0; $j < 10000; $j++) {
  63.                         $string = microtime();
  64.                         $query = "insert into `test` (`somefield`) values ('{$string}')";
  65.                         \DB::connection()->getPdo()->query($query);
  66.                     }
  67.  
  68.                     echo "#executions = " . $i * 10000 . " - mem: " . memory_get_usage() . "\n";
  69.                 }
  70.  
  71.                 echo "READY\n";
  72.                 break;
  73.  
  74.             default:
  75.                 $this->error("Invalid type [$type]");
  76.         }
  77.  
  78.         $this->info('Ready');
  79.     }
  80.  
  81.     /**
  82.      * Get the console command arguments.
  83.      *
  84.      * @return array
  85.      */
  86.     protected function getArguments()
  87.     {
  88.         return array(
  89.             array('type', InputArgument::REQUIRED, 'leak_example | noleak_example'),
  90.         );
  91.     }
  92.  
  93.  
  94. }
Advertisement
Add Comment
Please, Sign In to add comment