diff --git a/packages/runtime-playground/src/bench-command-handlers.ts b/packages/runtime-playground/src/bench-command-handlers.ts index b2085c81..7a25f476 100644 --- a/packages/runtime-playground/src/bench-command-handlers.ts +++ b/packages/runtime-playground/src/bench-command-handlers.ts @@ -1666,6 +1666,7 @@ function wp_codebox_bench_assert_required_observations(array $workload, array $p } function wp_codebox_bench_run_configured_workload(array $workload, string $plugin_path) { + static $php_file_results = array(); $steps = wp_codebox_bench_workload_run_steps($workload); $payload = array('metrics' => array(), 'metadata' => array(), 'artifacts' => array(), 'steps' => array(), 'diagnostics' => array()); if (isset($workload['metadata']) && is_array($workload['metadata'])) { @@ -1679,7 +1680,10 @@ function wp_codebox_bench_run_configured_workload(array $workload, string $plugi if ($type === 'php') { if (isset($step['file']) && is_string($step['file'])) { $file = $plugin_path . '/' . ltrim($step['file'], '/'); - $callable = require $file; + if (!array_key_exists($file, $php_file_results)) { + $php_file_results[$file] = require $file; + } + $callable = $php_file_results[$file]; $result = is_callable($callable) ? $callable() : $callable; } else { $result = null; diff --git a/tests/bench-command-step-behavior.test.ts b/tests/bench-command-step-behavior.test.ts index 747b82b1..bcede462 100644 --- a/tests/bench-command-step-behavior.test.ts +++ b/tests/bench-command-step-behavior.test.ts @@ -188,6 +188,38 @@ assert.match(requiredObservationGate[0], /"step_count":0/) assert.match(requiredObservationGate[1], /observation:rest-db-query-profiler/) assert.equal(requiredObservationGate[2], "report") +const configuredPhpFileIterations = await withTempDir("wp-codebox-configured-php-file-", async (directory) => { + const workloadFile = join(directory, "workload.php") + const phpTestFile = join(directory, "configured-php-file.php") + await writeFile( + workloadFile, + ` array('calls' => $GLOBALS['wp_codebox_test_call_count'])); +} +return 'wp_codebox_test_configured_workload_result'; +`, + ) + await writeFile( + phpTestFile, + ` array(array('type' => 'php', 'file' => 'workload.php'))); +wp_codebox_bench_run_configured_workload($workload, ${JSON.stringify(directory)}); +$payload = wp_codebox_bench_run_configured_workload($workload, ${JSON.stringify(directory)}); +echo json_encode(array( + 'loads' => $GLOBALS['wp_codebox_test_load_count'], + 'calls' => $GLOBALS['wp_codebox_test_call_count'], + 'metrics' => $payload['metrics'], +), JSON_UNESCAPED_SLASHES); +`, + ) + return runPhpFileJson<{ loads: number; calls: number; metrics: { calls: number } }>(phpTestFile) +}) +assert.deepEqual(configuredPhpFileIterations, { loads: 1, calls: 2, metrics: { calls: 2 } }) + const externalHttpGuardrailPayload = await withTempDir("wp-codebox-external-http-guardrail-", async (directory) => { const phpTestFile = join(directory, "external-http-guardrail.php") await writeFile(