feat(core,board,ecommerce,page): 성능 계측 인프라 4축 확장 및 계측 대상 선언 지점 신설
계측 축이 "목록 SELECT" 하나였고 프로파일 12종이 커맨드 파일에 하드코딩돼 있어 확장이 자기 대상을 추가할 수 없었다. 확장이 설치·제거되는 설치본마다 "실제로 존재하는 대상"이 달라지므로, 대상을 소유자가 선언하고 코어가 수집하도록 뒤집었다. - 선언 지점 두 곳, 스키마 하나: 코어 config/benchmark.php, 확장 getBenchmarkProfiles (모듈/플러그인). 수집은 method_exists 기반이라 인터페이스를 직접 구현한 서드파티 확장이 깨지지 않는다. - 축 4종(목록/화면/쓰기/배치)을 실행기로 분리하고 컨테이너 태그로 주입 — 축이 늘 때 커맨드를 고치지 않는다. - 화면 축은 HTTP 커널로 내부 요청을 처리한다. 라우트만 dispatch 하면 전역 미들웨어를 건너뛰어 화면과 다른 것을 재게 된다. 인증은 프로파일이 선언한 권한만 가진 임시 계정이 기본이며 --as 로 기존 계정을 지목할 수 있다. - 화면/쓰기 축은 계측 계정 생성부터 처리까지 롤백되는 트랜잭션 안에서 실행해 운영 DB 에도 잔여 데이터를 남기지 않는다. 배치 축은 내부 커밋·락 보유 때문에 감싸지 않고 --allow-write 로만 통제한다. - 잘못된 선언은 조용히 버리지 않고 사유를 경고로 남긴다. 버려진 선언이 곧 계측 사각이 되기 때문이다. 전부 건너뛴 실행은 종료 코드 1 로 끝난다. - 리포트는 환경 정보를 함께 적어 저장소 밖(storage/app/benchmarks)에 남긴다. 측정값이 실행 머신에 종속되므로 저장소에 축적하면 비교 불가능한 수치가 섞인다. 작업 중 발견해 함께 고친 것: - 이관 프로파일 3종의 soft_delete 선언이 실제 스키마와 어긋나 있었다 (users/product_inquiries/pages 에 deleted_at 없음). - 주문 목록이 상태 미지정 시 임시 주문 상태를 NOT IN 으로 제외하는데 선언에 빠져 있었다. filters 가 등가 비교만 지원한 것이 근본 원인이라 연산자 형태를 받도록 넓히고, 미허용 연산자는 측정 전에 거부한다. - 댓글 프로파일은 지배적 술어가 상관 서브쿼리라 선언형으로 재현할 수 없어 제거했다. 맨 테이블 스캔은 어느 화면도 내지 않는 수치가 된다. g7:bench:pagination → g7:bench 로 개명(4축을 재므로). 이전 이름은 별칭으로 남겼다. 테스트 61건 통과, 라이브 18종 전건 측정.
This commit is contained in:
1 parent
be9befe023
commit
5f4a733b3b
40 files changed
+5266
-567
No files matched your search
@@ -0,0 +1,141 @@
|
||||
<?php
|
||||
|
||||
namespace App\Benchmark\Axes;
|
||||
|
||||
use App\Benchmark\Contracts\BenchmarkAxisRunner;
|
||||
use App\Benchmark\DTO\BenchmarkProfile;
|
||||
use App\Benchmark\DTO\BenchmarkResult;
|
||||
use App\Benchmark\DTO\BenchmarkRunOptions;
|
||||
use App\Benchmark\QueryCollector;
|
||||
use App\Enums\BenchmarkAxis;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Symfony\Component\Console\Output\BufferedOutput;
|
||||
|
||||
/**
|
||||
* 배치 축 실행기 — 배치 커맨드 소요 시간 + 피크 메모리
|
||||
*
|
||||
* 배치는 한 번에 대량을 처리하므로 "느려짐"이 시간보다 메모리로 먼저 드러납니다(청크
|
||||
* 없이 전건 로딩 → OOM). 그래서 시간과 함께 피크 메모리를 함께 잽니다.
|
||||
*
|
||||
* 다른 축과 달리 트랜잭션으로 감싸지 않습니다 — 배치 커맨드는 내부에서 커밋하거나 DDL 을
|
||||
* 실행할 수 있고, 대량 처리를 긴 트랜잭션에 담으면 락 보유 시간이 계측 자체보다 위험해집니다.
|
||||
* 대신 데이터를 변경하는 배치는 `--allow-write` 없이는 실행하지 않습니다.
|
||||
*/
|
||||
class BatchAxisRunner implements BenchmarkAxisRunner
|
||||
{
|
||||
public function __construct(private readonly QueryCollector $collector) {}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function axis(): BenchmarkAxis
|
||||
{
|
||||
return BenchmarkAxis::Batch;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function run(BenchmarkProfile $profile, BenchmarkRunOptions $options, ?\Closure $onProgress = null): BenchmarkResult
|
||||
{
|
||||
$notify = $onProgress ?? static fn (string $message) => null;
|
||||
|
||||
if ($profile->mutates() && ! $options->allowWrite) {
|
||||
return BenchmarkResult::skipped($profile, '데이터를 변경하는 배치입니다. --allow-write 를 붙여 실행하세요.');
|
||||
}
|
||||
|
||||
$command = (string) $profile->option('command');
|
||||
$arguments = (array) $profile->option('arguments', []);
|
||||
|
||||
if (! array_key_exists($command, Artisan::all())) {
|
||||
return BenchmarkResult::skipped($profile, "등록되지 않은 커맨드: {$command} (해당 확장이 설치되어 있는지 확인)");
|
||||
}
|
||||
|
||||
$notify("배치 실행: {$command}");
|
||||
|
||||
// 피크 메모리는 프로세스 단위 누적값이라 감소하지 않는다. 실행 전 값을 함께
|
||||
// 기록해야 "이 배치가 피크를 밀어올렸는지"를 판정할 수 있다.
|
||||
$peakBefore = memory_get_peak_usage(true);
|
||||
$usageBefore = memory_get_usage(true);
|
||||
$buffer = new BufferedOutput;
|
||||
|
||||
$start = microtime(true);
|
||||
|
||||
try {
|
||||
$collected = $this->collector->collect(
|
||||
static fn () => Artisan::call($command, $arguments, $buffer)
|
||||
);
|
||||
$exitCode = (int) $collected['value'];
|
||||
$queries = $collected['queries'];
|
||||
} catch (\Throwable $e) {
|
||||
return BenchmarkResult::skipped($profile, '계측 실패: '.$e->getMessage());
|
||||
}
|
||||
|
||||
$elapsedMs = (microtime(true) - $start) * 1000;
|
||||
$peakAfter = memory_get_peak_usage(true);
|
||||
$usageAfter = memory_get_usage(true);
|
||||
$summary = $this->collector->summarize($queries);
|
||||
|
||||
$notes = [
|
||||
sprintf('실행 전 피크 %s → 실행 후 피크 %s', $this->formatBytes($peakBefore), $this->formatBytes($peakAfter)),
|
||||
];
|
||||
|
||||
if ($peakAfter <= $peakBefore) {
|
||||
$notes[] = '이 배치가 프로세스 피크를 밀어올리지 않았습니다 (실행 전 피크가 이미 더 높음).';
|
||||
}
|
||||
|
||||
if ($exitCode !== 0) {
|
||||
$notes[] = "커맨드가 실패 종료했습니다 (exit={$exitCode}) — 시간·메모리는 실패 지점까지의 값입니다.";
|
||||
}
|
||||
|
||||
$output = trim($buffer->fetch());
|
||||
|
||||
if ($output !== '') {
|
||||
$notes[] = '커맨드 출력: '.$output;
|
||||
}
|
||||
|
||||
return new BenchmarkResult(
|
||||
profile: $profile,
|
||||
headers: ['커맨드', '종료코드', '소요(ms)', '피크 메모리', '메모리 증가', '쿼리(건)'],
|
||||
rows: [[
|
||||
$command,
|
||||
(string) $exitCode,
|
||||
number_format($elapsedMs, 1),
|
||||
$this->formatBytes($peakAfter),
|
||||
$this->formatBytes(max(0, $usageAfter - $usageBefore)),
|
||||
number_format($summary['count']),
|
||||
]],
|
||||
metrics: [
|
||||
'command' => $command,
|
||||
'arguments' => $arguments,
|
||||
'exit_code' => $exitCode,
|
||||
'elapsed_ms' => round($elapsedMs, 2),
|
||||
'peak_memory_before_bytes' => $peakBefore,
|
||||
'peak_memory_after_bytes' => $peakAfter,
|
||||
'memory_delta_bytes' => $usageAfter - $usageBefore,
|
||||
'query_count' => $summary['count'],
|
||||
'db_ms' => $summary['db_ms'],
|
||||
],
|
||||
notes: $notes,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 바이트를 사람이 읽는 단위로 바꿉니다.
|
||||
*
|
||||
* @param int $bytes 바이트
|
||||
* @return string 포맷된 문자열
|
||||
*/
|
||||
private function formatBytes(int $bytes): string
|
||||
{
|
||||
if ($bytes >= 1024 ** 3) {
|
||||
return number_format($bytes / 1024 ** 3, 2).' GB';
|
||||
}
|
||||
|
||||
if ($bytes >= 1024 ** 2) {
|
||||
return number_format($bytes / 1024 ** 2, 1).' MB';
|
||||
}
|
||||
|
||||
return number_format($bytes / 1024, 1).' KB';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,347 @@
|
||||
<?php
|
||||
|
||||
namespace App\Benchmark\Axes;
|
||||
|
||||
use App\Benchmark\Contracts\BenchmarkAxisRunner;
|
||||
use App\Benchmark\DTO\BenchmarkProfile;
|
||||
use App\Benchmark\DTO\BenchmarkResult;
|
||||
use App\Benchmark\DTO\BenchmarkRunOptions;
|
||||
use App\Benchmark\SyntheticSeeder;
|
||||
use App\Enums\BenchmarkAxis;
|
||||
use Illuminate\Database\Query\Builder;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
/**
|
||||
* 목록 조회 축 실행기 — 깊은 OFFSET 비용을 컬럼 폭 3축으로 계측
|
||||
*
|
||||
* 목록 조회는 OFFSET 이 커질수록 느려지는데, 그 원인이 "건너뛸 행의 넓은 컬럼까지 읽기"
|
||||
* 인지 확인하려면 같은 필터·정렬로 (1) 전체 컬럼 (2) 실제 목록 컬럼 (3) 키 컬럼만 조회한
|
||||
* 비용을 나란히 재야 합니다. 셋째 축이 지연 조인(`PaginatesWithDeferredJoin`)의 inner
|
||||
* 쿼리에 해당하므로, 이 세 값의 배수가 곧 지연 조인 적용의 기대 효과입니다.
|
||||
*/
|
||||
class ListAxisRunner implements BenchmarkAxisRunner
|
||||
{
|
||||
/**
|
||||
* 목록 1페이지 상한 (OFFSET 스캔 비용 대비 무시할 수준이라 고정)
|
||||
*/
|
||||
private const PAGE_LIMIT = 20;
|
||||
|
||||
/**
|
||||
* 필터 선언에 쓸 수 있는 연산자 (닫힌 집합)
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
private const FILTER_OPERATORS = ['=', '!=', '<>', '<', '<=', '>', '>=', 'like', 'in', 'not in'];
|
||||
|
||||
public function __construct(private readonly SyntheticSeeder $seeder) {}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function axis(): BenchmarkAxis
|
||||
{
|
||||
return BenchmarkAxis::ListQuery;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function run(BenchmarkProfile $profile, BenchmarkRunOptions $options, ?\Closure $onProgress = null): BenchmarkResult
|
||||
{
|
||||
$notify = $onProgress ?? static fn (string $message) => null;
|
||||
$table = (string) $profile->option('table');
|
||||
|
||||
if (! Schema::hasTable($table)) {
|
||||
return BenchmarkResult::skipped($profile, "테이블이 없습니다: {$table} (해당 확장이 설치되어 있는지 확인)");
|
||||
}
|
||||
|
||||
if (($options->fresh || $options->seed > 0) && app()->environment('production')) {
|
||||
return BenchmarkResult::skipped($profile, '운영 환경에서는 시딩/비움을 사용할 수 없습니다.');
|
||||
}
|
||||
|
||||
$filterError = $this->validateFilters((array) $profile->option('filters', []));
|
||||
|
||||
if ($filterError !== null) {
|
||||
return BenchmarkResult::skipped($profile, $filterError);
|
||||
}
|
||||
|
||||
if ($options->fresh) {
|
||||
$this->seeder->truncate($table);
|
||||
$notify("비움: {$table}");
|
||||
}
|
||||
|
||||
if ($options->seed > 0) {
|
||||
$notify("시딩: {$table} ".number_format($options->seed).' 건');
|
||||
$this->seeder->seed(
|
||||
$table,
|
||||
$options->seed,
|
||||
(array) $profile->option('seed_overrides', []),
|
||||
fn (int $inserted, int $total) => $notify(sprintf(' 시딩 %s / %s', number_format($inserted), number_format($total)))
|
||||
);
|
||||
}
|
||||
|
||||
$columns = $this->existingColumns($table, (array) $profile->option('columns', ['*']));
|
||||
$order = $this->existingOrder($table, (array) $profile->option('order', [['id', 'desc']]));
|
||||
|
||||
$rows = [];
|
||||
$notes = [];
|
||||
|
||||
foreach ($options->offsets as $offset) {
|
||||
$allMs = $this->measure($profile, ['*'], $order, $offset, $options->runs);
|
||||
$listMs = $this->measure($profile, $columns, $order, $offset, $options->runs);
|
||||
$idOnlyMs = $this->measure($profile, ['id'], $order, $offset, $options->runs);
|
||||
|
||||
$rows[] = [
|
||||
'offset' => $offset,
|
||||
'all_ms' => $allMs,
|
||||
'list_ms' => $listMs,
|
||||
'id_only_ms' => $idOnlyMs,
|
||||
'ratio' => $idOnlyMs > 0 ? round($allMs / $idOnlyMs, 1) : null,
|
||||
];
|
||||
|
||||
if ($options->explain) {
|
||||
$notes[] = "EXPLAIN @ OFFSET {$offset} — 목록 컬럼";
|
||||
|
||||
foreach ($this->explain($profile, $columns, $order, $offset) as $line) {
|
||||
$notes[] = ' '.$line;
|
||||
}
|
||||
|
||||
// 지연 조인의 inner 가 실제로 어떤 계획을 타는지가 인덱스 설계의 근거다
|
||||
$notes[] = "EXPLAIN @ OFFSET {$offset} — ID 만 (지연 조인 inner)";
|
||||
|
||||
foreach ($this->explain($profile, ['id'], $order, $offset) as $line) {
|
||||
$notes[] = ' '.$line;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new BenchmarkResult(
|
||||
profile: $profile,
|
||||
headers: ['OFFSET', '전체 컬럼(ms)', '목록 컬럼(ms)', 'ID만(ms)', '전체÷ID'],
|
||||
rows: array_map(fn (array $row) => [
|
||||
number_format($row['offset']),
|
||||
number_format($row['all_ms'], 1),
|
||||
number_format($row['list_ms'], 1),
|
||||
number_format($row['id_only_ms'], 1),
|
||||
$row['ratio'] !== null ? $row['ratio'].'×' : '-',
|
||||
], $rows),
|
||||
metrics: [
|
||||
'table' => $table,
|
||||
'columns' => $columns,
|
||||
'runs' => $options->runs,
|
||||
'offsets' => $rows,
|
||||
],
|
||||
notes: $notes,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 스키마에 실제로 존재하는 컬럼만 남깁니다.
|
||||
*
|
||||
* `['*']` 는 "목록이 전 컬럼을 그대로 노출한다" 는 선언입니다. 응답 계약상 넓은 컬럼을
|
||||
* 뺄 수 없는 목록(활동 로그의 changes, 알림 발송 이력의 body 등)이 여기 해당하며, 이
|
||||
* 경우 계측의 비교 축은 select * vs select id 가 됩니다. 확장 버전에 따라 컬럼 구성이
|
||||
* 달라도 계측이 죽지 않도록 스키마에 없는 컬럼은 걸러냅니다.
|
||||
*
|
||||
* @param string $table 테이블명
|
||||
* @param array<int, string> $columns 후보 컬럼
|
||||
* @return array<int, string> 존재하는 컬럼 목록
|
||||
*/
|
||||
private function existingColumns(string $table, array $columns): array
|
||||
{
|
||||
if ($columns === ['*']) {
|
||||
return Schema::getColumnListing($table);
|
||||
}
|
||||
|
||||
$existing = array_values(array_filter(
|
||||
$columns,
|
||||
fn (mixed $column) => is_string($column) && Schema::hasColumn($table, $column)
|
||||
));
|
||||
|
||||
return $existing === [] ? ['id'] : $existing;
|
||||
}
|
||||
|
||||
/**
|
||||
* 스키마에 존재하는 정렬 컬럼만 남깁니다.
|
||||
*
|
||||
* @param string $table 테이블명
|
||||
* @param array<int, array{0: string, 1: string}> $order 후보 정렬
|
||||
* @return array<int, array{0: string, 1: string}> 적용 가능한 정렬 목록
|
||||
*/
|
||||
private function existingOrder(string $table, array $order): array
|
||||
{
|
||||
$existing = array_values(array_filter(
|
||||
$order,
|
||||
fn (mixed $spec) => is_array($spec) && isset($spec[0]) && Schema::hasColumn($table, (string) $spec[0])
|
||||
));
|
||||
|
||||
return $existing === [] ? [['id', 'desc']] : $existing;
|
||||
}
|
||||
|
||||
/**
|
||||
* 한 조합을 여러 번 실행해 중앙값(ms)을 돌려줍니다.
|
||||
*
|
||||
* `DB::enableQueryLog()` 는 오버헤드와 메모리 누적이 있어 쓰지 않고 직접 시간을 잽니다.
|
||||
*
|
||||
* @param BenchmarkProfile $profile 프로파일
|
||||
* @param array<int, string> $columns select 컬럼
|
||||
* @param array<int, array{0: string, 1: string}> $order 정렬
|
||||
* @param int $offset OFFSET
|
||||
* @param int $runs 측정 횟수
|
||||
* @return float 중앙값 (밀리초)
|
||||
*/
|
||||
private function measure(BenchmarkProfile $profile, array $columns, array $order, int $offset, int $runs): float
|
||||
{
|
||||
// 첫 회는 캐시 워밍 성격이라 버린다
|
||||
$this->buildQuery($profile, $columns, $order, $offset)->get();
|
||||
|
||||
$samples = [];
|
||||
|
||||
for ($i = 0; $i < max(1, $runs); $i++) {
|
||||
$start = microtime(true);
|
||||
$this->buildQuery($profile, $columns, $order, $offset)->get();
|
||||
$samples[] = (microtime(true) - $start) * 1000;
|
||||
}
|
||||
|
||||
sort($samples);
|
||||
|
||||
return $samples[intdiv(count($samples), 2)];
|
||||
}
|
||||
|
||||
/**
|
||||
* 계측 대상 쿼리를 조립합니다.
|
||||
*
|
||||
* @param BenchmarkProfile $profile 프로파일
|
||||
* @param array<int, string> $columns select 컬럼
|
||||
* @param array<int, array{0: string, 1: string}> $order 정렬
|
||||
* @param int $offset OFFSET
|
||||
* @return Builder 조립된 쿼리
|
||||
*/
|
||||
private function buildQuery(BenchmarkProfile $profile, array $columns, array $order, int $offset): Builder
|
||||
{
|
||||
$table = (string) $profile->option('table');
|
||||
$query = DB::table($table)->select($columns);
|
||||
|
||||
if ($profile->option('soft_delete', false) && Schema::hasColumn($table, 'deleted_at')) {
|
||||
$query->whereNull('deleted_at');
|
||||
}
|
||||
|
||||
$this->applyFilters($query, $table, (array) $profile->option('filters', []));
|
||||
|
||||
foreach ($order as $spec) {
|
||||
$query->orderBy($spec[0], $spec[1] ?? 'asc');
|
||||
}
|
||||
|
||||
return $query->offset($offset)->limit(self::PAGE_LIMIT);
|
||||
}
|
||||
|
||||
/**
|
||||
* 선언된 필터를 계측 쿼리에 적용합니다.
|
||||
*
|
||||
* 값이 `[연산자, 값]` 형태면 그 연산자로, 그 밖에는 등가 비교로 해석합니다. 등가 비교만
|
||||
* 지원하면 화면이 실제로 거는 필터를 선언할 방법이 없는 목록이 생깁니다 — 주문 목록은
|
||||
* 상태 미지정 시 임시 주문 상태를 `NOT IN` 으로 제외하므로, 그것을 선언하지 못하면
|
||||
* 계측이 화면과 다른 인덱스를 타게 됩니다.
|
||||
*
|
||||
* 연산자는 닫힌 집합으로 해석합니다. 임의 문자열을 그대로 넘기면 선언 오타가 조용히
|
||||
* 통과하거나 의도치 않은 SQL 이 조립됩니다.
|
||||
*
|
||||
* @param Builder $query 계측 쿼리
|
||||
* @param string $table 대상 테이블
|
||||
* @param array<string, mixed> $filters 선언된 필터
|
||||
*/
|
||||
private function applyFilters(Builder $query, string $table, array $filters): void
|
||||
{
|
||||
foreach ($filters as $column => $declared) {
|
||||
if (! Schema::hasColumn($table, (string) $column)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// [연산자, 값] 형태가 아니면 등가 비교 (배열 값을 IN 으로 오해석하지 않도록 형태로 판정)
|
||||
if (! is_array($declared) || count($declared) !== 2 || ! is_string($declared[0])) {
|
||||
$query->where($column, $declared);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
[$operator, $value] = $declared;
|
||||
|
||||
match (strtolower($operator)) {
|
||||
'=' => $query->where($column, '=', $value),
|
||||
'!=', '<>' => $query->where($column, '!=', $value),
|
||||
'<' => $query->where($column, '<', $value),
|
||||
'<=' => $query->where($column, '<=', $value),
|
||||
'>' => $query->where($column, '>', $value),
|
||||
'>=' => $query->where($column, '>=', $value),
|
||||
'like' => $query->where($column, 'like', $value),
|
||||
'in' => $query->whereIn($column, (array) $value),
|
||||
'not in' => $query->whereNotIn($column, (array) $value),
|
||||
// 도달 불가 — run() 이 실행 전에 연산자를 검증해 거부한다
|
||||
default => null,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 선언된 필터의 연산자를 실행 전에 검증합니다.
|
||||
*
|
||||
* 알 수 없는 연산자를 조용히 무시하면 그 필터가 빠진 채로 측정되어, 화면과 다른 것을
|
||||
* 재면서도 정상 측정으로 보고됩니다. 실행 전에 사유와 함께 거부합니다.
|
||||
*
|
||||
* @param array<string, mixed> $filters 선언된 필터
|
||||
* @return string|null 실패 사유 (문제 없으면 null)
|
||||
*/
|
||||
private function validateFilters(array $filters): ?string
|
||||
{
|
||||
foreach ($filters as $column => $declared) {
|
||||
if (! is_array($declared) || count($declared) !== 2 || ! is_string($declared[0])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (! in_array(strtolower($declared[0]), self::FILTER_OPERATORS, true)) {
|
||||
return sprintf(
|
||||
'필터 %s 의 연산자를 알 수 없습니다: %s (허용: %s)',
|
||||
$column,
|
||||
$declared[0],
|
||||
implode(', ', self::FILTER_OPERATORS)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 실행 계획을 수집합니다.
|
||||
*
|
||||
* @param BenchmarkProfile $profile 프로파일
|
||||
* @param array<int, string> $columns select 컬럼
|
||||
* @param array<int, array{0: string, 1: string}> $order 정렬
|
||||
* @param int $offset OFFSET
|
||||
* @return array<int, string> 실행 계획 요약 줄 목록
|
||||
*/
|
||||
private function explain(BenchmarkProfile $profile, array $columns, array $order, int $offset): array
|
||||
{
|
||||
$query = $this->buildQuery($profile, $columns, $order, $offset);
|
||||
|
||||
try {
|
||||
$plan = DB::select('EXPLAIN '.$query->toSql(), $query->getBindings());
|
||||
} catch (\Throwable $e) {
|
||||
return ['실행 계획 수집 실패: '.$e->getMessage()];
|
||||
}
|
||||
|
||||
return array_map(function ($row) {
|
||||
$row = (array) $row;
|
||||
|
||||
return sprintf(
|
||||
'type=%s key=%s rows=%s filtered=%s extra=%s',
|
||||
$row['type'] ?? '-',
|
||||
$row['key'] ?? '-',
|
||||
$row['rows'] ?? '-',
|
||||
$row['filtered'] ?? '-',
|
||||
$row['Extra'] ?? '-'
|
||||
);
|
||||
}, $plan);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,261 @@
|
||||
<?php
|
||||
|
||||
namespace App\Benchmark\Axes;
|
||||
|
||||
use App\Benchmark\BenchmarkIdentity;
|
||||
use App\Benchmark\Contracts\BenchmarkAxisRunner;
|
||||
use App\Benchmark\DTO\BenchmarkProfile;
|
||||
use App\Benchmark\DTO\BenchmarkResult;
|
||||
use App\Benchmark\DTO\BenchmarkRunOptions;
|
||||
use App\Benchmark\QueryCollector;
|
||||
use App\Enums\BenchmarkAxis;
|
||||
use Illuminate\Contracts\Http\Kernel as HttpKernel;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
||||
/**
|
||||
* 화면 응답 축 실행기 — 화면 1장의 응답 시간 + 실행 쿼리 건수 + N+1 후보
|
||||
*
|
||||
* 사용자가 체감하는 단위가 "화면 한 장"이므로 네 축 중 값어치가 가장 큽니다. 목록 SELECT
|
||||
* 는 빠른데 화면이 느린 경우(관계 지연 로딩으로 인한 N+1, 권한 조회 반복 등)를 잡는 것이
|
||||
* 이 축의 목적입니다.
|
||||
*
|
||||
* 실행 방식은 라우트를 **HTTP 커널로 내부 요청** 처리하는 것입니다. `Route::dispatch` 로
|
||||
* 라우트만 때리면 전역 미들웨어(인증·권한·로케일·타임존)를 건너뛰어 화면과 다른 것을 재게
|
||||
* 됩니다. 커널을 쓰면 실제 요청과 같은 경로를 지납니다.
|
||||
*
|
||||
* 계측 계정 생성과 요청 처리 전체를 롤백되는 트랜잭션으로 감싸므로, 계측이 데이터에 흔적을
|
||||
* 남기지 않습니다(`BenchmarkIdentity::withRolledBackTransaction`).
|
||||
*/
|
||||
class ScreenAxisRunner implements BenchmarkAxisRunner
|
||||
{
|
||||
public function __construct(
|
||||
private readonly BenchmarkIdentity $identity,
|
||||
private readonly QueryCollector $collector,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function axis(): BenchmarkAxis
|
||||
{
|
||||
return BenchmarkAxis::Screen;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function run(BenchmarkProfile $profile, BenchmarkRunOptions $options, ?\Closure $onProgress = null): BenchmarkResult
|
||||
{
|
||||
if ($profile->mutates() && ! $options->allowWrite) {
|
||||
return BenchmarkResult::skipped($profile, '데이터를 변경하는 화면입니다. --allow-write 를 붙여 실행하세요.');
|
||||
}
|
||||
|
||||
[$uri, $uriError] = $this->resolveUri($profile);
|
||||
|
||||
if ($uriError !== null) {
|
||||
return BenchmarkResult::skipped($profile, $uriError);
|
||||
}
|
||||
|
||||
// `--as` 로 지정한 계정은 트랜잭션 진입 전에 확인한다 — 없는 계정으로 계측을 시작하면
|
||||
// 롤백 구간 안에서 실패해 사유만 흐려진다.
|
||||
if ($options->asUser !== null && $this->identity->findExistingUser($options->asUser) === null) {
|
||||
return BenchmarkResult::skipped($profile, "계측에 사용할 계정을 찾을 수 없습니다: {$options->asUser}");
|
||||
}
|
||||
|
||||
$method = strtoupper((string) $profile->option('method', 'GET'));
|
||||
$query = (array) $profile->option('query', []);
|
||||
$query['per_page'] ??= $options->perPage;
|
||||
|
||||
// 데이터를 변경하는 화면은 반복 측정 시 2회차부터 조건이 달라진다(중복 키, 재고 소진).
|
||||
// 롤백은 계측 종료 시 한 번이므로 회차 간에는 되돌려지지 않는다 → 1회만 잰다.
|
||||
$runs = $profile->mutates() ? 1 : max(1, $options->runs);
|
||||
$notes = [];
|
||||
|
||||
if ($profile->mutates()) {
|
||||
$notes[] = '데이터 변경 화면이라 1회만 측정했습니다 (회차 간 조건 변화 방지).';
|
||||
}
|
||||
|
||||
try {
|
||||
$measured = $this->identity->withRolledBackTransaction(
|
||||
fn () => $this->measure($profile, (string) $uri, $method, $query, $runs, $options, $onProgress)
|
||||
);
|
||||
} catch (\Throwable $e) {
|
||||
return BenchmarkResult::skipped($profile, '계측 실패: '.$e->getMessage());
|
||||
}
|
||||
|
||||
$summary = $measured['summary'];
|
||||
|
||||
foreach ($summary['n_plus_one'] as $candidate) {
|
||||
$notes[] = sprintf('N+1 후보 %d회: %s', $candidate['count'], $candidate['sql']);
|
||||
}
|
||||
|
||||
if ($summary['n_plus_one'] === []) {
|
||||
$notes[] = 'N+1 후보 없음 (같은 SQL 5회 이상 반복 없음).';
|
||||
}
|
||||
|
||||
return new BenchmarkResult(
|
||||
profile: $profile,
|
||||
headers: ['요청', '상태', '응답(ms)', '쿼리(건)', 'DB(ms)'],
|
||||
rows: [[
|
||||
$method.' '.$uri,
|
||||
(string) $measured['status'],
|
||||
number_format($measured['total_ms'], 1),
|
||||
number_format($summary['count']),
|
||||
number_format($summary['db_ms'], 1),
|
||||
]],
|
||||
metrics: [
|
||||
'uri' => $uri,
|
||||
'method' => $method,
|
||||
'query' => $query,
|
||||
'status' => $measured['status'],
|
||||
'runs' => $runs,
|
||||
'total_ms' => $measured['total_ms'],
|
||||
'query_count' => $summary['count'],
|
||||
'db_ms' => $summary['db_ms'],
|
||||
'n_plus_one' => $summary['n_plus_one'],
|
||||
'acting_user' => $measured['acting_user'],
|
||||
],
|
||||
notes: $notes,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 계측 대상 URI 를 해석합니다.
|
||||
*
|
||||
* 라우트명 선언을 권장하는 이유는 두 가지입니다 — 프리픽스를 문자열로 조립하지 않아도
|
||||
* 되고, 라우트가 사라지면 계측이 조용히 404 를 재는 대신 사유를 남기고 건너뜁니다.
|
||||
*
|
||||
* 예외를 던지지 않고 사유 문자열을 함께 돌려주는 이유는, 이 축의 실패 계약이
|
||||
* `BenchmarkResult::skipped` 이기 때문입니다 — 던지고 곧바로 잡는 우회로를 만들지 않습니다.
|
||||
*
|
||||
* @param BenchmarkProfile $profile 프로파일
|
||||
* @return array{0: string|null, 1: string|null} [경로, 실패 사유]
|
||||
*/
|
||||
private function resolveUri(BenchmarkProfile $profile): array
|
||||
{
|
||||
$routeName = $profile->option('route');
|
||||
|
||||
if (is_string($routeName) && $routeName !== '') {
|
||||
if (Route::getRoutes()->getByName($routeName) === null) {
|
||||
return [null, "등록되지 않은 라우트명: {$routeName} (해당 확장이 설치되어 있는지 확인)"];
|
||||
}
|
||||
|
||||
return [route($routeName, (array) $profile->option('route_params', []), false), null];
|
||||
}
|
||||
|
||||
return ['/'.ltrim((string) $profile->option('uri'), '/'), null];
|
||||
}
|
||||
|
||||
/**
|
||||
* 내부 요청을 반복 실행해 응답 시간 중앙값과 쿼리 요약을 냅니다.
|
||||
*
|
||||
* @param BenchmarkProfile $profile 프로파일
|
||||
* @param string $uri 경로
|
||||
* @param string $method HTTP 메서드
|
||||
* @param array<string, mixed> $query 쿼리스트링
|
||||
* @param int $runs 측정 횟수
|
||||
* @param BenchmarkRunOptions $options 실행 옵션
|
||||
* @param \Closure|null $onProgress 진행 콜백
|
||||
* @return array{status: int, total_ms: float, summary: array<string, mixed>, acting_user: array<string, mixed>} 계측 결과
|
||||
*/
|
||||
private function measure(
|
||||
BenchmarkProfile $profile,
|
||||
string $uri,
|
||||
string $method,
|
||||
array $query,
|
||||
int $runs,
|
||||
BenchmarkRunOptions $options,
|
||||
?\Closure $onProgress,
|
||||
): array {
|
||||
$notify = $onProgress ?? static fn (string $message) => null;
|
||||
|
||||
$issued = $this->identity->issueToken(
|
||||
(array) $profile->option('permissions', []),
|
||||
$options->asUser !== null ? $this->identity->findExistingUser($options->asUser) : null
|
||||
);
|
||||
|
||||
$notify(sprintf(
|
||||
'인증: %s (%s)',
|
||||
$issued['user']->email,
|
||||
$issued['ephemeral'] ? '계측용 임시 계정 — 종료 시 롤백' : '지정 계정'
|
||||
));
|
||||
|
||||
$samples = [];
|
||||
$status = 0;
|
||||
$queries = [];
|
||||
|
||||
// 첫 회는 라우트 매칭·컨테이너 해석 워밍이라 버린다 (측정 회차와 별도로 1회 더 실행)
|
||||
for ($i = 0; $i <= $runs; $i++) {
|
||||
$collected = $this->collector->collect(
|
||||
fn () => $this->dispatch($uri, $method, $query, $issued['token'])
|
||||
);
|
||||
|
||||
/** @var array{status: int, ms: float} $outcome */
|
||||
$outcome = $collected['value'];
|
||||
$status = $outcome['status'];
|
||||
|
||||
if ($i === 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$samples[] = $outcome['ms'];
|
||||
$queries = $collected['queries'];
|
||||
}
|
||||
|
||||
sort($samples);
|
||||
|
||||
return [
|
||||
'status' => $status,
|
||||
'total_ms' => $samples[intdiv(count($samples), 2)],
|
||||
'summary' => $this->collector->summarize($queries),
|
||||
'acting_user' => [
|
||||
'id' => $issued['user']->id,
|
||||
'email' => $issued['user']->email,
|
||||
'ephemeral' => $issued['ephemeral'],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 내부 요청 1회를 처리하고 소요 시간을 잽니다.
|
||||
*
|
||||
* 커널이 컨테이너의 `request` 인스턴스와 인증 가드 상태를 갈아치우므로, 처리 후
|
||||
* 원상 복구합니다 — 복구하지 않으면 이어지는 회차/프로파일이 앞 요청의 인증 상태를
|
||||
* 물려받아 계측이 서로 오염됩니다.
|
||||
*
|
||||
* @param string $uri 경로
|
||||
* @param string $method HTTP 메서드
|
||||
* @param array<string, mixed> $query 쿼리스트링/바디
|
||||
* @param string $token Sanctum 토큰
|
||||
* @return array{status: int, ms: float} 상태 코드와 소요 시간
|
||||
*/
|
||||
private function dispatch(string $uri, string $method, array $query, string $token): array
|
||||
{
|
||||
$previousRequest = app()->bound('request') ? app('request') : null;
|
||||
|
||||
$request = Request::create($uri, $method, $query);
|
||||
$request->headers->set('Accept', 'application/json');
|
||||
$request->headers->set('Authorization', 'Bearer '.$token);
|
||||
|
||||
$start = microtime(true);
|
||||
|
||||
try {
|
||||
$response = app(HttpKernel::class)->handle($request);
|
||||
$status = $response->getStatusCode();
|
||||
// 스트리밍 응답은 본문 생성까지가 사용자 체감 시간이라 여기서 소비한다
|
||||
$response->getContent();
|
||||
} finally {
|
||||
$ms = (microtime(true) - $start) * 1000;
|
||||
|
||||
Auth::forgetGuards();
|
||||
|
||||
if ($previousRequest !== null) {
|
||||
app()->instance('request', $previousRequest);
|
||||
}
|
||||
}
|
||||
|
||||
return ['status' => $status, 'ms' => $ms];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,227 @@
|
||||
<?php
|
||||
|
||||
namespace App\Benchmark\Axes;
|
||||
|
||||
use App\Benchmark\BenchmarkIdentity;
|
||||
use App\Benchmark\Contracts\BenchmarkAxisRunner;
|
||||
use App\Benchmark\DTO\BenchmarkProfile;
|
||||
use App\Benchmark\DTO\BenchmarkResult;
|
||||
use App\Benchmark\DTO\BenchmarkRunOptions;
|
||||
use App\Benchmark\QueryCollector;
|
||||
use App\Enums\BenchmarkAxis;
|
||||
|
||||
/**
|
||||
* 쓰기 축 실행기 — 저장 경로 1회 소요 시간
|
||||
*
|
||||
* 목록 조회와 달리 저장 경로는 커맨드가 스스로 조립할 수 없습니다(주문 생성 하나에 재고·
|
||||
* 쿠폰·마일리지·알림이 얽힘). 그래서 실행 자체는 소유 확장이 선언한 콜백에 맡기고, 이
|
||||
* 실행기는 시간·쿼리 건수 계측과 안전장치만 담당합니다.
|
||||
*
|
||||
* 콜백은 `'Fqcn'`(invokable) 또는 `['Fqcn', 'method']` 형식만 허용합니다 — 코어 선언은
|
||||
* `config/benchmark.php` 에 있고 이 파일은 `config:cache` 대상이라 클로저를 담을 수 없기
|
||||
* 때문입니다. 확장 선언도 같은 스키마를 쓰므로 형식을 통일합니다.
|
||||
*
|
||||
* 선언 필드는 세 개입니다 — `prepare`(계측 제외 선행 준비, 회차마다 실행), `callback`
|
||||
* (계측 대상, prepare 반환값을 인자로 받음), `cleanup`(트랜잭션이 되돌리지 못하는 잔여물 정리).
|
||||
*
|
||||
* 계측으로 생긴 행은 롤백되는 트랜잭션으로 되돌립니다. 트랜잭션이 되돌리지 못하는 것
|
||||
* (파일·캐시·외부 호출)은 프로파일이 선언한 `cleanup` 콜백이 정리합니다.
|
||||
*/
|
||||
class WriteAxisRunner implements BenchmarkAxisRunner
|
||||
{
|
||||
public function __construct(
|
||||
private readonly BenchmarkIdentity $identity,
|
||||
private readonly QueryCollector $collector,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function axis(): BenchmarkAxis
|
||||
{
|
||||
return BenchmarkAxis::Write;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function run(BenchmarkProfile $profile, BenchmarkRunOptions $options, ?\Closure $onProgress = null): BenchmarkResult
|
||||
{
|
||||
$notify = $onProgress ?? static fn (string $message) => null;
|
||||
|
||||
if (! $options->allowWrite) {
|
||||
return BenchmarkResult::skipped($profile, '쓰기 축입니다. --allow-write 를 붙여 실행하세요.');
|
||||
}
|
||||
|
||||
$resolved = [];
|
||||
|
||||
foreach (['callback', 'prepare', 'cleanup'] as $field) {
|
||||
if ($field !== 'callback' && $profile->option($field) === null) {
|
||||
$resolved[$field] = null;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
[$callable, $error] = $this->resolveCallable($profile->option($field), $profile, $field);
|
||||
|
||||
if ($error !== null) {
|
||||
return BenchmarkResult::skipped($profile, $error);
|
||||
}
|
||||
|
||||
$resolved[$field] = $callable;
|
||||
}
|
||||
|
||||
$callback = $resolved['callback'];
|
||||
$prepare = $resolved['prepare'];
|
||||
$cleanup = $resolved['cleanup'];
|
||||
|
||||
$runs = max(1, $options->runs);
|
||||
|
||||
try {
|
||||
$measured = $this->identity->withRolledBackTransaction(
|
||||
fn () => $this->measure($callback, $prepare, $runs, $onProgress)
|
||||
);
|
||||
} catch (\Throwable $e) {
|
||||
return BenchmarkResult::skipped($profile, '계측 실패: '.$e->getMessage());
|
||||
} finally {
|
||||
if ($cleanup !== null) {
|
||||
// 트랜잭션 롤백으로 되돌지 않는 잔여물(파일·캐시)을 정리한다.
|
||||
// 정리 실패가 계측 결과를 삼키면 안 되므로 사유만 알린다.
|
||||
try {
|
||||
$cleanup();
|
||||
} catch (\Throwable $e) {
|
||||
$notify('cleanup 실패: '.$e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$summary = $this->collector->summarize($measured['queries']);
|
||||
|
||||
return new BenchmarkResult(
|
||||
profile: $profile,
|
||||
headers: ['첫 회(ms)', '중앙값(ms)', '회차', '쿼리(건)', 'DB(ms)'],
|
||||
rows: [[
|
||||
number_format($measured['first_ms'], 1),
|
||||
number_format($measured['median_ms'], 1),
|
||||
(string) $runs,
|
||||
number_format($summary['count']),
|
||||
number_format($summary['db_ms'], 1),
|
||||
]],
|
||||
metrics: [
|
||||
'first_ms' => $measured['first_ms'],
|
||||
'median_ms' => $measured['median_ms'],
|
||||
'runs' => $runs,
|
||||
'samples_ms' => $measured['samples'],
|
||||
'query_count' => $summary['count'],
|
||||
'db_ms' => $summary['db_ms'],
|
||||
'n_plus_one' => $summary['n_plus_one'],
|
||||
],
|
||||
notes: array_merge(
|
||||
['계측으로 생긴 행은 롤백했습니다.'],
|
||||
array_map(
|
||||
fn (array $candidate) => sprintf('반복 쿼리 %d회: %s', $candidate['count'], $candidate['sql']),
|
||||
$summary['n_plus_one']
|
||||
)
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 선언된 콜백을 호출 가능한 형태로 해석합니다.
|
||||
*
|
||||
* 예외를 던지지 않고 사유 문자열을 함께 돌려주는 이유는, 이 축의 실패 계약이
|
||||
* `BenchmarkResult::skipped` 이기 때문입니다 — 던지고 곧바로 잡는 우회로를 만들지 않습니다.
|
||||
*
|
||||
* @param mixed $declared 선언값
|
||||
* @param BenchmarkProfile $profile 프로파일 (오류 메시지용)
|
||||
* @param string $field 선언 필드명 (오류 메시지용)
|
||||
* @return array{0: callable|null, 1: string|null} [해석된 콜백, 실패 사유]
|
||||
*/
|
||||
private function resolveCallable(mixed $declared, BenchmarkProfile $profile, string $field): array
|
||||
{
|
||||
$where = $profile->qualifiedKey().' 의 '.$field;
|
||||
|
||||
if ($declared instanceof \Closure) {
|
||||
return [null, "{$where}: 클로저는 쓸 수 없습니다 (config:cache 불가). 'Fqcn' 또는 ['Fqcn', 'method'] 형식으로 선언하세요."];
|
||||
}
|
||||
|
||||
if (is_string($declared)) {
|
||||
if (! class_exists($declared)) {
|
||||
return [null, "{$where}: 클래스를 찾을 수 없습니다 — {$declared}"];
|
||||
}
|
||||
|
||||
$instance = app($declared);
|
||||
|
||||
if (! is_callable($instance)) {
|
||||
return [null, "{$where}: {$declared} 에 __invoke() 가 없습니다."];
|
||||
}
|
||||
|
||||
return [$instance, null];
|
||||
}
|
||||
|
||||
if (is_array($declared) && count($declared) === 2 && is_string($declared[0]) && is_string($declared[1])) {
|
||||
[$class, $method] = $declared;
|
||||
|
||||
if (! class_exists($class)) {
|
||||
return [null, "{$where}: 클래스를 찾을 수 없습니다 — {$class}"];
|
||||
}
|
||||
|
||||
$instance = app($class);
|
||||
|
||||
if (! method_exists($instance, $method)) {
|
||||
return [null, "{$where}: {$class}::{$method}() 가 없습니다."];
|
||||
}
|
||||
|
||||
return [[$instance, $method], null];
|
||||
}
|
||||
|
||||
return [null, "{$where}: 'Fqcn' 또는 ['Fqcn', 'method'] 형식이어야 합니다."];
|
||||
}
|
||||
|
||||
/**
|
||||
* 콜백을 반복 실행해 첫 회 시간과 중앙값을 냅니다.
|
||||
*
|
||||
* 첫 회를 버리지 않고 따로 보고하는 이유는, 저장 경로에서는 첫 회가 포함하는 비용
|
||||
* (클래스 로딩, 설정 해석, 관계 초기 조회)이 실사용에서도 발생하기 때문입니다.
|
||||
* 쿼리 요약은 마지막 회차 기준입니다 — 첫 회는 위 초기화 쿼리가 섞입니다.
|
||||
*
|
||||
* `prepare` 는 계측 구간 **밖**에서 회차마다 실행합니다. 저장 경로에는 선행 상태가
|
||||
* 필요한 경우가 많고(주문 생성에는 임시 주문이 필요하고 임시 주문은 1회만 전환됨),
|
||||
* 그 준비 비용이 측정값에 섞이면 재려던 것을 재지 못하게 됩니다.
|
||||
*
|
||||
* @param callable $callback 저장 경로 콜백 (prepare 반환값을 인자로 받음)
|
||||
* @param callable|null $prepare 회차별 선행 준비 콜백 (계측 제외, 회차 번호를 인자로 받음)
|
||||
* @param int $runs 측정 횟수
|
||||
* @param \Closure|null $onProgress 진행 콜백
|
||||
* @return array{first_ms: float, median_ms: float, samples: array<int, float>, queries: array<int, array{sql: string, time: float}>} 계측 결과
|
||||
*/
|
||||
private function measure(callable $callback, ?callable $prepare, int $runs, ?\Closure $onProgress): array
|
||||
{
|
||||
$notify = $onProgress ?? static fn (string $message) => null;
|
||||
$samples = [];
|
||||
$queries = [];
|
||||
|
||||
for ($i = 1; $i <= $runs; $i++) {
|
||||
$notify("쓰기 계측 {$i} / {$runs}");
|
||||
|
||||
$context = $prepare !== null ? $prepare($i) : null;
|
||||
|
||||
$start = microtime(true);
|
||||
$collected = $this->collector->collect(static function () use ($callback, $context) {
|
||||
$callback($context);
|
||||
});
|
||||
$samples[] = (microtime(true) - $start) * 1000;
|
||||
$queries = $collected['queries'];
|
||||
}
|
||||
|
||||
$sorted = $samples;
|
||||
sort($sorted);
|
||||
|
||||
return [
|
||||
'first_ms' => $samples[0],
|
||||
'median_ms' => $sorted[intdiv(count($sorted), 2)],
|
||||
'samples' => $samples,
|
||||
'queries' => $queries,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
<?php
|
||||
|
||||
namespace App\Benchmark;
|
||||
|
||||
use App\Enums\ExtensionOwnerType;
|
||||
use App\Models\Permission;
|
||||
use App\Models\Role;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
/**
|
||||
* 화면 계측용 인증 주체 해석기
|
||||
*
|
||||
* 관리자 화면의 응답 시간은 로그인 상태에서만 잴 수 있습니다. 두 방식을 지원합니다.
|
||||
*
|
||||
* 기본 — 프로파일이 선언한 권한만 가진 임시 관리자를 즉석 생성해 Sanctum 토큰 발급
|
||||
* `--as` — 기존 계정을 지정 (그 계정의 실제 역할/권한으로 재측정)
|
||||
*
|
||||
* 두 경로 모두 계정/역할/토큰 생성이라는 **쓰기**를 동반하므로, 호출자가 열어둔 트랜잭션
|
||||
* 안에서 수행하고 계측 후 롤백해 흔적을 남기지 않습니다(`withRolledBackTransaction`).
|
||||
* 트랜잭션이 열려 있으면 Laravel 이 읽기 쿼리도 write PDO 로 보내므로(`Connection::getReadPdo`
|
||||
* 의 `transactions > 0` 분기), 읽기/쓰기 분리 환경에서도 계측 요청이 이 계정을 인증할 수
|
||||
* 있습니다. 운영 DB 에서도 잔여 계정이 생기지 않는 이유가 이 롤백입니다.
|
||||
*/
|
||||
class BenchmarkIdentity
|
||||
{
|
||||
/**
|
||||
* 롤백되는 트랜잭션 안에서 콜백을 실행합니다.
|
||||
*
|
||||
* 계측이 만든 계정·토큰과, 계측 대상이 만든 행까지 함께 되돌립니다. 쓰기 축의 결과를
|
||||
* 되돌리는 것도 같은 장치를 씁니다.
|
||||
*
|
||||
* @param \Closure $callback 트랜잭션 안에서 실행할 작업
|
||||
* @return mixed 콜백 반환값
|
||||
*/
|
||||
public function withRolledBackTransaction(\Closure $callback): mixed
|
||||
{
|
||||
DB::beginTransaction();
|
||||
|
||||
try {
|
||||
return $callback();
|
||||
} finally {
|
||||
// 계측 결과 산출 여부와 무관하게 되돌린다 — 예외로 빠져나가도 잔여 데이터 없음
|
||||
DB::rollBack();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 계측에 사용할 Sanctum 토큰을 발급합니다.
|
||||
*
|
||||
* 호출 전에 `--as` 계정 존재를 `findExistingUser()` 로 확인해야 합니다 — 계정 부재는
|
||||
* 계측 실패가 아니라 실행 전 판정 대상이라 이 메서드는 그 경우를 다루지 않습니다.
|
||||
*
|
||||
* @param array<int, string> $permissions 임시 계정에 부여할 권한 식별자 목록
|
||||
* @param User|null $actor `--as` 로 확인된 기존 계정 (null 이면 임시 계정 생성)
|
||||
* @return array{token: string, user: User, ephemeral: bool} 토큰과 인증 주체
|
||||
*/
|
||||
public function issueToken(array $permissions = [], ?User $actor = null): array
|
||||
{
|
||||
$user = $actor ?? $this->makeEphemeralAdmin($permissions);
|
||||
|
||||
return [
|
||||
'token' => $user->createToken('g7-bench-'.Str::random(8))->plainTextToken,
|
||||
'user' => $user,
|
||||
'ephemeral' => $actor === null,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 지정된 기존 계정을 찾습니다.
|
||||
*
|
||||
* @param string $identifier 계정 ID 또는 이메일
|
||||
* @return User|null 찾은 계정 (없으면 null)
|
||||
*/
|
||||
public function findExistingUser(string $identifier): ?User
|
||||
{
|
||||
return ctype_digit($identifier)
|
||||
? User::find((int) $identifier)
|
||||
: User::where('email', $identifier)->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* 선언된 권한만 가진 임시 관리자를 생성합니다.
|
||||
*
|
||||
* 권한을 프로파일 선언에서 받는 이유는, 계측 대상 화면이 요구하는 권한만 주어야
|
||||
* 미들웨어 통과 여부까지 실제와 같아지기 때문입니다(전권 계정으로 재면 권한 검사
|
||||
* 비용과 분기가 달라집니다).
|
||||
*
|
||||
* @param array<int, string> $permissions 권한 식별자 목록
|
||||
* @return User 생성된 임시 관리자
|
||||
*/
|
||||
private function makeEphemeralAdmin(array $permissions): User
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$permissionIds = [];
|
||||
|
||||
foreach ($permissions as $identifier) {
|
||||
$identifier = (string) $identifier;
|
||||
|
||||
$permission = Permission::firstOrCreate(
|
||||
['identifier' => $identifier],
|
||||
[
|
||||
'name' => json_encode(['ko' => $identifier, 'en' => $identifier]),
|
||||
'description' => json_encode(['ko' => $identifier, 'en' => $identifier]),
|
||||
'extension_type' => ExtensionOwnerType::Core,
|
||||
'extension_identifier' => 'core',
|
||||
'type' => 'admin',
|
||||
]
|
||||
);
|
||||
|
||||
$permissionIds[] = $permission->id;
|
||||
}
|
||||
|
||||
$benchRole = Role::create([
|
||||
'identifier' => 'g7_bench_'.Str::random(8),
|
||||
'name' => json_encode(['ko' => '성능 계측 전용', 'en' => 'Benchmark Only']),
|
||||
'description' => json_encode(['ko' => '성능 계측 임시 역할', 'en' => 'Temporary benchmark role']),
|
||||
'is_active' => true,
|
||||
]);
|
||||
|
||||
$adminRole = Role::firstOrCreate(
|
||||
['identifier' => 'admin'],
|
||||
[
|
||||
'name' => json_encode(['ko' => '관리자', 'en' => 'Admin']),
|
||||
'description' => json_encode(['ko' => '시스템 관리자', 'en' => 'System Admin']),
|
||||
'extension_type' => ExtensionOwnerType::Core,
|
||||
'extension_identifier' => 'core',
|
||||
'type' => 'admin',
|
||||
'is_active' => true,
|
||||
]
|
||||
);
|
||||
|
||||
if ($permissionIds !== []) {
|
||||
$benchRole->permissions()->sync($permissionIds);
|
||||
}
|
||||
|
||||
$user->roles()->attach($adminRole->id, ['assigned_at' => now(), 'assigned_by' => null]);
|
||||
$user->roles()->attach($benchRole->id, ['assigned_at' => now(), 'assigned_by' => null]);
|
||||
|
||||
return $user->fresh();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,275 @@
|
||||
<?php
|
||||
|
||||
namespace App\Benchmark;
|
||||
|
||||
use App\Benchmark\DTO\BenchmarkProfile;
|
||||
use App\Contracts\Extension\ModuleManagerInterface;
|
||||
use App\Contracts\Extension\PluginManagerInterface;
|
||||
use App\Enums\BenchmarkAxis;
|
||||
|
||||
/**
|
||||
* 계측 프로파일 레지스트리 — 코어 config + 활성 확장 선언 수집
|
||||
*
|
||||
* 계측 대상은 "지금 이 설치본에 실제로 존재하는 것"이어야 하므로, 커맨드에 대상을
|
||||
* 하드코딩하지 않고 소유자가 선언한 것을 런타임에 모읍니다. 코어는
|
||||
* `config/benchmark.php`, 확장은 `getBenchmarkProfiles()` 오버라이드가 선언 지점입니다
|
||||
* (채널·알림 정의 등 기존 확장 선언 훅과 동일한 수집 모델).
|
||||
*
|
||||
* 확장 선언은 격리 호출합니다 — 확장 하나의 잘못된 선언이 전체 목록을 날리면 계측 자체를
|
||||
* 못 하게 되므로, 실패한 선언만 사유와 함께 `warnings()` 로 드러내고 나머지는 살립니다.
|
||||
* 조용히 버리지는 않습니다(버려진 선언이 곧 계측 사각이 됩니다).
|
||||
*/
|
||||
class BenchmarkProfileRegistry
|
||||
{
|
||||
/**
|
||||
* 수집된 프로파일 (정규화 키 → 프로파일)
|
||||
*
|
||||
* @var array<string, BenchmarkProfile>|null
|
||||
*/
|
||||
private ?array $profiles = null;
|
||||
|
||||
/**
|
||||
* 수집 중 발생한 선언 오류 메시지
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
private array $warnings = [];
|
||||
|
||||
public function __construct(
|
||||
private readonly ModuleManagerInterface $moduleManager,
|
||||
private readonly PluginManagerInterface $pluginManager,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* 모든 프로파일을 정규화 키 기준으로 반환합니다.
|
||||
*
|
||||
* @return array<string, BenchmarkProfile> 정규화 키 → 프로파일
|
||||
*/
|
||||
public function all(): array
|
||||
{
|
||||
if ($this->profiles !== null) {
|
||||
return $this->profiles;
|
||||
}
|
||||
|
||||
$profiles = [];
|
||||
|
||||
foreach ($this->collectCore() as $profile) {
|
||||
$profiles[$profile->qualifiedKey()] = $profile;
|
||||
}
|
||||
|
||||
foreach ($this->collectExtensions() as $profile) {
|
||||
$profiles[$profile->qualifiedKey()] = $profile;
|
||||
}
|
||||
|
||||
return $this->profiles = $profiles;
|
||||
}
|
||||
|
||||
/**
|
||||
* 특정 축의 프로파일만 반환합니다.
|
||||
*
|
||||
* @param BenchmarkAxis $axis 대상 축
|
||||
* @return array<string, BenchmarkProfile> 정규화 키 → 프로파일
|
||||
*/
|
||||
public function byAxis(BenchmarkAxis $axis): array
|
||||
{
|
||||
return array_filter($this->all(), fn (BenchmarkProfile $profile) => $profile->axis === $axis);
|
||||
}
|
||||
|
||||
/**
|
||||
* 키로 프로파일을 해석합니다.
|
||||
*
|
||||
* 정규화 키(`sirsoft-ecommerce/orders`)를 우선 대조하고, 없으면 짧은 키로 찾습니다.
|
||||
* 짧은 키가 둘 이상의 확장에서 선언돼 모호하면 후보 목록을 사유로 돌려줍니다 —
|
||||
* 임의로 하나를 고르면 어느 확장의 목록을 잰 것인지 알 수 없게 됩니다.
|
||||
*
|
||||
* @param string $key 프로파일 키 (정규화 키 또는 짧은 키)
|
||||
* @return array{0: BenchmarkProfile|null, 1: string|null} [찾은 프로파일, 실패 사유]
|
||||
*/
|
||||
public function resolve(string $key): array
|
||||
{
|
||||
$profiles = $this->all();
|
||||
|
||||
if (isset($profiles[$key])) {
|
||||
return [$profiles[$key], null];
|
||||
}
|
||||
|
||||
$matches = array_filter($profiles, fn (BenchmarkProfile $profile) => $profile->key === $key);
|
||||
|
||||
if ($matches === []) {
|
||||
return [null, "등록되지 않은 프로파일: {$key} (--list-profiles 로 목록 확인)"];
|
||||
}
|
||||
|
||||
if (count($matches) > 1) {
|
||||
$candidates = implode(', ', array_keys($matches));
|
||||
|
||||
return [null, "프로파일 키가 모호합니다: {$key} — 다음 중 하나로 지정하세요: {$candidates}"];
|
||||
}
|
||||
|
||||
return [reset($matches), null];
|
||||
}
|
||||
|
||||
/**
|
||||
* 키로 프로파일을 찾습니다. (해석 실패 시 null)
|
||||
*
|
||||
* 사유가 필요하면 `resolve()` 를 씁니다.
|
||||
*
|
||||
* @param string $key 프로파일 키 (정규화 키 또는 짧은 키)
|
||||
* @return BenchmarkProfile|null 찾은 프로파일
|
||||
*/
|
||||
public function find(string $key): ?BenchmarkProfile
|
||||
{
|
||||
return $this->resolve($key)[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* 수집 중 무시된 선언의 사유 목록을 반환합니다.
|
||||
*
|
||||
* @return array<int, string> 경고 메시지
|
||||
*/
|
||||
public function warnings(): array
|
||||
{
|
||||
$this->all();
|
||||
|
||||
return $this->warnings;
|
||||
}
|
||||
|
||||
/**
|
||||
* 코어 프로파일을 수집합니다.
|
||||
*
|
||||
* @return array<int, BenchmarkProfile> 코어 프로파일 목록
|
||||
*/
|
||||
private function collectCore(): array
|
||||
{
|
||||
$declared = config('benchmark.profiles', []);
|
||||
|
||||
return $this->normalize(is_array($declared) ? $declared : [], 'core', 'core');
|
||||
}
|
||||
|
||||
/**
|
||||
* 활성 모듈/플러그인 프로파일을 수집합니다.
|
||||
*
|
||||
* @return array<int, BenchmarkProfile> 확장 프로파일 목록
|
||||
*/
|
||||
private function collectExtensions(): array
|
||||
{
|
||||
$profiles = [];
|
||||
|
||||
foreach ($this->moduleManager->getActiveModules() as $module) {
|
||||
$profiles = array_merge(
|
||||
$profiles,
|
||||
$this->normalize($this->extract($module), 'module', $module->getIdentifier())
|
||||
);
|
||||
}
|
||||
|
||||
foreach ($this->pluginManager->getActivePlugins() as $plugin) {
|
||||
$profiles = array_merge(
|
||||
$profiles,
|
||||
$this->normalize($this->extract($plugin), 'plugin', $plugin->getIdentifier())
|
||||
);
|
||||
}
|
||||
|
||||
return $profiles;
|
||||
}
|
||||
|
||||
/**
|
||||
* 확장의 `getBenchmarkProfiles()` 선언을 격리 호출로 읽습니다.
|
||||
*
|
||||
* @param object $extension 확장 인스턴스
|
||||
* @return array<string, mixed> 선언 배열 (실패 시 빈 배열)
|
||||
*/
|
||||
private function extract(object $extension): array
|
||||
{
|
||||
if (! method_exists($extension, 'getBenchmarkProfiles')) {
|
||||
return [];
|
||||
}
|
||||
|
||||
try {
|
||||
$declared = $extension->getBenchmarkProfiles();
|
||||
} catch (\Throwable $e) {
|
||||
$this->warnings[] = sprintf(
|
||||
'%s: getBenchmarkProfiles() 호출 실패 — %s',
|
||||
method_exists($extension, 'getIdentifier') ? $extension->getIdentifier() : $extension::class,
|
||||
$e->getMessage()
|
||||
);
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
return is_array($declared) ? $declared : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* 선언 배열을 검증해 프로파일 객체로 정규화합니다.
|
||||
*
|
||||
* @param array<string, mixed> $declared 선언 배열 (키 → 정의)
|
||||
* @param string $sourceKind 출처 종류 (core|module|plugin)
|
||||
* @param string $sourceIdentifier 출처 식별자
|
||||
* @return array<int, BenchmarkProfile> 정규화된 프로파일 목록
|
||||
*/
|
||||
private function normalize(array $declared, string $sourceKind, string $sourceIdentifier): array
|
||||
{
|
||||
$profiles = [];
|
||||
|
||||
foreach ($declared as $key => $definition) {
|
||||
if (! is_string($key) || $key === '') {
|
||||
$this->warnings[] = "{$sourceIdentifier}: 프로파일 키는 빈 문자열이 아닌 문자열이어야 합니다.";
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (! is_array($definition)) {
|
||||
$this->warnings[] = "{$sourceIdentifier}/{$key}: 프로파일 정의는 배열이어야 합니다.";
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$axis = BenchmarkAxis::tryFrom((string) ($definition['type'] ?? ''));
|
||||
|
||||
if ($axis === null) {
|
||||
$this->warnings[] = sprintf(
|
||||
'%s/%s: 알 수 없는 type — %s (허용: %s)',
|
||||
$sourceIdentifier,
|
||||
$key,
|
||||
var_export($definition['type'] ?? null, true),
|
||||
implode('|', BenchmarkAxis::values())
|
||||
);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$missing = array_values(array_filter(
|
||||
$axis->requiredOptions(),
|
||||
fn (array $group) => array_filter(
|
||||
$group,
|
||||
fn (string $option) => isset($definition[$option])
|
||||
) === []
|
||||
));
|
||||
|
||||
if ($missing !== []) {
|
||||
$this->warnings[] = sprintf(
|
||||
'%s/%s: %s 축 필수 옵션 누락 — %s',
|
||||
$sourceIdentifier,
|
||||
$key,
|
||||
$axis->value,
|
||||
implode(', ', array_map(fn (array $group) => implode('|', $group), $missing))
|
||||
);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$label = $definition['label'] ?? null;
|
||||
unset($definition['type'], $definition['label']);
|
||||
|
||||
$profiles[] = new BenchmarkProfile(
|
||||
key: $key,
|
||||
axis: $axis,
|
||||
sourceKind: $sourceKind,
|
||||
sourceIdentifier: $sourceIdentifier,
|
||||
options: $definition,
|
||||
label: is_string($label) ? $label : null,
|
||||
);
|
||||
}
|
||||
|
||||
return $profiles;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,226 @@
|
||||
<?php
|
||||
|
||||
namespace App\Benchmark;
|
||||
|
||||
use App\Benchmark\DTO\BenchmarkResult;
|
||||
use App\Benchmark\DTO\BenchmarkRunOptions;
|
||||
use App\Enums\BenchmarkAxis;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
/**
|
||||
* 계측 결과 출력기 — 표준출력 표 / JSON / 마크다운 리포트
|
||||
*
|
||||
* 축을 모르고도 출력할 수 있는 이유는 `BenchmarkResult` 가 표시용 표와 기계 판독용 수치를
|
||||
* 함께 담기 때문입니다. 축이 늘어도 이 클래스는 고치지 않습니다.
|
||||
*
|
||||
* 마크다운 리포트에 환경 정보를 반드시 함께 적습니다 — 계측값은 실행 머신·DB 버전·
|
||||
* OPcache 여부에 종속되므로, 환경이 빠진 수치는 다른 리포트와 비교할 수 없는 숫자입니다.
|
||||
* 문서에 옮길 수치는 이 경로로만 산출해 눈대중 기재를 막는 것이 `--report` 의 목적입니다.
|
||||
*/
|
||||
class BenchmarkReporter
|
||||
{
|
||||
/**
|
||||
* 실행 환경 정보를 수집합니다.
|
||||
*
|
||||
* @return array<string, string> 항목 → 값
|
||||
*/
|
||||
public function environment(): array
|
||||
{
|
||||
return [
|
||||
'APP_ENV' => (string) config('app.env'),
|
||||
'G7 버전' => (string) config('app.version'),
|
||||
'DB 연결' => (string) config('database.default'),
|
||||
'DB 스키마' => $this->databaseName(),
|
||||
'DB 버전' => $this->databaseVersion(),
|
||||
'PHP 버전' => PHP_VERSION,
|
||||
'OPcache' => function_exists('opcache_get_status') && @opcache_get_status(false) !== false ? 'on' : 'off',
|
||||
'memory_limit' => (string) ini_get('memory_limit'),
|
||||
'config:cache' => app()->configurationIsCached() ? 'on' : 'off',
|
||||
'실행 머신' => php_uname('s').' '.php_uname('r').' / '.php_uname('m'),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON 으로 직렬화합니다.
|
||||
*
|
||||
* @param array<int, BenchmarkResult> $results 계측 결과
|
||||
* @param BenchmarkRunOptions $options 실행 옵션
|
||||
* @param array<int, string> $warnings 프로파일 수집 경고
|
||||
* @return string JSON 문자열
|
||||
*/
|
||||
public function toJson(array $results, BenchmarkRunOptions $options, array $warnings = []): string
|
||||
{
|
||||
return (string) json_encode([
|
||||
'generated_at' => now()->toIso8601String(),
|
||||
'environment' => $this->environment(),
|
||||
'options' => $options->toArray(),
|
||||
'warnings' => $warnings,
|
||||
'results' => array_map(fn (BenchmarkResult $result) => $result->toArray(), $results),
|
||||
], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
}
|
||||
|
||||
/**
|
||||
* 마크다운 리포트를 만듭니다.
|
||||
*
|
||||
* @param array<int, BenchmarkResult> $results 계측 결과
|
||||
* @param BenchmarkRunOptions $options 실행 옵션
|
||||
* @param array<int, string> $warnings 프로파일 수집 경고
|
||||
* @return string 마크다운 문서
|
||||
*/
|
||||
public function toMarkdown(array $results, BenchmarkRunOptions $options, array $warnings = []): string
|
||||
{
|
||||
$lines = [
|
||||
'# G7 성능 점검 리포트',
|
||||
'',
|
||||
'- 생성 시각: '.now()->toDateTimeString(),
|
||||
'',
|
||||
'## 실행 환경',
|
||||
'',
|
||||
'| 항목 | 값 |',
|
||||
'| ------ | ------ |',
|
||||
];
|
||||
|
||||
foreach ($this->environment() as $name => $value) {
|
||||
$lines[] = "| {$name} | {$value} |";
|
||||
}
|
||||
|
||||
$lines[] = '';
|
||||
$lines[] = '계측값은 위 환경에 종속됩니다. 다른 리포트와 비교할 때는 환경이 같은지 먼저 확인하세요.';
|
||||
$lines[] = '';
|
||||
$lines[] = '## 실행 조건';
|
||||
$lines[] = '';
|
||||
$lines[] = '| 항목 | 값 |';
|
||||
$lines[] = '| ------ | ------ |';
|
||||
|
||||
foreach ($options->toArray() as $name => $value) {
|
||||
$lines[] = sprintf('| %s | %s |', $name, $this->stringify($value));
|
||||
}
|
||||
|
||||
if ($warnings !== []) {
|
||||
$lines[] = '';
|
||||
$lines[] = '## 무시된 프로파일 선언';
|
||||
$lines[] = '';
|
||||
|
||||
foreach ($warnings as $warning) {
|
||||
$lines[] = '- '.$warning;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (BenchmarkAxis::cases() as $axis) {
|
||||
$axisResults = array_values(array_filter(
|
||||
$results,
|
||||
fn (BenchmarkResult $result) => $result->profile->axis === $axis
|
||||
));
|
||||
|
||||
if ($axisResults === []) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$lines[] = '';
|
||||
$lines[] = sprintf('## %s (%s)', $axis->label(), $axis->value);
|
||||
|
||||
foreach ($axisResults as $result) {
|
||||
$lines = array_merge($lines, $this->markdownSection($result));
|
||||
}
|
||||
}
|
||||
|
||||
return implode("\n", $lines)."\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* 결과 1건의 마크다운 절을 만듭니다.
|
||||
*
|
||||
* @param BenchmarkResult $result 계측 결과
|
||||
* @return array<int, string> 마크다운 줄 목록
|
||||
*/
|
||||
private function markdownSection(BenchmarkResult $result): array
|
||||
{
|
||||
$title = $result->profile->qualifiedKey();
|
||||
|
||||
if ($result->profile->label !== null) {
|
||||
$title .= ' — '.$result->profile->label;
|
||||
}
|
||||
|
||||
$lines = ['', '### '.$title, ''];
|
||||
|
||||
if ($result->skipped) {
|
||||
$lines[] = '측정하지 않음: '.$result->skipReason;
|
||||
|
||||
return $lines;
|
||||
}
|
||||
|
||||
$lines[] = '| '.implode(' | ', $result->headers).' |';
|
||||
$lines[] = '| '.implode(' | ', array_fill(0, count($result->headers), '------')).' |';
|
||||
|
||||
foreach ($result->rows as $row) {
|
||||
$lines[] = '| '.implode(' | ', $row).' |';
|
||||
}
|
||||
|
||||
if ($result->notes !== []) {
|
||||
$lines[] = '';
|
||||
|
||||
foreach ($result->notes as $note) {
|
||||
// 실행 계획/SQL 은 표 안에서 깨지므로 코드 스팬으로 감싼다
|
||||
$lines[] = '- '.(str_contains($note, '|') ? '`'.$note.'`' : $note);
|
||||
}
|
||||
}
|
||||
|
||||
return $lines;
|
||||
}
|
||||
|
||||
/**
|
||||
* 옵션 값을 표에 넣을 문자열로 바꿉니다.
|
||||
*
|
||||
* @param mixed $value 옵션 값
|
||||
* @return string 표시 문자열
|
||||
*/
|
||||
private function stringify(mixed $value): string
|
||||
{
|
||||
return match (true) {
|
||||
is_bool($value) => $value ? 'true' : 'false',
|
||||
is_array($value) => implode(', ', array_map(fn (mixed $item) => (string) $item, $value)),
|
||||
$value === null => '-',
|
||||
default => (string) $value,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 계측이 사용한 DB 스키마명을 반환합니다.
|
||||
*
|
||||
* 읽기/쓰기 분리 설정에서는 스키마명이 `write`/`read` 하위에만 있어 최상위 `database`
|
||||
* 가 비어 있습니다. 리포트에 스키마가 비면 어느 DB 를 잰 것인지 알 수 없으므로
|
||||
* 세 위치를 순서대로 확인합니다.
|
||||
*
|
||||
* @return string 스키마명 (확인 불가 시 '-')
|
||||
*/
|
||||
private function databaseName(): string
|
||||
{
|
||||
$connection = (string) config('database.default');
|
||||
|
||||
foreach (['database', 'write.database', 'read.database'] as $key) {
|
||||
$name = config("database.connections.{$connection}.{$key}");
|
||||
|
||||
if (is_string($name) && $name !== '') {
|
||||
return $name;
|
||||
}
|
||||
}
|
||||
|
||||
return '-';
|
||||
}
|
||||
|
||||
/**
|
||||
* DB 서버 버전을 조회합니다.
|
||||
*
|
||||
* @return string DB 버전 (조회 실패 시 '-')
|
||||
*/
|
||||
private function databaseVersion(): string
|
||||
{
|
||||
try {
|
||||
$row = DB::selectOne('select version() as version');
|
||||
|
||||
return (string) ($row->version ?? '-');
|
||||
} catch (\Throwable) {
|
||||
return '-';
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Benchmark\Contracts;
|
||||
|
||||
use App\Benchmark\DTO\BenchmarkProfile;
|
||||
use App\Benchmark\DTO\BenchmarkResult;
|
||||
use App\Benchmark\DTO\BenchmarkRunOptions;
|
||||
use App\Enums\BenchmarkAxis;
|
||||
|
||||
/**
|
||||
* 계측 축 실행기 계약
|
||||
*
|
||||
* 축이 늘어날 때 커맨드를 고치지 않고 실행기를 추가하도록 분리합니다. 커맨드는
|
||||
* 프로파일의 `type` 으로 실행기를 고르는 일만 합니다.
|
||||
*/
|
||||
interface BenchmarkAxisRunner
|
||||
{
|
||||
/**
|
||||
* 이 실행기가 담당하는 축을 반환합니다.
|
||||
*
|
||||
* @return BenchmarkAxis 담당 축
|
||||
*/
|
||||
public function axis(): BenchmarkAxis;
|
||||
|
||||
/**
|
||||
* 프로파일 1건을 계측합니다.
|
||||
*
|
||||
* @param BenchmarkProfile $profile 계측 대상
|
||||
* @param BenchmarkRunOptions $options 실행 옵션
|
||||
* @param \Closure|null $onProgress 진행 상황 콜백 (string $message, 시딩 등 장시간 작업 알림용)
|
||||
* @return BenchmarkResult 계측 결과
|
||||
*/
|
||||
public function run(BenchmarkProfile $profile, BenchmarkRunOptions $options, ?\Closure $onProgress = null): BenchmarkResult;
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
namespace App\Benchmark\DTO;
|
||||
|
||||
use App\Enums\BenchmarkAxis;
|
||||
|
||||
/**
|
||||
* 계측 프로파일 — 코어 config / 확장 선언에서 수집된 계측 대상 1건 (Value Object)
|
||||
*
|
||||
* 레지스트리가 선언 배열을 검증해 이 객체로 정규화하고, 축 실행기가 그대로 받아 씁니다.
|
||||
* 축별 옵션 스키마가 서로 다르므로 공통 필드(키·축·출처·라벨)만 프로퍼티로 승격하고
|
||||
* 축 고유 옵션은 `options` 에 남깁니다 — 축이 늘 때 VO 를 고치지 않아도 되게 합니다.
|
||||
*/
|
||||
final readonly class BenchmarkProfile
|
||||
{
|
||||
/**
|
||||
* @param string $key 선언된 프로파일 키 (확장 내부에서만 고유)
|
||||
* @param BenchmarkAxis $axis 계측 축
|
||||
* @param string $sourceKind 출처 종류 (core|module|plugin)
|
||||
* @param string $sourceIdentifier 출처 식별자 (코어는 'core')
|
||||
* @param array<string, mixed> $options 축 고유 옵션
|
||||
* @param string|null $label 표시용 설명
|
||||
*/
|
||||
public function __construct(
|
||||
public string $key,
|
||||
public BenchmarkAxis $axis,
|
||||
public string $sourceKind,
|
||||
public string $sourceIdentifier,
|
||||
public array $options = [],
|
||||
public ?string $label = null,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* 출처를 포함한 전역 고유 키를 반환합니다.
|
||||
*
|
||||
* 서로 다른 확장이 같은 키(`orders` 등)를 선언할 수 있으므로 충돌 시에는 이 키로
|
||||
* 지목합니다. 커맨드는 짧은 키가 유일할 때만 짧은 키를 허용합니다.
|
||||
*
|
||||
* @return string `{출처}/{키}` 형태의 정규화 키
|
||||
*/
|
||||
public function qualifiedKey(): string
|
||||
{
|
||||
return $this->sourceIdentifier.'/'.$this->key;
|
||||
}
|
||||
|
||||
/**
|
||||
* 축 고유 옵션 값을 읽습니다.
|
||||
*
|
||||
* @param string $name 옵션 키
|
||||
* @param mixed $default 미선언 시 기본값
|
||||
* @return mixed 옵션 값
|
||||
*/
|
||||
public function option(string $name, mixed $default = null): mixed
|
||||
{
|
||||
return $this->options[$name] ?? $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* 이 프로파일 실행이 데이터를 변경하는지 판정합니다.
|
||||
*
|
||||
* 축 기본값을 쓰되, 프로파일이 `mutating` 을 명시하면 그 선언을 따릅니다.
|
||||
* `screen` 축은 GET 이 아니면 변경으로 봅니다.
|
||||
*
|
||||
* @return bool 데이터 변경 여부
|
||||
*/
|
||||
public function mutates(): bool
|
||||
{
|
||||
$declared = $this->options['mutating'] ?? null;
|
||||
|
||||
if (is_bool($declared)) {
|
||||
return $declared;
|
||||
}
|
||||
|
||||
if ($this->axis === BenchmarkAxis::Screen) {
|
||||
return strtoupper((string) $this->option('method', 'GET')) !== 'GET';
|
||||
}
|
||||
|
||||
return $this->axis->mutatesByDefault();
|
||||
}
|
||||
|
||||
/**
|
||||
* 배열로 직렬화합니다. (`--json` / 리포트 출력용)
|
||||
*
|
||||
* @return array<string, mixed> 직렬화 결과
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'key' => $this->key,
|
||||
'qualified_key' => $this->qualifiedKey(),
|
||||
'axis' => $this->axis->value,
|
||||
'source' => ['kind' => $this->sourceKind, 'identifier' => $this->sourceIdentifier],
|
||||
'label' => $this->label,
|
||||
'options' => $this->options,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
namespace App\Benchmark\DTO;
|
||||
|
||||
/**
|
||||
* 계측 결과 1건 — 축 실행기의 산출물 (Value Object)
|
||||
*
|
||||
* 출력기(`BenchmarkReporter`)가 축을 몰라도 표/JSON/마크다운을 만들 수 있도록,
|
||||
* 표시용 표(`headers`/`rows`)와 기계 판독용 수치(`metrics`)를 함께 담습니다.
|
||||
* 축이 늘어날 때 출력기를 고치지 않아도 되는 지점이 여기입니다.
|
||||
*/
|
||||
final readonly class BenchmarkResult
|
||||
{
|
||||
/**
|
||||
* @param BenchmarkProfile $profile 계측 대상 프로파일
|
||||
* @param array<int, string> $headers 표 헤더
|
||||
* @param array<int, array<int, string>> $rows 표 행 (표시용 문자열)
|
||||
* @param array<string, mixed> $metrics 기계 판독용 원시 수치
|
||||
* @param array<int, string> $notes 부가 설명 줄 (실행 계획, N+1 후보 등)
|
||||
* @param bool $skipped 실행하지 않았는지 여부
|
||||
* @param string|null $skipReason 실행하지 않은 사유
|
||||
*/
|
||||
public function __construct(
|
||||
public BenchmarkProfile $profile,
|
||||
public array $headers = [],
|
||||
public array $rows = [],
|
||||
public array $metrics = [],
|
||||
public array $notes = [],
|
||||
public bool $skipped = false,
|
||||
public ?string $skipReason = null,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* 실행하지 않은 결과를 만듭니다.
|
||||
*
|
||||
* 실패/건너뜀을 결과 목록에서 빼면 리포트가 "전부 측정됨"으로 읽히므로, 사유를 담은
|
||||
* 결과로 남겨 표와 JSON 양쪽에 드러냅니다.
|
||||
*
|
||||
* @param BenchmarkProfile $profile 대상 프로파일
|
||||
* @param string $reason 건너뛴 사유
|
||||
* @return self 건너뜀 결과
|
||||
*/
|
||||
public static function skipped(BenchmarkProfile $profile, string $reason): self
|
||||
{
|
||||
return new self(profile: $profile, skipped: true, skipReason: $reason);
|
||||
}
|
||||
|
||||
/**
|
||||
* 배열로 직렬화합니다.
|
||||
*
|
||||
* @return array<string, mixed> 직렬화 결과
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'profile' => $this->profile->qualifiedKey(),
|
||||
'axis' => $this->profile->axis->value,
|
||||
'label' => $this->profile->label,
|
||||
'source' => ['kind' => $this->profile->sourceKind, 'identifier' => $this->profile->sourceIdentifier],
|
||||
'skipped' => $this->skipped,
|
||||
'skip_reason' => $this->skipReason,
|
||||
'headers' => $this->headers,
|
||||
'rows' => $this->rows,
|
||||
'metrics' => $this->metrics,
|
||||
'notes' => $this->notes,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace App\Benchmark\DTO;
|
||||
|
||||
/**
|
||||
* 계측 실행 옵션 — 커맨드 옵션을 축 실행기로 한 번 전달하는 값 묶음 (Value Object)
|
||||
*
|
||||
* 축마다 쓰는 옵션이 다르지만(offsets 는 list 축만, allowWrite 는 write/batch 축만)
|
||||
* 실행기 시그니처를 하나로 두기 위해 한 객체로 모읍니다.
|
||||
*/
|
||||
final readonly class BenchmarkRunOptions
|
||||
{
|
||||
/**
|
||||
* @param array<int, int> $offsets 측정할 OFFSET 목록 (list 축)
|
||||
* @param int $runs 측정 횟수 (첫 회는 버림)
|
||||
* @param int $seed 계측 전 합성 행 시딩 건수 (0 = 시딩 안 함, list 축)
|
||||
* @param bool $fresh 시딩 전 대상 테이블 비움 (list 축)
|
||||
* @param bool $explain 실행 계획 수집 (list 축)
|
||||
* @param bool $allowWrite 데이터 변경 축 실행 허용
|
||||
* @param string|null $asUser 계측에 사용할 기존 계정 (ID 또는 이메일, screen 축)
|
||||
* @param int $perPage 목록/화면 1페이지 건수
|
||||
*/
|
||||
public function __construct(
|
||||
public array $offsets = [0],
|
||||
public int $runs = 3,
|
||||
public int $seed = 0,
|
||||
public bool $fresh = false,
|
||||
public bool $explain = false,
|
||||
public bool $allowWrite = false,
|
||||
public ?string $asUser = null,
|
||||
public int $perPage = 20,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* 배열로 직렬화합니다. (리포트의 실행 조건 기재용)
|
||||
*
|
||||
* @return array<string, mixed> 직렬화 결과
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'offsets' => $this->offsets,
|
||||
'runs' => $this->runs,
|
||||
'seed' => $this->seed,
|
||||
'fresh' => $this->fresh,
|
||||
'explain' => $this->explain,
|
||||
'allow_write' => $this->allowWrite,
|
||||
'as_user' => $this->asUser,
|
||||
'per_page' => $this->perPage,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
|
||||
namespace App\Benchmark;
|
||||
|
||||
use Illuminate\Database\Events\QueryExecuted;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
/**
|
||||
* 실행 쿼리 수집기 — 화면/쓰기/배치 축의 쿼리 건수·시간·N+1 후보 산출
|
||||
*
|
||||
* 목록 SELECT 는 빨라도 화면이 느린 대표적 원인이 N+1 이므로, 응답 시간만 재고 끝내면
|
||||
* 원인을 못 찾습니다. 이 수집기는 계측 구간에서 실행된 쿼리를 모아 건수·DB 시간과, 같은
|
||||
* SQL 이 반복 실행된 그룹(N+1 후보)을 함께 돌려줍니다.
|
||||
*
|
||||
* `DB::listen` 은 한 번 등록하면 해제할 수 없으므로, 리스너는 인스턴스당 한 번만 등록하고
|
||||
* 수집 여부는 플래그로 켰다 끕니다 — 계측 구간 밖의 쿼리가 섞이지 않게 합니다.
|
||||
*/
|
||||
class QueryCollector
|
||||
{
|
||||
/**
|
||||
* 같은 SQL 이 이 횟수 이상 반복되면 N+1 후보로 본다
|
||||
*/
|
||||
private const N_PLUS_ONE_THRESHOLD = 5;
|
||||
|
||||
/**
|
||||
* 수집 구간 여부
|
||||
*/
|
||||
private bool $collecting = false;
|
||||
|
||||
/**
|
||||
* 수집된 쿼리 (sql, time)
|
||||
*
|
||||
* @var array<int, array{sql: string, time: float}>
|
||||
*/
|
||||
private array $queries = [];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
DB::listen(function (QueryExecuted $event) {
|
||||
if (! $this->collecting) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->queries[] = ['sql' => $event->sql, 'time' => (float) $event->time];
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 콜백 실행 구간의 쿼리를 수집합니다.
|
||||
*
|
||||
* @template TReturn
|
||||
*
|
||||
* @param \Closure(): TReturn $callback 계측 대상 작업
|
||||
* @return array{value: TReturn, queries: array<int, array{sql: string, time: float}>} 반환값과 수집 결과
|
||||
*/
|
||||
public function collect(\Closure $callback): array
|
||||
{
|
||||
$this->queries = [];
|
||||
$this->collecting = true;
|
||||
|
||||
try {
|
||||
$value = $callback();
|
||||
} finally {
|
||||
$this->collecting = false;
|
||||
}
|
||||
|
||||
return ['value' => $value, 'queries' => $this->queries];
|
||||
}
|
||||
|
||||
/**
|
||||
* 수집된 쿼리를 요약합니다.
|
||||
*
|
||||
* @param array<int, array{sql: string, time: float}> $queries 수집 결과
|
||||
* @return array{count: int, db_ms: float, n_plus_one: array<int, array{count: int, sql: string}>} 요약
|
||||
*/
|
||||
public function summarize(array $queries): array
|
||||
{
|
||||
$grouped = [];
|
||||
|
||||
foreach ($queries as $query) {
|
||||
$grouped[$query['sql']] = ($grouped[$query['sql']] ?? 0) + 1;
|
||||
}
|
||||
|
||||
arsort($grouped);
|
||||
|
||||
$candidates = [];
|
||||
|
||||
foreach ($grouped as $sql => $count) {
|
||||
if ($count < self::N_PLUS_ONE_THRESHOLD) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$candidates[] = ['count' => $count, 'sql' => $sql];
|
||||
}
|
||||
|
||||
return [
|
||||
'count' => count($queries),
|
||||
'db_ms' => round(array_sum(array_column($queries, 'time')), 2),
|
||||
'n_plus_one' => $candidates,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,190 @@
|
||||
<?php
|
||||
|
||||
namespace App\Benchmark;
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
/**
|
||||
* 계측용 합성 행 시딩기
|
||||
*
|
||||
* 깊은 OFFSET 비용은 행 수와 행 폭이 있어야 재현되므로, 계측 전에 대상 테이블을 원하는
|
||||
* 규모까지 채웁니다. 스키마를 introspect 해 NOT NULL + 기본값 없는 컬럼만 타입에 맞춰
|
||||
* 채우므로 계측 대상이 늘 때마다 시더를 따라 만들지 않아도 됩니다.
|
||||
*
|
||||
* 합성 값은 실제 부모 행을 가리키지 않으므로 외래키 제약을 일시 해제합니다. 본 시딩이
|
||||
* 재현하려는 것은 대상 테이블의 OFFSET 스캔 비용(행 수·행 폭)이고 조인은 계측 대상이
|
||||
* 아니므로, 부모 무결성 없이 목적에 충분합니다 — 폐기 가능한 계측용 DB 에서만 쓰는 것을
|
||||
* 전제로 하며, 운영 환경 거부는 호출자(`ListAxisRunner`)가 담당합니다.
|
||||
*/
|
||||
class SyntheticSeeder
|
||||
{
|
||||
/**
|
||||
* 한 번에 삽입할 행 수
|
||||
*/
|
||||
private const CHUNK_SIZE = 500;
|
||||
|
||||
/**
|
||||
* 계측용 합성 행을 시딩합니다.
|
||||
*
|
||||
* @param string $table 대상 테이블
|
||||
* @param int $count 시딩 건수
|
||||
* @param array<string, mixed> $overrides 컬럼별 고정값 (실제 조회 조건과 맞추기 위한 지정)
|
||||
* @param \Closure|null $onProgress 진행 콜백 (int $inserted, int $total)
|
||||
*/
|
||||
public function seed(string $table, int $count, array $overrides = [], ?\Closure $onProgress = null): void
|
||||
{
|
||||
$fillable = $this->fillableColumns($table);
|
||||
|
||||
$chunk = [];
|
||||
$inserted = 0;
|
||||
|
||||
for ($i = 1; $i <= $count; $i++) {
|
||||
$chunk[] = $this->synthesizeRow($table, $fillable, $overrides, $i);
|
||||
|
||||
if (count($chunk) >= self::CHUNK_SIZE) {
|
||||
$this->insert($table, $chunk);
|
||||
$inserted += count($chunk);
|
||||
$chunk = [];
|
||||
|
||||
if ($onProgress !== null) {
|
||||
$onProgress($inserted, $count);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($chunk !== []) {
|
||||
$this->insert($table, $chunk);
|
||||
$inserted += count($chunk);
|
||||
|
||||
if ($onProgress !== null) {
|
||||
$onProgress($inserted, $count);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 대상 테이블을 비웁니다.
|
||||
*
|
||||
* 다른 테이블이 FK 로 참조하면 TRUNCATE 가 거부되므로 제약을 잠시 내립니다.
|
||||
*
|
||||
* @param string $table 대상 테이블
|
||||
*/
|
||||
public function truncate(string $table): void
|
||||
{
|
||||
Schema::withoutForeignKeyConstraints(function () use ($table) {
|
||||
DB::table($table)->truncate();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 값을 반드시 채워야 하는 컬럼 목록을 반환합니다.
|
||||
*
|
||||
* @param string $table 대상 테이블
|
||||
* @return array<int, array<string, mixed>> Schema::getColumns() 항목 목록
|
||||
*/
|
||||
private function fillableColumns(string $table): array
|
||||
{
|
||||
return array_values(array_filter(
|
||||
Schema::getColumns($table),
|
||||
fn (array $column) => ! ($column['auto_increment'] ?? false)
|
||||
&& ! $column['nullable']
|
||||
&& ($column['default'] === null)
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* 합성 행 1건을 만듭니다.
|
||||
*
|
||||
* @param string $table 대상 테이블
|
||||
* @param array<int, array<string, mixed>> $fillable 채워야 하는 컬럼 목록
|
||||
* @param array<string, mixed> $overrides 컬럼별 고정값
|
||||
* @param int $index 행 인덱스 (고유값 생성용)
|
||||
* @return array<string, mixed> 합성 행
|
||||
*/
|
||||
private function synthesizeRow(string $table, array $fillable, array $overrides, int $index): array
|
||||
{
|
||||
$row = [];
|
||||
|
||||
foreach ($fillable as $column) {
|
||||
$row[$column['name']] = array_key_exists($column['name'], $overrides)
|
||||
? $overrides[$column['name']]
|
||||
: $this->synthesizeValue($column, $index);
|
||||
}
|
||||
|
||||
// NULL 허용 컬럼이라 위 루프에서 빠졌더라도, 실제 조회 조건에 쓰이는 컬럼은
|
||||
// 채워야 계측이 화면과 같은 인덱스를 타게 된다.
|
||||
foreach ($overrides as $name => $value) {
|
||||
if (! array_key_exists($name, $row) && Schema::hasColumn($table, $name)) {
|
||||
$row[$name] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return $row;
|
||||
}
|
||||
|
||||
/**
|
||||
* 합성 행을 삽입합니다. (외래키 제약 일시 해제)
|
||||
*
|
||||
* @param string $table 대상 테이블
|
||||
* @param array<int, array<string, mixed>> $chunk 삽입할 행 묶음
|
||||
*/
|
||||
private function insert(string $table, array $chunk): void
|
||||
{
|
||||
Schema::withoutForeignKeyConstraints(function () use ($table, $chunk) {
|
||||
DB::table($table)->insert($chunk);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 컬럼 타입에 맞는 합성 값을 만듭니다.
|
||||
*
|
||||
* @param array<string, mixed> $column Schema::getColumns() 항목
|
||||
* @param int $index 행 인덱스 (고유값 생성용)
|
||||
* @return mixed 합성 값
|
||||
*/
|
||||
private function synthesizeValue(array $column, int $index): mixed
|
||||
{
|
||||
$type = strtolower((string) ($column['type_name'] ?? $column['type'] ?? 'varchar'));
|
||||
|
||||
return match (true) {
|
||||
str_contains($type, 'int') => $index,
|
||||
str_contains($type, 'decimal'), str_contains($type, 'float'), str_contains($type, 'double') => 1000,
|
||||
str_contains($type, 'bool'), str_contains($type, 'tinyint') => 0,
|
||||
str_contains($type, 'json') => '{}',
|
||||
// 시간 컬럼을 한 값으로 채우면 정렬이 전부 동률이 되어 filesort 가 지배해 버린다.
|
||||
// 정렬 인덱스 효과를 재는 것이 목적이므로 행마다 값을 분산시킨다.
|
||||
str_contains($type, 'date'), str_contains($type, 'time') => now()->subMinutes($index),
|
||||
str_contains($type, 'text') => str_repeat('벤치마크 본문 ', 100),
|
||||
default => $this->synthesizeString($column, $index),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 문자열 컬럼의 선언 길이에 맞는 합성 값을 만듭니다.
|
||||
*
|
||||
* 컬럼 길이를 무시하고 고정 길이 문자열을 넣으면 `varchar(10)` 류(신고 대상 타입,
|
||||
* 우편번호, 로그 타입 등)에서 "Data too long" 으로 시딩 자체가 실패한다.
|
||||
*
|
||||
* @param array<string, mixed> $column Schema::getColumns() 항목
|
||||
* @param int $index 행 인덱스 (고유값 생성용)
|
||||
* @return string 합성 문자열
|
||||
*/
|
||||
private function synthesizeString(array $column, int $index): string
|
||||
{
|
||||
$length = 40;
|
||||
|
||||
// type 은 'varchar(10)' 형태로 선언 길이를 담고 있다
|
||||
if (preg_match('/\((\d+)\)/', (string) ($column['type'] ?? ''), $m) === 1) {
|
||||
$length = max(1, (int) $m[1]);
|
||||
}
|
||||
|
||||
// 짧은 컬럼은 고유성 확보가 우선이라 인덱스 자체를 문자열로 쓴다
|
||||
if ($length <= 12) {
|
||||
return substr((string) $index, -$length);
|
||||
}
|
||||
|
||||
return substr("bench-{$index}-".Str::random(8), 0, $length);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,395 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Benchmark\BenchmarkProfileRegistry;
|
||||
use App\Benchmark\BenchmarkReporter;
|
||||
use App\Benchmark\Contracts\BenchmarkAxisRunner;
|
||||
use App\Benchmark\DTO\BenchmarkProfile;
|
||||
use App\Benchmark\DTO\BenchmarkResult;
|
||||
use App\Benchmark\DTO\BenchmarkRunOptions;
|
||||
use App\Enums\BenchmarkAxis;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
/**
|
||||
* 성능 계측 커맨드 — 목록 조회 / 화면 응답 / 쓰기 작업 / 배치 작업 4축
|
||||
*
|
||||
* 계측 대상은 소유자가 선언합니다 — 코어는 `config/benchmark.php`, 확장은
|
||||
* `getBenchmarkProfiles()` 오버라이드입니다. 커맨드에 대상을 하드코딩하지 않는 이유는,
|
||||
* 확장이 설치·제거되는 설치본마다 실제로 존재하는 대상이 다르기 때문입니다.
|
||||
*
|
||||
* 축별 실행은 `App\Benchmark\Axes\*` 실행기가 담당하고, 이 커맨드는 프로파일 선택·옵션
|
||||
* 해석·출력만 합니다.
|
||||
*
|
||||
* tinker 스크립트 대신 커맨드로 만든 이유: tinker 는 REPL 이라 스크립트 실행 후에도 STDIN 을
|
||||
* 기다려 출력이 유실된다.
|
||||
*/
|
||||
class BenchCommand extends Command
|
||||
{
|
||||
protected $signature = 'g7:bench
|
||||
{--profile=* : 계측할 프로파일 키 (짧은 키 또는 출처/키 형태). 다중 지정 가능}
|
||||
{--axis= : 축 단위 실행 (list|screen|write|batch)}
|
||||
{--all : 등록된 모든 프로파일 실행}
|
||||
{--offsets=0,20000,50000,99980 : 측정할 OFFSET 목록 (쉼표 구분, list 축)}
|
||||
{--runs=3 : 측정 횟수 (첫 회는 버림)}
|
||||
{--per-page=20 : 목록/화면 1페이지 건수}
|
||||
{--seed=0 : 계측 전 합성 행 시딩 건수 (0 = 시딩 안 함, list 축)}
|
||||
{--fresh : 시딩 전에 대상 테이블을 비움 (운영 환경에서는 거부)}
|
||||
{--explain : 각 OFFSET 의 실행 계획도 함께 수집 (list 축)}
|
||||
{--as= : 화면 계측에 사용할 기존 계정 (ID 또는 이메일). 미지정 시 계측용 임시 계정}
|
||||
{--allow-write : 데이터를 변경하는 축(write/batch/비-GET 화면) 실행 허용}
|
||||
{--database= : 계측에 사용할 데이터베이스명 (미지정 시 기본 연결. 개발 DB 오염 방지용)}
|
||||
{--json : 기계 판독용 JSON 출력}
|
||||
{--report= : 마크다운 리포트 저장 경로 (값 없이 --report 만 주면 storage/app/benchmarks/ 아래 자동 생성)}
|
||||
{--list-profiles : 등록된 프로파일 목록 출력}';
|
||||
|
||||
protected $description = '목록 조회·화면 응답·쓰기·배치 4축 성능을 계측합니다 (대상은 코어 config + 확장 선언에서 수집)';
|
||||
|
||||
protected $aliases = ['g7:bench:pagination'];
|
||||
|
||||
/**
|
||||
* 리포트 기본 저장 디렉토리 (저장소 미추적 — 계측값은 실행 머신 사양에 종속되므로 축적하지 않음)
|
||||
*/
|
||||
private const REPORT_DIR = 'app/benchmarks';
|
||||
|
||||
/**
|
||||
* @param BenchmarkProfileRegistry $registry 프로파일 레지스트리
|
||||
* @param BenchmarkReporter $reporter 결과 출력기
|
||||
* @param iterable<BenchmarkAxisRunner> $runners 축 실행기 목록 (컨테이너 태그 주입)
|
||||
*/
|
||||
public function __construct(
|
||||
private readonly BenchmarkProfileRegistry $registry,
|
||||
private readonly BenchmarkReporter $reporter,
|
||||
private readonly iterable $runners,
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* 커맨드를 실행합니다.
|
||||
*
|
||||
* @return int 종료 코드
|
||||
*/
|
||||
public function handle(): int
|
||||
{
|
||||
$this->applyDatabaseOverride();
|
||||
|
||||
if ($this->option('list-profiles')) {
|
||||
return $this->listProfiles();
|
||||
}
|
||||
|
||||
[$profiles, $selectionError] = $this->selectProfiles();
|
||||
|
||||
if ($selectionError !== null) {
|
||||
$this->error($selectionError);
|
||||
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
if ($profiles === []) {
|
||||
$this->error('계측할 프로파일이 없습니다. --profile / --axis / --all 중 하나를 지정하세요.');
|
||||
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
$options = $this->runOptions();
|
||||
|
||||
// 계측 결과가 환경에 좌우되므로 실행 환경을 먼저 드러낸다
|
||||
if (! $this->option('json')) {
|
||||
foreach ($this->reporter->environment() as $name => $value) {
|
||||
$this->line(sprintf(' %-14s %s', $name, $value));
|
||||
}
|
||||
|
||||
$this->newLine();
|
||||
}
|
||||
|
||||
$this->reportCollectionWarnings();
|
||||
|
||||
$results = [];
|
||||
|
||||
foreach ($profiles as $profile) {
|
||||
$results[] = $this->runProfile($profile, $options);
|
||||
}
|
||||
|
||||
return $this->output($results, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* 계측용 데이터베이스 지정을 반영합니다.
|
||||
*
|
||||
* 계측은 대량 시딩을 동반하므로 개발 DB 대신 폐기 가능한 DB 를 지정할 수 있어야 합니다.
|
||||
* config 가 캐시된 환경에서는 `.env` / `--env` 로 연결을 바꿀 수 없어 런타임 치환이
|
||||
* 유일한 수단입니다.
|
||||
*/
|
||||
private function applyDatabaseOverride(): void
|
||||
{
|
||||
$database = (string) $this->option('database');
|
||||
|
||||
if ($database === '') {
|
||||
return;
|
||||
}
|
||||
|
||||
$connection = config('database.default');
|
||||
|
||||
config([
|
||||
"database.connections.{$connection}.database" => $database,
|
||||
"database.connections.{$connection}.write.database" => $database,
|
||||
"database.connections.{$connection}.read.database" => $database,
|
||||
]);
|
||||
|
||||
DB::purge($connection);
|
||||
}
|
||||
|
||||
/**
|
||||
* 등록된 프로파일 목록을 출력합니다.
|
||||
*
|
||||
* @return int 종료 코드
|
||||
*/
|
||||
private function listProfiles(): int
|
||||
{
|
||||
$profiles = $this->registry->all();
|
||||
|
||||
if ($this->option('json')) {
|
||||
$this->line((string) json_encode(
|
||||
array_map(fn (BenchmarkProfile $profile) => $profile->toArray(), array_values($profiles)),
|
||||
JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES
|
||||
));
|
||||
|
||||
return self::SUCCESS;
|
||||
}
|
||||
|
||||
$this->table(
|
||||
['프로파일', '축', '출처', '설명'],
|
||||
array_map(fn (BenchmarkProfile $profile) => [
|
||||
$profile->qualifiedKey(),
|
||||
$profile->axis->value,
|
||||
$profile->sourceKind,
|
||||
$profile->label ?? '-',
|
||||
], array_values($profiles))
|
||||
);
|
||||
|
||||
$this->reportCollectionWarnings();
|
||||
|
||||
return self::SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* 실행할 프로파일을 고릅니다.
|
||||
*
|
||||
* @return array{0: array<int, BenchmarkProfile>, 1: string|null} [실행 대상, 실패 사유]
|
||||
*/
|
||||
private function selectProfiles(): array
|
||||
{
|
||||
$keys = array_filter((array) $this->option('profile'), 'strlen');
|
||||
|
||||
if ($keys !== []) {
|
||||
$selected = [];
|
||||
|
||||
foreach ($keys as $key) {
|
||||
[$profile, $error] = $this->registry->resolve((string) $key);
|
||||
|
||||
if ($error !== null) {
|
||||
return [[], $error];
|
||||
}
|
||||
|
||||
$selected[] = $profile;
|
||||
}
|
||||
|
||||
return [$selected, null];
|
||||
}
|
||||
|
||||
$axisOption = (string) $this->option('axis');
|
||||
|
||||
if ($axisOption !== '') {
|
||||
$axis = BenchmarkAxis::tryFrom($axisOption);
|
||||
|
||||
if ($axis === null) {
|
||||
return [[], "알 수 없는 축: {$axisOption} (허용: ".implode('|', BenchmarkAxis::values()).')'];
|
||||
}
|
||||
|
||||
return [array_values($this->registry->byAxis($axis)), null];
|
||||
}
|
||||
|
||||
return [$this->option('all') ? array_values($this->registry->all()) : [], null];
|
||||
}
|
||||
|
||||
/**
|
||||
* 커맨드 옵션을 실행 옵션으로 해석합니다.
|
||||
*
|
||||
* @return BenchmarkRunOptions 실행 옵션
|
||||
*/
|
||||
private function runOptions(): BenchmarkRunOptions
|
||||
{
|
||||
$asUser = (string) $this->option('as');
|
||||
|
||||
return new BenchmarkRunOptions(
|
||||
offsets: $this->parseOffsets(),
|
||||
runs: max(1, (int) $this->option('runs')),
|
||||
seed: max(0, (int) $this->option('seed')),
|
||||
fresh: (bool) $this->option('fresh'),
|
||||
explain: (bool) $this->option('explain'),
|
||||
allowWrite: (bool) $this->option('allow-write'),
|
||||
asUser: $asUser === '' ? null : $asUser,
|
||||
perPage: max(1, (int) $this->option('per-page')),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* OFFSET 목록을 파싱합니다.
|
||||
*
|
||||
* @return array<int, int> 오름차순 정렬된 OFFSET 목록
|
||||
*/
|
||||
private function parseOffsets(): array
|
||||
{
|
||||
$offsets = array_map('intval', array_filter(explode(',', (string) $this->option('offsets')), 'strlen'));
|
||||
sort($offsets);
|
||||
|
||||
return $offsets === [] ? [0] : $offsets;
|
||||
}
|
||||
|
||||
/**
|
||||
* 프로파일 1건을 계측합니다.
|
||||
*
|
||||
* @param BenchmarkProfile $profile 대상 프로파일
|
||||
* @param BenchmarkRunOptions $options 실행 옵션
|
||||
* @return BenchmarkResult 계측 결과
|
||||
*/
|
||||
private function runProfile(BenchmarkProfile $profile, BenchmarkRunOptions $options): BenchmarkResult
|
||||
{
|
||||
$runner = $this->runnerFor($profile->axis);
|
||||
|
||||
if ($runner === null) {
|
||||
return BenchmarkResult::skipped($profile, "축 실행기가 없습니다: {$profile->axis->value}");
|
||||
}
|
||||
|
||||
if (! $this->option('json')) {
|
||||
$this->line(sprintf('▶ %s (%s)', $profile->qualifiedKey(), $profile->axis->label()));
|
||||
}
|
||||
|
||||
$onProgress = $this->option('json')
|
||||
? null
|
||||
: fn (string $message) => $this->line(' '.$message);
|
||||
|
||||
return $runner->run($profile, $options, $onProgress);
|
||||
}
|
||||
|
||||
/**
|
||||
* 축에 대응하는 실행기를 찾습니다.
|
||||
*
|
||||
* @param BenchmarkAxis $axis 대상 축
|
||||
* @return BenchmarkAxisRunner|null 실행기 (없으면 null)
|
||||
*/
|
||||
private function runnerFor(BenchmarkAxis $axis): ?BenchmarkAxisRunner
|
||||
{
|
||||
foreach ($this->runners as $runner) {
|
||||
if ($runner->axis() === $axis) {
|
||||
return $runner;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 무시된 프로파일 선언을 알립니다.
|
||||
*
|
||||
* 선언 오류를 조용히 버리면 그 대상이 계측 사각으로 남으므로 매 실행에 드러냅니다.
|
||||
*/
|
||||
private function reportCollectionWarnings(): void
|
||||
{
|
||||
if ($this->option('json')) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($this->registry->warnings() as $warning) {
|
||||
$this->warn('무시된 프로파일 선언 — '.$warning);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 계측 결과를 출력합니다.
|
||||
*
|
||||
* @param array<int, BenchmarkResult> $results 계측 결과
|
||||
* @param BenchmarkRunOptions $options 실행 옵션
|
||||
* @return int 종료 코드
|
||||
*/
|
||||
private function output(array $results, BenchmarkRunOptions $options): int
|
||||
{
|
||||
$warnings = $this->registry->warnings();
|
||||
|
||||
if ($this->option('json')) {
|
||||
$this->line($this->reporter->toJson($results, $options, $warnings));
|
||||
} else {
|
||||
foreach ($results as $result) {
|
||||
$this->newLine();
|
||||
$this->line(sprintf('%s (%s)', $result->profile->qualifiedKey(), $result->profile->axis->label()));
|
||||
|
||||
if ($result->skipped) {
|
||||
$this->warn(' 측정하지 않음: '.$result->skipReason);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->table($result->headers, $result->rows);
|
||||
|
||||
foreach ($result->notes as $note) {
|
||||
$this->line(' '.$note);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->wantsReport()) {
|
||||
$path = $this->writeReport($results, $options, $warnings);
|
||||
$this->newLine();
|
||||
$this->info('리포트 저장: '.$path);
|
||||
}
|
||||
|
||||
// 전부 건너뛴 실행을 성공으로 보고하면 "측정했다"로 읽히므로 실패로 돌려준다
|
||||
$measured = array_filter($results, fn (BenchmarkResult $result) => ! $result->skipped);
|
||||
|
||||
return $measured === [] ? self::FAILURE : self::SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* 리포트 출력이 요청되었는지 판정합니다.
|
||||
*
|
||||
* `--report` 는 값이 선택이라 `option()` 만으로는 "미지정"과 "값 없이 지정"을
|
||||
* 구분할 수 없으므로 원시 입력을 확인합니다.
|
||||
*
|
||||
* @return bool 리포트 출력 여부
|
||||
*/
|
||||
private function wantsReport(): bool
|
||||
{
|
||||
return $this->input->hasParameterOption('--report')
|
||||
|| $this->input->hasParameterOption('--report=')
|
||||
|| (string) $this->option('report') !== '';
|
||||
}
|
||||
|
||||
/**
|
||||
* 마크다운 리포트를 저장합니다.
|
||||
*
|
||||
* @param array<int, BenchmarkResult> $results 계측 결과
|
||||
* @param BenchmarkRunOptions $options 실행 옵션
|
||||
* @param array<int, string> $warnings 프로파일 수집 경고
|
||||
* @return string 저장된 절대 경로
|
||||
*/
|
||||
private function writeReport(array $results, BenchmarkRunOptions $options, array $warnings): string
|
||||
{
|
||||
$given = (string) $this->option('report');
|
||||
|
||||
$path = $given !== ''
|
||||
? $given
|
||||
: storage_path(self::REPORT_DIR.'/bench-'.now()->format('Ymd-His').'.md');
|
||||
|
||||
$directory = dirname($path);
|
||||
|
||||
if (! is_dir($directory)) {
|
||||
mkdir($directory, 0755, true);
|
||||
}
|
||||
|
||||
file_put_contents($path, $this->reporter->toMarkdown($results, $options, $warnings));
|
||||
|
||||
return $path;
|
||||
}
|
||||
}
|
||||
@@ -1,556 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Database\Query\Builder;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
/**
|
||||
* 깊은 OFFSET 페이지네이션 비용 계측 커맨드
|
||||
*
|
||||
* 목록 조회는 OFFSET 이 커질수록 느려지는데, 그 원인이 "건너뛸 행의 넓은 컬럼까지 읽기"인지
|
||||
* 확인하려면 같은 필터·정렬로 (1) 실제 목록 컬럼과 (2) 키 컬럼만 조회한 비용을 나란히 재야 한다.
|
||||
* 본 커맨드는 그 두 축을 같은 조건에서 측정해 표로 출력한다.
|
||||
*
|
||||
* tinker 스크립트 대신 커맨드로 만든 이유: tinker 는 REPL 이라 스크립트 실행 후에도 STDIN 을
|
||||
* 기다려 출력이 유실된다.
|
||||
*/
|
||||
class BenchPaginationCommand extends Command
|
||||
{
|
||||
protected $signature = 'g7:bench:pagination
|
||||
{--table=board_posts : 계측 대상 프로파일 키}
|
||||
{--offsets=0,20000,50000,99980 : 측정할 OFFSET 목록 (쉼표 구분)}
|
||||
{--runs=3 : OFFSET 당 측정 횟수 (첫 회는 버림)}
|
||||
{--seed=0 : 계측 전 합성 행 시딩 건수 (0 = 시딩 안 함)}
|
||||
{--database= : 계측에 사용할 데이터베이스명 (미지정 시 기본 연결. 개발 DB 오염 방지용)}
|
||||
{--fresh : 시딩 전에 대상 테이블을 비움 (운영 환경에서는 거부)}
|
||||
{--explain : 각 OFFSET 의 실행 계획도 함께 수집}
|
||||
{--json : 기계 판독용 JSON 출력}
|
||||
{--list-profiles : 등록된 프로파일 목록 출력}';
|
||||
|
||||
protected $description = '깊은 OFFSET 목록 조회 비용을 목록 컬럼 / 키 컬럼 두 축으로 계측합니다';
|
||||
|
||||
/**
|
||||
* 계측 프로파일 정의
|
||||
*
|
||||
* columns 는 해당 목록이 실제로 select 하는 컬럼이다. 스키마에 없는 컬럼은 실행 시 걸러낸다
|
||||
* (확장 버전에 따라 컬럼 구성이 달라도 커맨드가 죽지 않도록).
|
||||
*
|
||||
* @return array<string, array{table: string, columns: array<int, string>, order: array<int, array{0: string, 1: string}>, soft_delete: bool}>
|
||||
*/
|
||||
private function profiles(): array
|
||||
{
|
||||
return [
|
||||
'board_posts' => [
|
||||
'table' => 'board_posts',
|
||||
'columns' => ['id', 'board_id', 'title', 'author_name', 'view_count', 'is_notice', 'created_at'],
|
||||
'order' => [['created_at', 'desc'], ['id', 'desc']],
|
||||
'soft_delete' => true,
|
||||
// 실제 목록은 게시판 하나를 조회한다. 필터 없이 재면 인덱스 선택이 달라져
|
||||
// 화면에서 일어나는 일과 다른 것을 재게 된다.
|
||||
'filters' => ['board_id' => 1],
|
||||
'seed_overrides' => ['board_id' => 1, 'is_notice' => 0],
|
||||
],
|
||||
'orders' => [
|
||||
'table' => 'ecommerce_orders',
|
||||
'columns' => ['id', 'user_id', 'order_number', 'order_status', 'order_device', 'is_first_order', 'currency', 'currency_snapshot', 'total_amount', 'total_shipping_amount', 'total_paid_amount', 'total_cancelled_amount', 'total_refunded_amount', 'total_points_used_amount', 'total_earned_points_amount', 'mc_total_amount', 'mc_total_shipping_amount', 'ordered_at', 'created_at', 'updated_at', 'deleted_at'],
|
||||
'order' => [['ordered_at', 'desc'], ['id', 'desc']],
|
||||
'soft_delete' => true,
|
||||
],
|
||||
// 아래 두 목록은 응답 계약상 넓은 컬럼(changes/properties, body)을 그대로 노출하므로
|
||||
// 컬럼 프루닝이 불가하다. 지연 조인만 적용했고, 계측 축도 select * vs select id 다.
|
||||
'activity_logs' => [
|
||||
'table' => 'activity_logs',
|
||||
'columns' => ['*'],
|
||||
'order' => [['created_at', 'desc'], ['id', 'desc']],
|
||||
'soft_delete' => false,
|
||||
],
|
||||
'notification_logs' => [
|
||||
'table' => 'notification_logs',
|
||||
'columns' => ['*'],
|
||||
'order' => [['created_at', 'desc'], ['id', 'desc']],
|
||||
'soft_delete' => false,
|
||||
],
|
||||
'users' => [
|
||||
'table' => 'users',
|
||||
'columns' => ['*'],
|
||||
'order' => [['created_at', 'desc'], ['id', 'desc']],
|
||||
'soft_delete' => true,
|
||||
],
|
||||
'reports' => [
|
||||
'table' => 'boards_reports',
|
||||
'columns' => ['*'],
|
||||
'order' => [['created_at', 'desc'], ['id', 'desc']],
|
||||
'soft_delete' => true,
|
||||
],
|
||||
'coupon_issues' => [
|
||||
'table' => 'ecommerce_promotion_coupon_issues',
|
||||
'columns' => ['*'],
|
||||
'order' => [['issued_at', 'desc'], ['id', 'desc']],
|
||||
'soft_delete' => false,
|
||||
],
|
||||
'products' => [
|
||||
'table' => 'ecommerce_products',
|
||||
'columns' => ['*'],
|
||||
'order' => [['created_at', 'desc'], ['id', 'desc']],
|
||||
'soft_delete' => true,
|
||||
],
|
||||
'product_inquiries' => [
|
||||
'table' => 'ecommerce_product_inquiries',
|
||||
'columns' => ['*'],
|
||||
'order' => [['created_at', 'desc'], ['id', 'desc']],
|
||||
'soft_delete' => true,
|
||||
],
|
||||
'product_reviews' => [
|
||||
'table' => 'ecommerce_product_reviews',
|
||||
'columns' => ['*'],
|
||||
'order' => [['created_at', 'desc'], ['id', 'desc']],
|
||||
'soft_delete' => true,
|
||||
],
|
||||
'pages' => [
|
||||
'table' => 'pages',
|
||||
'columns' => ['*'],
|
||||
'order' => [['created_at', 'desc'], ['id', 'desc']],
|
||||
'soft_delete' => true,
|
||||
],
|
||||
'extra_fee_templates' => [
|
||||
'table' => 'ecommerce_shipping_policy_extra_fee_templates',
|
||||
'columns' => ['*'],
|
||||
'order' => [['zipcode', 'asc'], ['id', 'asc']],
|
||||
'soft_delete' => false,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 커맨드를 실행합니다.
|
||||
*
|
||||
* @return int 종료 코드
|
||||
*/
|
||||
public function handle(): int
|
||||
{
|
||||
$profiles = $this->profiles();
|
||||
|
||||
if ($this->option('list-profiles')) {
|
||||
foreach ($profiles as $key => $profile) {
|
||||
$this->line(sprintf('%-20s %s', $key, $profile['table']));
|
||||
}
|
||||
|
||||
return self::SUCCESS;
|
||||
}
|
||||
|
||||
$key = (string) $this->option('table');
|
||||
|
||||
if (! isset($profiles[$key])) {
|
||||
$this->error("등록되지 않은 프로파일: {$key} (--list-profiles 로 목록 확인)");
|
||||
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
$profile = $profiles[$key];
|
||||
|
||||
// 계측은 대량 시딩을 동반하므로 개발 DB 대신 폐기 가능한 DB 를 지정할 수 있어야 한다.
|
||||
// config 가 캐시된 환경에서는 .env / --env 로 연결을 바꿀 수 없어 런타임 치환이 유일한 수단이다.
|
||||
$database = (string) $this->option('database');
|
||||
|
||||
if ($database !== '') {
|
||||
$connection = config('database.default');
|
||||
|
||||
config([
|
||||
"database.connections.{$connection}.database" => $database,
|
||||
"database.connections.{$connection}.write.database" => $database,
|
||||
"database.connections.{$connection}.read.database" => $database,
|
||||
]);
|
||||
|
||||
DB::purge($connection);
|
||||
}
|
||||
|
||||
if (! Schema::hasTable($profile['table'])) {
|
||||
$this->error("테이블이 없습니다: {$profile['table']} (해당 확장이 설치되어 있는지 확인)");
|
||||
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
// 계측 결과가 환경에 좌우되므로 실행 환경을 먼저 드러낸다
|
||||
$this->line(sprintf(
|
||||
'환경: APP_ENV=%s / DB=%s / config:cache=%s',
|
||||
config('app.env'),
|
||||
config('database.default'),
|
||||
app()->configurationIsCached() ? 'on' : 'off'
|
||||
));
|
||||
|
||||
if ($this->option('fresh')) {
|
||||
if (app()->environment('production')) {
|
||||
$this->error('운영 환경에서는 --fresh 를 사용할 수 없습니다.');
|
||||
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
// 다른 테이블이 FK 로 참조하면 TRUNCATE 가 거부되므로 제약을 잠시 내린다
|
||||
Schema::withoutForeignKeyConstraints(function () use ($profile) {
|
||||
DB::table($profile['table'])->truncate();
|
||||
});
|
||||
|
||||
$this->warn("비움: {$profile['table']}");
|
||||
}
|
||||
|
||||
$seed = (int) $this->option('seed');
|
||||
|
||||
if ($seed > 0) {
|
||||
$this->seedRows($profile['table'], $seed, $profile['seed_overrides'] ?? []);
|
||||
}
|
||||
|
||||
$columns = $this->existingColumns($profile['table'], $profile['columns']);
|
||||
$order = $this->existingOrder($profile['table'], $profile['order']);
|
||||
$offsets = $this->parseOffsets();
|
||||
$runs = max(1, (int) $this->option('runs'));
|
||||
|
||||
$rows = [];
|
||||
|
||||
foreach ($offsets as $offset) {
|
||||
// 세 축: 컬럼 인자를 생략했을 때(=select *) / 목록 컬럼만 / 키 컬럼만.
|
||||
// 첫 축이 프루닝 이전 상태, 셋째 축이 지연 조인의 inner 에 해당한다.
|
||||
$allMs = $this->measure($profile, ['*'], $order, $offset, $runs);
|
||||
$listMs = $this->measure($profile, $columns, $order, $offset, $runs);
|
||||
$idOnlyMs = $this->measure($profile, ['id'], $order, $offset, $runs);
|
||||
|
||||
$rows[] = [
|
||||
'offset' => $offset,
|
||||
'all_ms' => $allMs,
|
||||
'list_ms' => $listMs,
|
||||
'id_only_ms' => $idOnlyMs,
|
||||
'ratio' => $idOnlyMs > 0 ? round($allMs / $idOnlyMs, 1) : null,
|
||||
'explain' => $this->option('explain') ? $this->explain($profile, $columns, $order, $offset) : null,
|
||||
// 지연 조인의 inner 가 실제로 어떤 계획을 타는지가 인덱스 설계의 근거다
|
||||
'explain_id_only' => $this->option('explain') ? $this->explain($profile, ['id'], $order, $offset) : null,
|
||||
];
|
||||
}
|
||||
|
||||
if ($this->option('json')) {
|
||||
$this->line(json_encode([
|
||||
'profile' => $key,
|
||||
'table' => $profile['table'],
|
||||
'columns' => $columns,
|
||||
'runs' => $runs,
|
||||
'results' => $rows,
|
||||
], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
|
||||
|
||||
return self::SUCCESS;
|
||||
}
|
||||
|
||||
$this->table(
|
||||
['OFFSET', '전체 컬럼(ms)', '목록 컬럼(ms)', 'ID만(ms)', '전체÷ID'],
|
||||
array_map(fn (array $row) => [
|
||||
number_format($row['offset']),
|
||||
number_format($row['all_ms'], 1),
|
||||
number_format($row['list_ms'], 1),
|
||||
number_format($row['id_only_ms'], 1),
|
||||
$row['ratio'] !== null ? $row['ratio'].'×' : '-',
|
||||
], $rows)
|
||||
);
|
||||
|
||||
if ($this->option('explain')) {
|
||||
foreach ($rows as $row) {
|
||||
$this->newLine();
|
||||
$this->line("EXPLAIN @ OFFSET {$row['offset']} — 목록 컬럼");
|
||||
|
||||
foreach ($row['explain'] as $line) {
|
||||
$this->line(' '.$line);
|
||||
}
|
||||
|
||||
$this->line("EXPLAIN @ OFFSET {$row['offset']} — ID 만 (지연 조인 inner)");
|
||||
|
||||
foreach ($row['explain_id_only'] as $line) {
|
||||
$this->line(' '.$line);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return self::SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* OFFSET 목록을 파싱합니다.
|
||||
*
|
||||
* @return array<int, int> 오름차순 정렬된 OFFSET 목록
|
||||
*/
|
||||
private function parseOffsets(): array
|
||||
{
|
||||
$offsets = array_map('intval', array_filter(explode(',', (string) $this->option('offsets')), 'strlen'));
|
||||
sort($offsets);
|
||||
|
||||
return $offsets === [] ? [0] : $offsets;
|
||||
}
|
||||
|
||||
/**
|
||||
* 스키마에 실제로 존재하는 컬럼만 남깁니다.
|
||||
*
|
||||
* @param string $table 테이블명
|
||||
* @param array<int, string> $columns 후보 컬럼
|
||||
* @return array<int, string> 존재하는 컬럼 목록
|
||||
*/
|
||||
private function existingColumns(string $table, array $columns): array
|
||||
{
|
||||
// ['*'] 는 "목록이 전 컬럼을 그대로 노출한다" 는 선언이다. 응답 계약상 넓은 컬럼을
|
||||
// 뺄 수 없는 목록(활동 로그의 changes, 알림 발송 이력의 body 등)이 여기 해당하며,
|
||||
// 이 경우 계측의 비교 축은 "select * vs select id" 가 된다.
|
||||
if ($columns === ['*']) {
|
||||
return Schema::getColumnListing($table);
|
||||
}
|
||||
|
||||
$existing = array_values(array_filter($columns, fn (string $column) => Schema::hasColumn($table, $column)));
|
||||
|
||||
return $existing === [] ? ['id'] : $existing;
|
||||
}
|
||||
|
||||
/**
|
||||
* 스키마에 존재하는 정렬 컬럼만 남깁니다.
|
||||
*
|
||||
* @param string $table 테이블명
|
||||
* @param array<int, array{0: string, 1: string}> $order 후보 정렬
|
||||
* @return array<int, array{0: string, 1: string}> 적용 가능한 정렬 목록
|
||||
*/
|
||||
private function existingOrder(string $table, array $order): array
|
||||
{
|
||||
$existing = array_values(array_filter($order, fn (array $spec) => Schema::hasColumn($table, $spec[0])));
|
||||
|
||||
return $existing === [] ? [['id', 'desc']] : $existing;
|
||||
}
|
||||
|
||||
/**
|
||||
* 한 조합을 여러 번 실행해 중앙값(ms)을 돌려줍니다.
|
||||
*
|
||||
* DB::enableQueryLog() 는 오버헤드와 메모리 누적이 있어 쓰지 않고 직접 시간을 잰다.
|
||||
*
|
||||
* @param array $profile 프로파일 정의
|
||||
* @param array<int, string> $columns select 컬럼
|
||||
* @param array<int, array{0: string, 1: string}> $order 정렬
|
||||
* @param int $offset OFFSET
|
||||
* @param int $runs 측정 횟수
|
||||
* @return float 중앙값 (밀리초)
|
||||
*/
|
||||
private function measure(array $profile, array $columns, array $order, int $offset, int $runs): float
|
||||
{
|
||||
// 첫 회는 캐시 워밍 성격이라 버린다
|
||||
$this->runQuery($profile, $columns, $order, $offset);
|
||||
|
||||
$samples = [];
|
||||
|
||||
for ($i = 0; $i < $runs; $i++) {
|
||||
$start = microtime(true);
|
||||
$this->runQuery($profile, $columns, $order, $offset);
|
||||
$samples[] = (microtime(true) - $start) * 1000;
|
||||
}
|
||||
|
||||
sort($samples);
|
||||
|
||||
return $samples[intdiv(count($samples), 2)];
|
||||
}
|
||||
|
||||
/**
|
||||
* 계측 대상 쿼리를 실행합니다.
|
||||
*
|
||||
* @param array $profile 프로파일 정의
|
||||
* @param array<int, string> $columns select 컬럼
|
||||
* @param array<int, array{0: string, 1: string}> $order 정렬
|
||||
* @param int $offset OFFSET
|
||||
*/
|
||||
private function runQuery(array $profile, array $columns, array $order, int $offset): void
|
||||
{
|
||||
$this->buildQuery($profile, $columns, $order, $offset)->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* 계측 대상 쿼리를 조립합니다.
|
||||
*
|
||||
* @param array $profile 프로파일 정의
|
||||
* @param array<int, string> $columns select 컬럼
|
||||
* @param array<int, array{0: string, 1: string}> $order 정렬
|
||||
* @param int $offset OFFSET
|
||||
* @return Builder 조립된 쿼리
|
||||
*/
|
||||
private function buildQuery(array $profile, array $columns, array $order, int $offset)
|
||||
{
|
||||
$query = DB::table($profile['table'])->select($columns);
|
||||
|
||||
if ($profile['soft_delete'] && Schema::hasColumn($profile['table'], 'deleted_at')) {
|
||||
$query->whereNull('deleted_at');
|
||||
}
|
||||
|
||||
foreach ($profile['filters'] ?? [] as $column => $value) {
|
||||
if (Schema::hasColumn($profile['table'], $column)) {
|
||||
$query->where($column, $value);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($order as $spec) {
|
||||
$query->orderBy($spec[0], $spec[1]);
|
||||
}
|
||||
|
||||
return $query->offset($offset)->limit(20);
|
||||
}
|
||||
|
||||
/**
|
||||
* 실행 계획을 수집합니다.
|
||||
*
|
||||
* @param array $profile 프로파일 정의
|
||||
* @param array<int, string> $columns select 컬럼
|
||||
* @param array<int, array{0: string, 1: string}> $order 정렬
|
||||
* @param int $offset OFFSET
|
||||
* @return array<int, string> 실행 계획 요약 줄 목록
|
||||
*/
|
||||
private function explain(array $profile, array $columns, array $order, int $offset): array
|
||||
{
|
||||
$query = $this->buildQuery($profile, $columns, $order, $offset);
|
||||
|
||||
try {
|
||||
$plan = DB::select('EXPLAIN '.$query->toSql(), $query->getBindings());
|
||||
} catch (\Throwable $e) {
|
||||
return ['실행 계획 수집 실패: '.$e->getMessage()];
|
||||
}
|
||||
|
||||
return array_map(function ($row) {
|
||||
$row = (array) $row;
|
||||
|
||||
return sprintf(
|
||||
'type=%s key=%s rows=%s filtered=%s extra=%s',
|
||||
$row['type'] ?? '-',
|
||||
$row['key'] ?? '-',
|
||||
$row['rows'] ?? '-',
|
||||
$row['filtered'] ?? '-',
|
||||
$row['Extra'] ?? '-'
|
||||
);
|
||||
}, $plan);
|
||||
}
|
||||
|
||||
/**
|
||||
* 계측용 합성 행을 시딩합니다.
|
||||
*
|
||||
* 스키마를 introspect 해 NOT NULL + 기본값 없는 컬럼만 타입에 맞춰 채운다. 프로파일마다
|
||||
* 별도 시더를 두면 대상이 늘 때마다 시더가 따라 늘어나므로 일반화한다.
|
||||
*
|
||||
* @param string $table 대상 테이블
|
||||
* @param int $count 시딩 건수
|
||||
* @param array<string, mixed> $overrides 컬럼별 고정값 (실제 조회 조건과 맞추기 위한 지정)
|
||||
*/
|
||||
private function seedRows(string $table, int $count, array $overrides = []): void
|
||||
{
|
||||
$columns = Schema::getColumns($table);
|
||||
$fillable = array_values(array_filter(
|
||||
$columns,
|
||||
fn (array $column) => ! ($column['auto_increment'] ?? false)
|
||||
&& ! $column['nullable']
|
||||
&& ($column['default'] === null)
|
||||
));
|
||||
|
||||
$this->info("시딩: {$table} {$count} 건");
|
||||
$bar = $this->output->createProgressBar($count);
|
||||
|
||||
$chunk = [];
|
||||
$chunkSize = 500;
|
||||
|
||||
for ($i = 1; $i <= $count; $i++) {
|
||||
$row = [];
|
||||
|
||||
foreach ($fillable as $column) {
|
||||
$row[$column['name']] = array_key_exists($column['name'], $overrides)
|
||||
? $overrides[$column['name']]
|
||||
: $this->synthesizeValue($column, $i);
|
||||
}
|
||||
|
||||
foreach ($overrides as $name => $value) {
|
||||
if (! array_key_exists($name, $row) && Schema::hasColumn($table, $name)) {
|
||||
$row[$name] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
$chunk[] = $row;
|
||||
|
||||
if (count($chunk) >= $chunkSize) {
|
||||
$this->insertSynthetic($table, $chunk);
|
||||
$bar->advance(count($chunk));
|
||||
$chunk = [];
|
||||
}
|
||||
}
|
||||
|
||||
if ($chunk !== []) {
|
||||
$this->insertSynthetic($table, $chunk);
|
||||
$bar->advance(count($chunk));
|
||||
}
|
||||
|
||||
$bar->finish();
|
||||
$this->newLine(2);
|
||||
}
|
||||
|
||||
/**
|
||||
* 합성 행을 삽입합니다. (외래키 제약 일시 해제)
|
||||
*
|
||||
* 합성 값은 실제 부모 행을 가리키지 않으므로 FK 를 그대로 두면 삽입이 거부된다.
|
||||
* 본 커맨드가 재는 것은 대상 테이블의 OFFSET 스캔 비용이고 조인은 측정 대상이 아니므로,
|
||||
* 부모 무결성 없이 행 수와 행 폭만 재현하면 목적에 충분하다. 폐기 가능한 계측용 DB
|
||||
* (`--database`)에서만 쓰는 것을 전제로 한다.
|
||||
*
|
||||
* @param string $table 대상 테이블
|
||||
* @param array<int, array<string, mixed>> $chunk 삽입할 행 묶음
|
||||
*/
|
||||
private function insertSynthetic(string $table, array $chunk): void
|
||||
{
|
||||
Schema::withoutForeignKeyConstraints(function () use ($table, $chunk) {
|
||||
DB::table($table)->insert($chunk);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 컬럼 타입에 맞는 합성 값을 만듭니다.
|
||||
*
|
||||
* @param array $column Schema::getColumns() 항목
|
||||
* @param int $index 행 인덱스 (고유값 생성용)
|
||||
* @return mixed 합성 값
|
||||
*/
|
||||
private function synthesizeValue(array $column, int $index): mixed
|
||||
{
|
||||
$type = strtolower((string) ($column['type_name'] ?? $column['type'] ?? 'varchar'));
|
||||
|
||||
return match (true) {
|
||||
str_contains($type, 'int') => $index,
|
||||
str_contains($type, 'decimal'), str_contains($type, 'float'), str_contains($type, 'double') => 1000,
|
||||
str_contains($type, 'bool'), str_contains($type, 'tinyint') => 0,
|
||||
str_contains($type, 'json') => '{}',
|
||||
// 시간 컬럼을 한 값으로 채우면 정렬이 전부 동률이 되어 filesort 가 지배해 버린다.
|
||||
// 정렬 인덱스 효과를 재는 것이 목적이므로 행마다 값을 분산시킨다.
|
||||
str_contains($type, 'date'), str_contains($type, 'time') => now()->subMinutes($index),
|
||||
str_contains($type, 'text') => str_repeat('벤치마크 본문 ', 100),
|
||||
default => $this->synthesizeString($column, $index),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 문자열 컬럼의 선언 길이에 맞는 합성 값을 만듭니다.
|
||||
*
|
||||
* 컬럼 길이를 무시하고 고정 길이 문자열을 넣으면 `varchar(10)` 류(신고 대상 타입,
|
||||
* 우편번호, 로그 타입 등)에서 "Data too long" 으로 시딩 자체가 실패한다.
|
||||
*
|
||||
* @param array $column Schema::getColumns() 항목
|
||||
* @param int $index 행 인덱스 (고유값 생성용)
|
||||
* @return string 합성 문자열
|
||||
*/
|
||||
private function synthesizeString(array $column, int $index): string
|
||||
{
|
||||
$length = 40;
|
||||
|
||||
// type 은 'varchar(10)' 형태로 선언 길이를 담고 있다
|
||||
if (preg_match('/\((\d+)\)/', (string) ($column['type'] ?? ''), $m) === 1) {
|
||||
$length = max(1, (int) $m[1]);
|
||||
}
|
||||
|
||||
// 짧은 컬럼은 고유성 확보가 우선이라 인덱스 자체를 문자열로 쓴다
|
||||
if ($length <= 12) {
|
||||
return substr((string) $index, -$length);
|
||||
}
|
||||
|
||||
return substr("bench-{$index}-".Str::random(8), 0, $length);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
namespace App\Enums;
|
||||
|
||||
/**
|
||||
* 성능 계측 축 Enum
|
||||
*
|
||||
* `g7:bench` 커맨드가 재는 네 가지 대상을 구분합니다. 프로파일 선언의 `type` 필드
|
||||
* 값 도메인이며, 축마다 필수 옵션과 실행기(`App\Benchmark\Axes\*`)가 다릅니다.
|
||||
*/
|
||||
enum BenchmarkAxis: string
|
||||
{
|
||||
/**
|
||||
* 목록 SELECT 비용 (전체 컬럼 / 목록 컬럼 / 키 컬럼 3축 비교)
|
||||
*/
|
||||
case ListQuery = 'list';
|
||||
|
||||
/**
|
||||
* 화면 1장 응답 시간 + 실행 쿼리 건수 + N+1 후보
|
||||
*/
|
||||
case Screen = 'screen';
|
||||
|
||||
/**
|
||||
* 저장 경로 1회 소요 시간 (주문 생성, 게시글 등록 등)
|
||||
*/
|
||||
case Write = 'write';
|
||||
|
||||
/**
|
||||
* 배치 커맨드 소요 시간 + 피크 메모리
|
||||
*/
|
||||
case Batch = 'batch';
|
||||
|
||||
/**
|
||||
* 사람이 읽을 수 있는 라벨
|
||||
*
|
||||
* @return string 축 라벨
|
||||
*/
|
||||
public function label(): string
|
||||
{
|
||||
return match ($this) {
|
||||
self::ListQuery => '목록 조회',
|
||||
self::Screen => '화면 응답',
|
||||
self::Write => '쓰기 작업',
|
||||
self::Batch => '배치 작업',
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 프로파일 선언에서 반드시 채워야 하는 옵션 키 목록
|
||||
*
|
||||
* 각 원소는 "대안 그룹"이며, 그룹마다 최소 하나가 선언되어야 합니다
|
||||
* (`screen` 축은 라우트명 또는 URI 중 하나). 레지스트리가 이 목록으로 선언을
|
||||
* 검증하며, 누락된 선언은 사유와 함께 경고로 드러내고 목록에서 제외합니다
|
||||
* (조용히 버리면 계측 사각이 됩니다).
|
||||
*
|
||||
* @return array<int, array<int, string>> 필수 옵션 대안 그룹 목록
|
||||
*/
|
||||
public function requiredOptions(): array
|
||||
{
|
||||
return match ($this) {
|
||||
self::ListQuery => [['table']],
|
||||
self::Screen => [['route', 'uri']],
|
||||
self::Write => [['callback']],
|
||||
self::Batch => [['command']],
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 기본적으로 데이터를 변경하는 축인지 여부
|
||||
*
|
||||
* true 인 축은 `--allow-write` 없이는 실행을 거부합니다. `screen` 축은 선언한
|
||||
* HTTP 메서드에 따라 달라지므로 프로파일 단위로 다시 판정합니다.
|
||||
*
|
||||
* @return bool 데이터 변경 축 여부
|
||||
*/
|
||||
public function mutatesByDefault(): bool
|
||||
{
|
||||
return match ($this) {
|
||||
self::ListQuery, self::Screen => false,
|
||||
self::Write, self::Batch => true,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 모든 값 배열
|
||||
*
|
||||
* @return array<int, string> 축 값 목록
|
||||
*/
|
||||
public static function values(): array
|
||||
{
|
||||
return array_column(self::cases(), 'value');
|
||||
}
|
||||
}
|
||||
@@ -749,6 +749,38 @@ abstract class AbstractModule implements CacheableExtensionInterface, ModuleInte
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* 성능 계측 프로파일 정의를 반환합니다.
|
||||
*
|
||||
* 이 모듈이 소유한 목록/화면/저장 경로/배치 중 성능을 재고 싶은 대상을 선언합니다.
|
||||
* `g7:bench` 커맨드가 코어 `config/benchmark.php` 선언과 함께 수집합니다
|
||||
* (`App\Benchmark\BenchmarkProfileRegistry`). 계측 대상을 코어 커맨드에 하드코딩하지
|
||||
* 않는 이유는, 확장이 설치·제거되는 설치본마다 실제로 존재하는 대상이 다르기
|
||||
* 때문입니다. 키는 모듈 내부에서만 고유하면 되고, 다른 확장과 겹치면 커맨드가
|
||||
* `{식별자}/{키}` 로 지목합니다.
|
||||
*
|
||||
* `write` 축의 `callback` 은 클로저를 쓸 수 없습니다 — 코어 선언과 스키마를 공유하고
|
||||
* 코어 쪽은 `config:cache` 대상이므로, 형식을 `'Fqcn'`(invokable) 또는
|
||||
* `['Fqcn', 'method']` 로 통일합니다.
|
||||
*
|
||||
* @return array<string, array<string, mixed>> 프로파일 키 → 정의
|
||||
* [
|
||||
* 'orders' => [
|
||||
* 'type' => 'list', // list | screen | write | batch
|
||||
* 'label' => '주문 목록',
|
||||
* 'table' => 'ecommerce_orders',
|
||||
* 'columns' => ['id', 'order_number', ...],
|
||||
* 'order' => [['ordered_at', 'desc']],
|
||||
* 'filters' => ['order_status' => 'paid'],
|
||||
* 'soft_delete' => true,
|
||||
* ],
|
||||
* ]
|
||||
*/
|
||||
public function getBenchmarkProfiles(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* 모듈 설치 시 실행할 시더 클래스 목록 반환
|
||||
*
|
||||
|
||||
@@ -658,6 +658,37 @@ abstract class AbstractPlugin implements CacheableExtensionInterface, PluginInte
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* 성능 계측 프로파일 정의를 반환합니다.
|
||||
*
|
||||
* 이 플러그인이 소유한 목록/화면/저장 경로/배치 중 성능을 재고 싶은 대상을 선언합니다.
|
||||
* `g7:bench` 커맨드가 코어 `config/benchmark.php` 선언과 함께 수집합니다
|
||||
* (`App\Benchmark\BenchmarkProfileRegistry`). 계측 대상을 코어 커맨드에 하드코딩하지
|
||||
* 않는 이유는, 확장이 설치·제거되는 설치본마다 실제로 존재하는 대상이 다르기
|
||||
* 때문입니다. 키는 플러그인 내부에서만 고유하면 되고, 다른 확장과 겹치면 커맨드가
|
||||
* `{식별자}/{키}` 로 지목합니다.
|
||||
*
|
||||
* `write` 축의 `callback` 은 클로저를 쓸 수 없습니다 — 코어 선언과 스키마를 공유하고
|
||||
* 코어 쪽은 `config:cache` 대상이므로, 형식을 `'Fqcn'`(invokable) 또는
|
||||
* `['Fqcn', 'method']` 로 통일합니다.
|
||||
*
|
||||
* @return array<string, array<string, mixed>> 프로파일 키 → 정의
|
||||
* [
|
||||
* 'consent_history' => [
|
||||
* 'type' => 'list', // list | screen | write | batch
|
||||
* 'label' => '동의 이력 목록',
|
||||
* 'table' => 'gdpr_user_consent_histories',
|
||||
* 'columns' => ['*'],
|
||||
* 'order' => [['created_at', 'desc']],
|
||||
* 'soft_delete' => false,
|
||||
* ],
|
||||
* ]
|
||||
*/
|
||||
public function getBenchmarkProfiles(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* 플러그인 설치 시 실행할 시더 클래스 목록 반환
|
||||
*
|
||||
|
||||
@@ -2,6 +2,11 @@
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use App\Benchmark\Axes\BatchAxisRunner;
|
||||
use App\Benchmark\Axes\ListAxisRunner;
|
||||
use App\Benchmark\Axes\ScreenAxisRunner;
|
||||
use App\Benchmark\Axes\WriteAxisRunner;
|
||||
use App\Console\Commands\BenchCommand;
|
||||
use App\Contracts\Extension\CacheInterface;
|
||||
use App\Contracts\Extension\ExtensionMiddlewareRegistryInterface;
|
||||
use App\Contracts\Extension\HookListenerInterface;
|
||||
@@ -119,9 +124,30 @@ class CoreServiceProvider extends ServiceProvider
|
||||
|
||||
$this->registerRepositoryBindings();
|
||||
$this->registerExtensionManagers();
|
||||
$this->registerBenchmarkAxes();
|
||||
// ActivityLogManager 제거됨 — Monolog 채널(config/logging.php 'activity')로 대체
|
||||
}
|
||||
|
||||
/**
|
||||
* 성능 계측 축 실행기를 등록합니다.
|
||||
*
|
||||
* 축이 늘어날 때 `g7:bench` 커맨드를 고치지 않고 여기에 실행기만 추가하면 되도록
|
||||
* 태그로 묶어 주입합니다. 실행기는 CLI 계측 시점에만 해석되므로 웹 요청 비용은 없습니다.
|
||||
*/
|
||||
private function registerBenchmarkAxes(): void
|
||||
{
|
||||
$this->app->tag([
|
||||
ListAxisRunner::class,
|
||||
ScreenAxisRunner::class,
|
||||
WriteAxisRunner::class,
|
||||
BatchAxisRunner::class,
|
||||
], 'benchmark.axes');
|
||||
|
||||
$this->app->when(BenchCommand::class)
|
||||
->needs('$runners')
|
||||
->giveTagged('benchmark.axes');
|
||||
}
|
||||
|
||||
/**
|
||||
* Extension(모듈/플러그인) PSR-4 오토로드를 등록합니다.
|
||||
*
|
||||
|
||||
Reference in new issue
Block a user