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 15, 2024
1 parent af73e22 commit 1876e74
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/JsonConverterBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace League\Csv;

use PhpBench\Attributes as Bench;

final class JsonConverterBench
{
#[Bench\OutputTimeUnit('seconds')]
#[Bench\Assert('mode(variant.mem.peak) < 2097152'), Bench\Assert('mode(variant.time.avg) < 10000000')]
public function benchConverting10000CSVRowsUsingJsonConverter(): void
{
$document = Reader::createFromPath(dirname(__DIR__).'/test_files/prenoms.csv');
$document->setHeaderOffset(0);
$document->setDelimiter(';');
CharsetConverter::addTo($document, 'iso-8859-15', 'utf-8');

$bytes = JsonConverter::create()->save($document, ($tmpFile = tmpfile()));

assert($bytes === filesize(stream_get_meta_data($tmpFile)['uri']));
}

#[Bench\OutputTimeUnit('seconds')]
#[Bench\Assert('mode(variant.mem.peak) <= 7641688'), Bench\Assert('mode(variant.time.avg) < 10000000')]
public function benchConverting10000CSVRowsUsingJsonEncode(): void
{
$document = Reader::createFromPath(dirname(__DIR__).'/test_files/prenoms.csv');
$document->setHeaderOffset(0);
$document->setDelimiter(';');
CharsetConverter::addTo($document, 'iso-8859-15', 'utf-8');

$tmpFile = tmpfile();
$filename = stream_get_meta_data($tmpFile)['uri'];

$bytes = file_put_contents($filename, json_encode($document));

assert($bytes === filesize($filename));
}
}

0 comments on commit 1876e74

Please sign in to comment.