├── README.md ├── composer.json └── src ├── Events └── SeedingFinished.php └── Seeder.php /README.md: -------------------------------------------------------------------------------- 1 | # Laravel Seeder Debugger 2 | 3 | Get simple debug info after finished seeding, like this: 4 | ``` 5 | Database seeding completed successfully. 6 | Seeding execution time: 8.6s. 7 | Database queries executed: 329. 8 | Current RAM usage is 18.7MB with peak during execution 59.1MB. 9 | ``` 10 | Thanks to this info you can try to write more efficient seeders :) 11 | 12 | Do you need more info in debug? Feedback and pull requests are welcome. 13 | 14 | NOTE: This library is part of my [laravel-seeder-extended](https://github.com/chojnicki/laravel-seeder-extended) 15 | that beside of just debugging is adding extra methods. You can use this library alone or laravel-seeder-extender depends of your needs. 16 | 17 | ## Requirements 18 | 19 | - Laravel / Lumen 5.5 or higher (written on 5.8, not tested on lower than 5.5 but should work on 5.*) 20 | 21 | 22 | ## Instalation with Composer 23 | 24 | ``` 25 | composer require chojnicki/laravel-seeder-debugger 26 | ``` 27 | 28 | 29 | 30 | ## Usage 31 | 32 | In DatabaseSeeder.php simply replace: 33 | ``` 34 | use Illuminate\Database\Seeder; 35 | ``` 36 | with: 37 | ``` 38 | use Chojnicki\LaravelSeederDebugger\Seeder; 39 | ``` 40 | 41 | ## Events 42 | 43 | If you want to debug seeding outside console (for ex. use Log) then there is event SeedingFinished that you can listen to. 44 | 45 | EventServiceProvider.php: 46 | ``` 47 | use Chojnicki\LaravelSeederDebugger\Events\SeedingFinished; 48 | ``` 49 | 50 | In you listener, $event->debug will return array with all info used in console, be keys: execution_time, queries_count, ram_usage, ram_usage_peak. 51 | 52 | ## Note 53 | This debugger simply extends original Seeder library (is not a fork) so all functionality is preserved and there should not be conflicts with already written seeders. 54 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "chojnicki/laravel-seeder-debugger", 3 | "description": "Show debug info after finishing seeding like execution time or queries count, that can help you write better seeders.", 4 | "type": "library", 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "chojnicki", 9 | "email": "lukchojnicki@gmail.com" 10 | } 11 | ], 12 | "require": { 13 | }, 14 | "autoload": { 15 | "psr-4": { 16 | "Chojnicki\\LaravelSeederDebugger\\": "src/" 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/Events/SeedingFinished.php: -------------------------------------------------------------------------------- 1 | debug = $debug; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/Seeder.php: -------------------------------------------------------------------------------- 1 | debug) { 21 | $this->startExecutionTime = microtime(true); 22 | 23 | DB::listen(function () { 24 | $this->queriesCount++; 25 | }); 26 | } 27 | } 28 | 29 | 30 | /** 31 | * Show debug info after finish 32 | */ 33 | public function __destruct() 34 | { 35 | $seederName = (new \ReflectionClass($this))->getShortName(); 36 | if ($seederName != 'DatabaseSeeder') return; // print debug only on main seeder 37 | 38 | if (! $this->debug) return; 39 | 40 | /* Debug execution time */ 41 | $executionTime = microtime(true) - $this->startExecutionTime; 42 | $this->command->info('Seeding execution time: ' . round($executionTime, 2) . 's.'); 43 | 44 | /* Debug queries */ 45 | $this->command->info('Database queries executed: ' . $this->queriesCount . '.'); 46 | 47 | /* Debug RAM usage */ 48 | $RAMUsage = memory_get_usage(); 49 | $RAMUsage = round($RAMUsage / 1024 / 1024, 2); // to MB 50 | $RAMUsagePeak = memory_get_peak_usage(); 51 | $RAMUsagePeak = round($RAMUsagePeak / 1024 / 1024, 2); // to MB 52 | 53 | /* Print debug in console */ 54 | $this->command->info('Current RAM usage is ' . $RAMUsage . 'MB with peak during execution ' . $RAMUsagePeak . 'MB.'); 55 | 56 | /* Fire event with debug */ 57 | event(new SeedingFinished([ 58 | 'execution_time' => $executionTime, 59 | 'queries_count' => $this->queriesCount, 60 | 'ram_usage' => $RAMUsage, 61 | 'ram_usage_peak' => $RAMUsage, 62 | ])); 63 | } 64 | 65 | } 66 | --------------------------------------------------------------------------------