storage_test_unit()
Description
Perform one workload unit. Stage cursors and file sizes are server-owned and bounded.Parameters
| Column | Type | Default | Description |
|---|---|---|---|
| &$state: | array | ||
| $state | array | Mutable state, persisted after each request. |
Return
| bool | True when the current stage is complete. |
Location
include/storage_test_functions.php lines 204 to 286
Definition
function storage_test_unit(array &$state): bool
{
$manifest = storage_test_manifest();
$files = array_keys($manifest['files']);
$stage = $state['stage'];
$i = $state['cursor'];
if ($stage === 'directories' || $stage === 'enumerate') {
if ($i >= count($manifest['dirs'])) {
return true;
}
$path = storage_test_path($state, $manifest['dirs'][$i]);
storage_test_measure($state, $stage, static function () use ($stage, $path): int {
if ($stage === 'directories' && !is_dir(dirname($path))) {
return 0;
}
if ($stage === 'enumerate' && !is_dir($path)) {
return 0;
}
if ($stage === 'directories' ? !mkdir($path, 0700) : scandir($path) === false) {
throw new RuntimeException('storage_test_io_error');
}
return 1;
});
if (is_dir($path)) {
$state['last_operation'] = ['kind' => $stage === 'directories'
? 'directory_created' : 'directory_enumerated', 'path' => $manifest['dirs'][$i]];
}
} elseif ($stage === 'write' || $stage === 'read') {
if ($i >= 64) {
return true;
}
// Four 16 MiB assets, streamed in 1 MiB chunks.
$file = 'asset-' . intdiv($i, 16) . '.bin';
$offset = ($i % 16) * 1024 * 1024;
if ($stage === 'read' && ($state['sizes'][$file] ?? 0) < $offset + 1024 * 1024) {
++$state['cursor'];
return false;
}
storage_test_transfer($state, $file, $offset, 1024 * 1024, $stage === 'write', $stage);
if ($stage === 'write') {
$state['sizes'][$file] = $offset + 1024 * 1024;
}
} elseif (in_array($stage, ['small_create', 'small_read', 'stat'], true)) {
if ($i >= 96) {
return true;
}
$file = $files[$i + 4];
$path = storage_test_path($state, $file);
if ($stage === 'small_create') {
// If directory creation hit its budget, do not generate files in missing directories.
if (is_dir(dirname($path))) {
storage_test_transfer($state, $file, 0, $manifest['files'][$file], true, $stage);
$state['sizes'][$file] = $manifest['files'][$file];
++$state['small_created'];
}
} elseif (isset($state['sizes'][$file])) {
if ($stage === 'small_read') {
storage_test_transfer($state, $file, 0, $state['sizes'][$file], false, $stage);
++$state['small_read'];
} else {
storage_test_measure($state, 'stat', static function () use ($path): int {
clearstatcache(true, $path);
if (stat($path) === false) {
throw new RuntimeException('storage_test_io_error');
}
return 1;
});
$state['last_operation'] = ['kind' => 'metadata_read', 'path' => $file];
}
}
} elseif ($stage === 'random') {
$assets = array_filter($state['sizes'], static fn($size, $name) => str_starts_with($name, 'asset-')
&& $size >= 65536, ARRAY_FILTER_USE_BOTH);
if ($i >= 64 || $assets === []) {
return true;
}
$file = array_rand($assets);
$offset = random_int(0, intdiv($assets[$file] - 65536, 4096)) * 4096;
storage_test_transfer($state, $file, $offset, 65536, false, 'random');
}
++$state['cursor'];
return false;
}
This article was last updated 13th November 2026 16:05 Europe/London time based on the source file dated 13th November 2026 11:40 Europe/London time.