storage_test_transfer()
Description
Transfer one bounded chunk, checking short reads/writes and closing the handle on every path.Data generation is outside the measured operation. Writes include fflush() and close, not fsync().
Parameters
| Column | Type | Default | Description |
|---|---|---|---|
| &$state | array | ||
| $relative | string | Manifest filename. | |
| $offset | int | Byte offset. | |
| $length | int | Expected transfer size (at most 1 MiB). | |
| $write | bool | Write new random data instead of reading. | |
| $metric : | string | storage_test_path$state | |
| $data | |||
| $metric | string | Measurement key. | |
| function | static | ||
| $path | |||
| $state | array | Mutable benchmark state. |
Return
| void |
Location
include/storage_test_functions.php lines 147 to 196
Definition
function storage_test_transfer(
array &$state,
string $relative,
int $offset,
int $length,
bool $write,
string $metric
): void {
$path = storage_test_path($state, $relative);
$data = $write ? random_bytes($length) : '';
storage_test_measure($state, $metric, static function () use (
&$state,
$path,
$offset,
$length,
$write,
$data
): int {
if ($write && $offset > 0 && filesize($path) !== $offset) {
throw new RuntimeException('storage_test_io_error');
}
$handle = fopen($path, $write ? ($offset === 0 ? 'xb' : 'r+b') : 'rb');
if ($handle === false) {
throw new RuntimeException('storage_test_io_error');
}
try {
if (fseek($handle, $offset) !== 0) {
throw new RuntimeException('storage_test_io_error');
}
$done = 0;
while ($done < $length) {
$result = $write ? fwrite($handle, substr($data, $done)) : fread($handle, $length - $done);
$bytes = $write ? $result : ($result === false ? false : strlen($result));
if ($bytes === false || $bytes === 0) {
throw new RuntimeException('storage_test_io_error');
}
$done += $bytes;
$state[$write ? 'written' : 'read'] += $bytes;
}
if ($write && !fflush($handle)) {
throw new RuntimeException('storage_test_io_error');
}
return $done;
} finally {
fclose($handle);
}
});
$state['last_operation'] = ['kind' => $write ? 'file_written' : 'file_read',
'path' => $relative, 'bytes' => $length];
}
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.