Files
Gnuboard7/tests/Unit/Benchmark/BenchmarkProfileTest.php
HeuJung 5f4a733b3b 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종 전건 측정.
2026-08-02 14:44:33 +09:00

154 lines
5.7 KiB
PHP

<?php
namespace Tests\Unit\Benchmark;
use App\Benchmark\DTO\BenchmarkProfile;
use App\Enums\BenchmarkAxis;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;
/**
* 계측 프로파일 VO 및 축 Enum 계약 단위 테스트.
*
* 축이 늘어나도 VO 를 고치지 않는다는 설계(축 고유 옵션은 `options` 에 남김)와, 변경 판정이
* 어디서 결정되는지(축 기본값 → HTTP 메서드 → 선언 순)를 고정한다.
*/
class BenchmarkProfileTest extends TestCase
{
/**
* 프로파일을 만듭니다.
*
* @param BenchmarkAxis $axis 계측 축
* @param array<string, mixed> $options 축 고유 옵션
* @param string $sourceIdentifier 출처 식별자
* @return BenchmarkProfile 프로파일
*/
private function profile(BenchmarkAxis $axis, array $options = [], string $sourceIdentifier = 'core'): BenchmarkProfile
{
return new BenchmarkProfile(
key: 'sample',
axis: $axis,
sourceKind: $sourceIdentifier === 'core' ? 'core' : 'module',
sourceIdentifier: $sourceIdentifier,
options: $options,
label: '샘플',
);
}
/**
* 정규화 키는 출처와 키를 합쳐 전역 고유해집니다.
*/
#[Test]
public function 정규화_키는_출처를_포함한다(): void
{
$this->assertSame('core/sample', $this->profile(BenchmarkAxis::ListQuery)->qualifiedKey());
$this->assertSame(
'vendor-mod/sample',
$this->profile(BenchmarkAxis::ListQuery, [], 'vendor-mod')->qualifiedKey()
);
}
/**
* 축 고유 옵션은 기본값과 함께 읽힙니다.
*/
#[Test]
public function 옵션은_기본값과_함께_읽힌다(): void
{
$profile = $this->profile(BenchmarkAxis::ListQuery, ['table' => 'users']);
$this->assertSame('users', $profile->option('table'));
$this->assertSame(['id'], $profile->option('columns', ['id']));
$this->assertNull($profile->option('absent'));
}
/**
* 목록/화면 축은 기본적으로 데이터를 변경하지 않고, 쓰기/배치 축은 변경합니다.
*/
#[Test]
public function 축_기본_변경_판정(): void
{
$this->assertFalse($this->profile(BenchmarkAxis::ListQuery)->mutates());
$this->assertFalse($this->profile(BenchmarkAxis::Screen)->mutates());
$this->assertTrue($this->profile(BenchmarkAxis::Write)->mutates());
$this->assertTrue($this->profile(BenchmarkAxis::Batch)->mutates());
}
/**
* 화면 축의 변경 판정은 선언된 HTTP 메서드를 따릅니다.
*
* @effects screen_mutation_verdict_follows_http_method
*/
#[Test]
public function 화면_축은_http_메서드로_변경을_판정한다(): void
{
$this->assertFalse($this->profile(BenchmarkAxis::Screen, ['method' => 'GET'])->mutates());
// 대소문자와 무관하게 판정한다
$this->assertFalse($this->profile(BenchmarkAxis::Screen, ['method' => 'get'])->mutates());
$this->assertTrue($this->profile(BenchmarkAxis::Screen, ['method' => 'POST'])->mutates());
$this->assertTrue($this->profile(BenchmarkAxis::Screen, ['method' => 'DELETE'])->mutates());
}
/**
* 명시 선언이 축 기본값과 메서드 판정을 모두 덮어씁니다.
*
* 읽기 전용 배치(리포트 산출 등)를 `--allow-write` 없이 재려면 이 경로가 필요합니다.
*
* @effects explicit_mutating_declaration_overrides_axis_default
*/
#[Test]
public function 명시_mutating_선언이_기본값을_덮는다(): void
{
$this->assertFalse($this->profile(BenchmarkAxis::Batch, ['mutating' => false])->mutates());
$this->assertTrue($this->profile(BenchmarkAxis::ListQuery, ['mutating' => true])->mutates());
$this->assertFalse(
$this->profile(BenchmarkAxis::Screen, ['method' => 'POST', 'mutating' => false])->mutates()
);
}
/**
* 직렬화 결과가 리포트/JSON 이 기대하는 키를 담습니다.
*/
#[Test]
public function 직렬화가_약속된_키를_담는다(): void
{
$payload = $this->profile(BenchmarkAxis::ListQuery, ['table' => 'users'])->toArray();
foreach (['key', 'qualified_key', 'axis', 'source', 'label', 'options'] as $key) {
$this->assertArrayHasKey($key, $payload);
}
$this->assertSame('list', $payload['axis']);
$this->assertSame(['kind' => 'core', 'identifier' => 'core'], $payload['source']);
$this->assertSame(['table' => 'users'], $payload['options']);
}
/**
* 축마다 필수 옵션이 대안 그룹으로 선언됩니다.
*
* 화면 축이 라우트명 또는 URI 중 하나만 있으면 되는 성질이 이 구조에 담깁니다.
*
* @effects screen_axis_accepts_route_or_uri_alternative
*/
#[Test]
public function 축별_필수_옵션은_대안_그룹으로_선언된다(): void
{
$this->assertSame([['table']], BenchmarkAxis::ListQuery->requiredOptions());
$this->assertSame([['route', 'uri']], BenchmarkAxis::Screen->requiredOptions());
$this->assertSame([['callback']], BenchmarkAxis::Write->requiredOptions());
$this->assertSame([['command']], BenchmarkAxis::Batch->requiredOptions());
}
/**
* 축 값 목록과 라벨이 정의됩니다.
*/
#[Test]
public function 축_값과_라벨이_정의된다(): void
{
$this->assertSame(['list', 'screen', 'write', 'batch'], BenchmarkAxis::values());
foreach (BenchmarkAxis::cases() as $axis) {
$this->assertNotSame('', $axis->label());
}
}
}