Skip to content

Commit

Permalink
Adding JsonConverter benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Oct 16, 2024
1 parent af73e22 commit b6d493b
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions src/JsonConverterBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

declare(strict_types=1);

namespace League\Csv;

use PhpBench\Attributes as Bench;
use const SEEK_END;

final class JsonConverterBench
{
#[Bench\OutputTimeUnit('seconds')]
#[Bench\Assert('mode(variant.mem.peak) <= 8000000'), Bench\Assert('mode(variant.time.avg) < 10000000')]
public function benchUsingJsonEncode(): void
{
$document = $this->getDocument();
$tmpFile = tmpfile();
$bytes = fwrite($tmpFile, json_encode($document)); /* @phpstan-ignore-line */

fseek($tmpFile, 0, SEEK_END);
assert($bytes === ftell($tmpFile));
}

#[Bench\OutputTimeUnit('seconds')]
#[Bench\Assert('mode(variant.mem.peak) < 4000000'), Bench\Assert('mode(variant.time.avg) < 10000000')]
public function benchUsingDefaultJsonConverter(): void
{
$document = $this->getDocument();
$tmpFile = tmpfile();
$bytes = JsonConverter::create()->save($document, $tmpFile);

fseek($tmpFile, 0, SEEK_END);
assert($bytes === ftell($tmpFile));
}

#[Bench\OutputTimeUnit('seconds')]
#[Bench\Assert('mode(variant.mem.peak) < 4000000'), Bench\Assert('mode(variant.time.avg) < 10000000')]
public function benchUsingJsonConverterWithForceObject(): void
{
$document = $this->getDocument();
$tmpFile = tmpfile();
$bytes = JsonConverter::create()->withForceObject()->save($document, $tmpFile);

fseek($tmpFile, 0, SEEK_END);
assert($bytes === ftell($tmpFile));
}

#[Bench\OutputTimeUnit('seconds')]
#[Bench\Assert('mode(variant.mem.peak) < 4000000'), Bench\Assert('mode(variant.time.avg) < 10000000')]
public function benchUsingJsonConverterWithPrettyPrint(): void
{
$document = $this->getDocument();
$tmpFile = tmpfile();
$bytes = JsonConverter::create()->withPrettyPrint()->save($document, $tmpFile);

fseek($tmpFile, 0, SEEK_END);
assert($bytes === ftell($tmpFile));
}

#[Bench\OutputTimeUnit('seconds')]
#[Bench\Assert('mode(variant.mem.peak) < 4000000'), Bench\Assert('mode(variant.time.avg) < 10000000')]
public function benchUsingJsonConverterWithSmallChunkSize(): void
{
$document = $this->getDocument();
$tmpFile = tmpfile();
$bytes = JsonConverter::create()->chunkSize(1)->save($document, $tmpFile);

fseek($tmpFile, 0, SEEK_END);
assert($bytes === ftell($tmpFile));
}

private function getDocument(): Reader
{
$document = Reader::createFromPath(dirname(__DIR__).'/test_files/prenoms.csv');
$document->setHeaderOffset(0);
$document->setDelimiter(';');
CharsetConverter::addTo($document, 'iso-8859-15', 'utf-8');

return $document;
}
}

0 comments on commit b6d493b

Please sign in to comment.