1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
| $patterns = [ 'product.xx_' ];
function format($bytes): string { if ($bytes < 1024) { return "{$bytes} bytes"; }
if ($bytes < 1024 * 1024) { $bytes = round($bytes / 1024, 2);
return "{$bytes} Kb"; }
if ($bytes < 1024 * 1024 * 1024) { $bytes = round($bytes / 1024 / 1024, 2);
return "{$bytes} Mb"; }
$bytes = round($bytes / 1024 / 1024 / 1024, 2);
return "{$bytes} Gb"; }
class CollectionStats { private $stats;
public function __construct(array $stats) { $this->stats = $stats; }
public function toArray(): array { return [ 'collection' => $this->stats['ns'], 'size' => format($this->stats['size']), 'count' => $this->stats['count'], 'avgObjSize' => format($this->stats['avgObjSize'] ?? 0), 'storageSize' => format($this->stats['storageSize']), 'totalIndexSize' => format($this->stats['totalIndexSize']),
'raw' => [ 'count' => $this->stats['count'], 'size' => $this->stats['size'], 'avgObjSize' => $this->stats['avgObjSize'] ?? 0, 'storageSize' => $this->stats['storageSize'], 'totalIndexSize' => $this->stats['totalIndexSize'], 'bytes_in_cache' => $this->stats['wiredTiger']['cache']['bytes currently in the cache'], ], ]; } }
foreach ($patterns as $pattern) { [$connectionName, $collectionPrefix] = explode('.', $pattern);
dump(compact('connectionName', 'collectionPrefix'));
$collections = get_collection_names($connectionName); $collections = collect($collections) ->filter(function ($collection) use ($collectionPrefix) { return Str::startsWith($collection, $collectionPrefix); }) ->values() ->toArray();
$result = [];
foreach ($collections as $collection) { $cursor = DB::connection($connectionName)->getMongoDB()->command(['collStats' => $collection]); $res = json_decode(json_encode($cursor->toArray()[0]), true); $reply = json_encode($res, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
$result[] = (new CollectionStats($res))->toArray(); }
$results = collect($result);
dump('collection_count: ' . $results->count()); dump('total_document_count: ' . $results->sum('raw.count')); dump('total_size: ' . format($results->sum('raw.size'))); dump('avg_obj_size: ' . format($results->avg('raw.avgObjSize'))); dump('total_storage_size: ' . format($results->sum('raw.storageSize'))); dump('total_index_size: ' . format($results->sum('raw.totalIndexSize'))); dump('total_bytes_in_cache: ' . format($results->sum('raw.bytes_in_cache'))); echo PHP_EOL; }
|