Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions benchmarks/DataManagementBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace Infocyph\Pathwise\Benchmarks;

use Infocyph\Pathwise\Indexing\ChecksumIndexer;
use Infocyph\Pathwise\Retention\RetentionManager;
use Infocyph\Pathwise\Utils\FlysystemHelper;
use Infocyph\Pathwise\Utils\PathHelper;
use PhpBench\Attributes as Bench;

#[Bench\Iterations(5)]
#[Bench\Revs(50)]
#[Bench\BeforeMethods(['setUp'])]
#[Bench\AfterMethods(['tearDown'])]
final class DataManagementBench
{
private string $baseDir;

public function setUp(): void
{
$this->baseDir = PathHelper::normalize(sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'pathwise_data_ops_bench_' . uniqid('', true));
FlysystemHelper::createDirectory($this->baseDir);

// Build a deterministic mixed set with duplicates.
for ($i = 0; $i < 8; $i++) {
$content = $i < 4 ? 'duplicate-content' : 'unique-content-' . $i;
FlysystemHelper::write(
PathHelper::join($this->baseDir, sprintf('artifact_%02d.txt', $i)),
$content,
);
}
}

public function tearDown(): void
{
if (FlysystemHelper::directoryExists($this->baseDir)) {
FlysystemHelper::deleteDirectory($this->baseDir);
}
}

public function benchApplyRetentionScanOnly(): void
{
RetentionManager::apply($this->baseDir, null, null, 'mtime');
}

public function benchBuildChecksumIndex(): void
{
ChecksumIndexer::buildIndex($this->baseDir, 'sha256');
}

public function benchFindDuplicates(): void
{
ChecksumIndexer::findDuplicates($this->baseDir, 'sha256');
}
}
84 changes: 84 additions & 0 deletions benchmarks/FileAndDirectoryBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

declare(strict_types=1);

namespace Infocyph\Pathwise\Benchmarks;

use Infocyph\Pathwise\DirectoryManager\DirectoryOperations;
use Infocyph\Pathwise\FileManager\FileOperations;
use Infocyph\Pathwise\Utils\FlysystemHelper;
use Infocyph\Pathwise\Utils\PathHelper;
use PhpBench\Attributes as Bench;

#[Bench\Iterations(5)]
#[Bench\Revs(120)]
#[Bench\BeforeMethods(['setUp'])]
#[Bench\AfterMethods(['tearDown'])]
final class FileAndDirectoryBench
{
private string $baseDir;

private string $expectedChecksum;

private string $filePath;

public function setUp(): void
{
$this->baseDir = PathHelper::normalize(sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'pathwise_file_dir_bench_' . uniqid('', true));
FlysystemHelper::createDirectory($this->baseDir);

$logsDir = PathHelper::join($this->baseDir, 'logs');
$exportsDir = PathHelper::join($this->baseDir, 'exports');
FlysystemHelper::createDirectory($logsDir);
FlysystemHelper::createDirectory($exportsDir);

for ($i = 0; $i < 6; $i++) {
FlysystemHelper::write(
PathHelper::join($logsDir, sprintf('entry_%02d.log', $i)),
'benchmark-log-' . $i,
);
}

for ($i = 0; $i < 6; $i++) {
FlysystemHelper::write(
PathHelper::join($exportsDir, sprintf('report_%02d.txt', $i)),
str_repeat('report-line-' . $i . PHP_EOL, 10),
);
}

$this->filePath = PathHelper::join($this->baseDir, 'exports', 'report_00.txt');
$this->expectedChecksum = hash('sha256', (string) FlysystemHelper::read($this->filePath));
}

public function tearDown(): void
{
if (FlysystemHelper::directoryExists($this->baseDir)) {
FlysystemHelper::deleteDirectory($this->baseDir);
}
}

public function benchDirectoryFindTxtFiles(): void
{
$directory = new DirectoryOperations($this->baseDir);
$directory->find(['extension' => 'txt']);
}

public function benchDirectoryListContentsDetailed(): void
{
$directory = new DirectoryOperations($this->baseDir);
$directory->listContents(true);
}

public function benchDirectorySize(): void
{
$directory = new DirectoryOperations($this->baseDir);
$directory->size();
}

public function benchFileReadAndVerifyChecksum(): void
{
$file = new FileOperations($this->filePath);
$file->read();
$file->verifyChecksum($this->expectedChecksum, 'sha256');
}
}
52 changes: 52 additions & 0 deletions benchmarks/FlysystemHelperBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace Infocyph\Pathwise\Benchmarks;

use Infocyph\Pathwise\Utils\FlysystemHelper;
use Infocyph\Pathwise\Utils\PathHelper;
use PhpBench\Attributes as Bench;

#[Bench\Iterations(5)]
#[Bench\Revs(80)]
#[Bench\BeforeMethods(['setUp'])]
#[Bench\AfterMethods(['tearDown'])]
final class FlysystemHelperBench
{
private string $baseDir;

private string $filePath;

public function setUp(): void
{
$this->baseDir = PathHelper::normalize(sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'pathwise_bench_' . uniqid('', true));
FlysystemHelper::createDirectory($this->baseDir);

$this->filePath = PathHelper::join($this->baseDir, 'payload.txt');
FlysystemHelper::write($this->filePath, str_repeat('Pathwise benchmark payload' . PHP_EOL, 128));
}

public function tearDown(): void
{
if (FlysystemHelper::directoryExists($this->baseDir)) {
FlysystemHelper::deleteDirectory($this->baseDir);
}
}

public function benchChecksumSha256(): void
{
FlysystemHelper::checksum($this->filePath, 'sha256');
}

public function benchGetMetadataSizeAndMtime(): void
{
FlysystemHelper::size($this->filePath);
FlysystemHelper::lastModified($this->filePath);
}

public function benchReadFileContents(): void
{
FlysystemHelper::read($this->filePath);
}
}
42 changes: 42 additions & 0 deletions benchmarks/PathHelperBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Infocyph\Pathwise\Benchmarks;

use Infocyph\Pathwise\Utils\PathHelper;
use PhpBench\Attributes as Bench;

#[Bench\Iterations(6)]
#[Bench\Revs(1500)]
final class PathHelperBench
{
public function benchJoinMultipleSegments(): void
{
PathHelper::join(
'/var',
'www',
'project',
'storage',
'uploads',
'../uploads',
'2026',
'05',
'13',
'artifact.zip',
);
}

public function benchNormalizeDeepRelativePath(): void
{
PathHelper::normalize('/var/www/project/src/../tests/./fixtures/../../src/Utils/PathHelper.php');
}

public function benchRelativePathComputation(): void
{
PathHelper::relativePath(
'/var/www/project/modules/pathwise/src',
'/var/www/project/modules/pathwise/tests/Feature/UploadProcessorTest.php',
);
}
}
98 changes: 98 additions & 0 deletions benchmarks/StreamHandlerBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

declare(strict_types=1);

namespace Infocyph\Pathwise\Benchmarks;

use Infocyph\Pathwise\StreamHandler\DownloadProcessor;
use Infocyph\Pathwise\StreamHandler\UploadProcessor;
use Infocyph\Pathwise\Utils\FlysystemHelper;
use Infocyph\Pathwise\Utils\PathHelper;
use PhpBench\Attributes as Bench;

#[Bench\Iterations(4)]
#[Bench\Revs(40)]
#[Bench\BeforeMethods(['setUp'])]
#[Bench\AfterMethods(['tearDown'])]
final class StreamHandlerBench
{
private string $baseDir;

private string $downloadFilePath;

private DownloadProcessor $downloadProcessor;

private string $uploadDir;

private UploadProcessor $uploadProcessor;

public function setUp(): void
{
$this->baseDir = PathHelper::normalize(sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'pathwise_stream_bench_' . uniqid('', true));
$this->uploadDir = PathHelper::join($this->baseDir, 'uploads');
$downloadsDir = PathHelper::join($this->baseDir, 'downloads');

FlysystemHelper::createDirectory($this->uploadDir);
FlysystemHelper::createDirectory($downloadsDir);

$this->downloadFilePath = PathHelper::join($downloadsDir, 'dataset.txt');
FlysystemHelper::write($this->downloadFilePath, str_repeat('download-content' . PHP_EOL, 64));

$this->uploadProcessor = new UploadProcessor();
$this->uploadProcessor->setDirectorySettings($this->uploadDir, false);
$this->uploadProcessor->setValidationSettings([], 2 * 1024 * 1024);

$this->downloadProcessor = new DownloadProcessor();
$this->downloadProcessor->setAllowedRoots([$this->baseDir]);
}

public function tearDown(): void
{
if (FlysystemHelper::directoryExists($this->baseDir)) {
FlysystemHelper::deleteDirectory($this->baseDir);
}
}

public function benchPrepareDownloadMetadata(): void
{
$this->downloadProcessor->prepareDownload($this->downloadFilePath, 'dataset.txt', null);
}

public function benchProcessUpload(): void
{
$tmpUpload = tempnam(sys_get_temp_dir(), 'pathwise_upload_bench_');
if (!is_string($tmpUpload)) {
return;
}

file_put_contents($tmpUpload, str_repeat('upload-payload', 128));

try {
$destination = $this->uploadProcessor->processUpload([
'error' => UPLOAD_ERR_OK,
'size' => filesize($tmpUpload) ?: 0,
'tmp_name' => $tmpUpload,
'name' => 'payload.txt',
]);
FlysystemHelper::delete($destination);
} finally {
if (is_file($tmpUpload)) {
unlink($tmpUpload);
}
}
}

public function benchStreamDownload(): void
{
$output = fopen('php://temp', 'w+b');
if (!is_resource($output)) {
return;
}

try {
$this->downloadProcessor->streamDownload($this->downloadFilePath, $output);
} finally {
fclose($output);
}
}
}