Skip to content

Commit

Permalink
Fix conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
christophrumpel committed Feb 18, 2025
2 parents 2d13464 + f9be901 commit 69d0b7a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
34 changes: 31 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,53 @@
![CleanShot 2025-02-08 at 17 56 49 3@2x](https://github.com/user-attachments/assets/6d0cac81-7e3f-4443-9ad3-e6b04e16b8e7)

(Part of the video [Import One Million Rows To The Database (PHP/Laravel)](https://youtu.be/CAi4WEKOT4A))

# Artisan Benchmark

## Installation

```shell
composer require christophrumpel/artisan-benchmark
```

## Usage

This package lets you `benchmark` your artisan commands:

```shell
php artisan benchmark your:command
```

Just replaces `your:command` with the signature of your command. Your command will be run; afterward, you will see the benchmark output.
Simply replace `⁠your:command` with your command signature. After execution, you'll see detailed benchmark results.


![CleanShot 2025-02-08 at 17 56 49@2x](https://github.com/user-attachments/assets/d5a6e86d-1cc4-4786-b246-3c8939aec053)


You can also provide no signature, then the benchmark command will show you a list of commands to pick from:
If you run the command without a signature, it will display a list of available commands to choose from:


```shell
php artisan benchmark
```

## Good To Know
![CleanShot 2025-02-14 at 13 21 14@2x](https://github.com/user-attachments/assets/a490b8ec-7859-4966-9fbf-f1e3c66d55d2)


## Table Count Monitoring

You can monitor changes in a specific database table's record count by using the `⁠--tableToWatch` option:
```php
php artisan benchmark your:command --tableToWatch=users
```

Be aware that it only shows the count difference from before running your command.

![CleanShot 2025-02-14 at 13 34 31@2x](https://github.com/user-attachments/assets/ce0ec54a-b99b-49d6-99cd-7b4f062097cc)



## Technical Details

### Query Count
The package uses a listener on the Laravel database event `Illuminate\Database\Events\QueryExecuted` to track the number of database queries.
Expand Down
9 changes: 6 additions & 3 deletions src/BenchmarksArtisanCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace ChristophRumpel\ArtisanBenchmark;

use Illuminate\Database\Events\QueryExecuted;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Number;
use Illuminate\Support\Str;

Expand All @@ -16,6 +18,8 @@ trait BenchmarksArtisanCommand

protected int $benchmarkStartMemory;

protected int $queryCount = 0;

protected ?int $tableToWatchBeginCount;

public function handle(): void
Expand All @@ -41,16 +45,15 @@ protected function startBenchmark(): void
$this->tableToWatchBeginCount = DB::table($tableToWatch)->count();
}

DB::enableQueryLog();
DB::flushQueryLog();
Event::listen(QueryExecuted::class, fn () => $this->queryCount++);
}

protected function endBenchmark(): void
{
$metrics = collect([
'time' => $this->formatExecutionTime(microtime(true) - $this->benchmarkStartTime),
'memory' => round((memory_get_usage() - $this->benchmarkStartMemory) / 1024 / 1024, 2).'MB',
'queries' => count(DB::getQueryLog()),
'queries' => $this->queryCount,
]);

if ($tableToWatch = $this->option('tableToWatch')) {
Expand Down

0 comments on commit 69d0b7a

Please sign in to comment.