Skip to content

Commit b4e1266

Browse files
committed
Enhance runtime mapping to use environment ports and update tests
- Rework `RuntimeMatrixRunner` to dynamically build `runtimeMap` using environment-defined ports. - Add `testRuntimeMapUsesProjectEnvPorts` to verify runtime map configuration. - Simplify `MetadataEngineTest` with mock approach for Redis exception handling. - Include environment variable settings in GitHub workflow for benchmarking.
1 parent 530e873 commit b4e1266

4 files changed

Lines changed: 63 additions & 27 deletions

File tree

.github/workflows/runtime-benchmark-coverage.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ jobs:
1111
runtime-matrix-and-coverage:
1212
runs-on: ubuntu-latest
1313
timeout-minutes: 120
14+
env:
15+
HOST_PORT: '8008'
16+
HOST_PORT_SWOOLE: '8011'
1417

1518
steps:
1619
- name: Checkout

src/base/benchmark/RuntimeMatrixRunner.php

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ class RuntimeMatrixRunner
99
/**
1010
* @var array<string, array{service:string,base_url:string}>
1111
*/
12-
private array $runtimeMap = [
13-
'php-s' => ['service' => 'php', 'base_url' => 'http://127.0.0.1:8008'],
14-
'swoole' => ['service' => 'php-swoole', 'base_url' => 'http://127.0.0.1:8011'],
15-
];
12+
private array $runtimeMap;
1613

1714
/**
1815
* @var array<int, array{name:string,concurrency:int,requests:int}>
@@ -41,6 +38,7 @@ public function __construct(
4138
$this->composeFile = $composeFile ?: $this->projectRoot . DIRECTORY_SEPARATOR . 'docker-compose.yml';
4239
$this->configFile = $configFile ?: $this->projectRoot . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'config.json';
4340
$this->outputDir = $outputDir ?: $this->projectRoot . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . 'benchmark' . DIRECTORY_SEPARATOR . 'runtime-matrix';
41+
$this->runtimeMap = $this->buildRuntimeMap();
4442
$this->profiles = $profiles ?: [
4543
['name' => 'L1', 'concurrency' => 1, 'requests' => 2000],
4644
['name' => 'L2', 'concurrency' => 20, 'requests' => 4000],
@@ -570,4 +568,38 @@ private function resolveCacheMode(bool $opcache, bool $redis): string
570568
}
571569
return 'NONE';
572570
}
571+
572+
/**
573+
* @return array<string, array{service:string,base_url:string}>
574+
*/
575+
private function buildRuntimeMap(): array
576+
{
577+
return [
578+
'php-s' => [
579+
'service' => 'php',
580+
'base_url' => 'http://127.0.0.1:' . $this->envValue('HOST_PORT', '8001'),
581+
],
582+
'swoole' => [
583+
'service' => 'php-swoole',
584+
'base_url' => 'http://127.0.0.1:' . $this->envValue('HOST_PORT_SWOOLE', '8011'),
585+
],
586+
];
587+
}
588+
589+
private function envValue(string $name, string $default): string
590+
{
591+
$value = getenv($name);
592+
if (is_string($value) && $value !== '') {
593+
return $value;
594+
}
595+
596+
$envFile = $this->projectRoot . DIRECTORY_SEPARATOR . '.env';
597+
if (!is_file($envFile)) {
598+
return $default;
599+
}
600+
601+
$values = parse_ini_file($envFile, false, INI_SCANNER_RAW);
602+
$fileValue = is_array($values) ? ($values[$name] ?? null) : null;
603+
return is_string($fileValue) && $fileValue !== '' ? $fileValue : $default;
604+
}
573605
}

tests/base/benchmark/RuntimeMatrixRunnerTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,21 @@ public function testBuildScenariosReturnsExpectedMatrixSize(): void
4343
$this->assertCount(16, array_unique($ids));
4444
}
4545

46+
public function testRuntimeMapUsesProjectEnvPorts(): void
47+
{
48+
file_put_contents(
49+
$this->tmpRoot . DIRECTORY_SEPARATOR . '.env',
50+
"HOST_PORT=19008\nHOST_PORT_SWOOLE=19011\n"
51+
);
52+
53+
$runner = $this->newFakeRunner();
54+
$property = new \ReflectionProperty(RuntimeMatrixRunner::class, 'runtimeMap');
55+
$runtimeMap = $property->getValue($runner);
56+
57+
$this->assertSame('http://127.0.0.1:19008', $runtimeMap['php-s']['base_url']);
58+
$this->assertSame('http://127.0.0.1:19011', $runtimeMap['swoole']['base_url']);
59+
}
60+
4661
public function testRunWritesReportsAndRestoresConfig(): void
4762
{
4863
$runner = $this->newFakeRunner();

tests/base/type/helper/MetadataEngineTest.php

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -428,32 +428,18 @@ public function testRedisAndLockHelpersHandleExceptionsAndOpcacheDrop(): void
428428
Config::save($override, []);
429429
Config::getInstance()->loadConfigData(true);
430430

431-
$probe = new class extends MetadataEngine {
431+
$redisStub = $this->createMock(\Redis::class);
432+
$redisStub->method('get')->willThrowException(new \RedisException('boom-get'));
433+
$redisStub->method('setex')->willThrowException(new \RedisException('boom-setex'));
434+
$redisStub->method('del')->willThrowException(new \RedisException('boom-del'));
435+
$redisStub->method('set')->willThrowException(new \RedisException('boom-set'));
436+
437+
$probe = new class($redisStub) extends MetadataEngine {
432438
private \Redis $redisStub;
433439

434-
public function __construct()
440+
public function __construct(\Redis $redisStub)
435441
{
436-
$this->redisStub = new class extends \Redis {
437-
public function get(string $key): mixed
438-
{
439-
throw new \RedisException('boom-get');
440-
}
441-
442-
public function setex(string $key, int $expire, mixed $value): void
443-
{
444-
throw new \RedisException('boom-setex');
445-
}
446-
447-
public function del(array|string $key, string ...$other_keys): \Redis|int|false
448-
{
449-
throw new \RedisException('boom-del');
450-
}
451-
452-
public function set(string $key, mixed $value, mixed $options = null): \Redis|string|bool
453-
{
454-
throw new \RedisException('boom-set');
455-
}
456-
};
442+
$this->redisStub = $redisStub;
457443
}
458444

459445
protected function redisClient(): ?\Redis

0 commit comments

Comments
 (0)