Files
2026-10-04 15:25:03 +02:00

59 lines
1.8 KiB
PHP

<?php
namespace App\Filament\Imports\Helper;
use Filament\Notifications\Notification;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\HtmlString;
class ImportBenchmark
{
public static float $benchmarkStartTime;
public static int $benchmarkStartMemory;
public static int $startRowCount;
public static int $startQueries;
public static function start(string $table): void
{
self::$startRowCount = DB::connection('mongodb')->table($table)->count();
self::$benchmarkStartTime = microtime(true);
self::$benchmarkStartMemory = memory_get_usage();
DB::connection('mongodb')->enableQueryLog();
}
public static function end(string $table): void
{
$executionTime = microtime(true) - self::$benchmarkStartTime;
$memoryUsage = round((memory_get_usage() - self::$benchmarkStartMemory) / 1024 / 1024, 2);
$logs = DB::connection('mongodb')->getQueryLog();
$queriesCount = \count($logs);
// Get row count after we've stopped tracking queries
$rowDiff = DB::connection('mongodb')->table($table)->count() - self::$startRowCount;
$formattedTime = match (true) {
$executionTime >= 60 => sprintf('%dm %ds', floor($executionTime / 60), $executionTime % 60),
$executionTime >= 1 => round($executionTime, 2).'s',
default => round($executionTime * 1000).'ms',
};
$str = '⚡ TIME: '.$formattedTime;
$str .= '<br/>MEM: '.$memoryUsage.'MB';
$str .= '<br/>SQL: '.number_format($queriesCount);
$str .= '<br/>ROWS: '.number_format($rowDiff);
$recipient = Auth::user();
Notification::make()
->success()
->title('Import compleated')
->body(new HtmlString($str))
->sendToDatabase($recipient);
}
}