Skip to content

Commit

Permalink
test: benchmark tests for #459
Browse files Browse the repository at this point in the history
  • Loading branch information
g105b committed Mar 7, 2025
1 parent 2de9031 commit bc964a1
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 0 deletions.
34 changes: 34 additions & 0 deletions test/benchmark/run.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
echo "Running benchmarks...", PHP_EOL;

$fileList = glob(__DIR__ . "/scripts/*.php");
$outputList = [];

foreach($fileList as $phpFile) {
echo "Executing: ", basename($phpFile), PHP_EOL;

$output = null;
$returnVar = null;

exec("php " . escapeshellarg($phpFile), $output, $returnVar);

array_push($outputList, $output);

if($returnVar !== 0) {
echo "Error: Script ", basename($phpFile), " exited with code $returnVar", PHP_EOL;
}
}

echo "--------", PHP_EOL;

foreach($fileList as $i => $file) {
$basename = pathinfo($file, PATHINFO_BASENAME);
echo "$basename output: ";
$output = $outputList[$i];

foreach($output as $line) {
echo strlen($line) > 120 ? substr($line, 0, 120) . "..." : $line;
echo PHP_EOL;
}
echo PHP_EOL, "--------", PHP_EOL;
}
35 changes: 35 additions & 0 deletions test/benchmark/scripts/01-basic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php /** @noinspection PhpElementIsNotAvailableInCurrentPhpVersionInspection */
use Dom\HTMLDocument as NativeHTMLDocument;
use Gt\Dom\HTMLDocument;

require __DIR__ . "/../../../vendor/autoload.php";

$timeArray = [];

foreach([HTMLDocument::class, NativeHTMLDocument::class] as $domClassName) {
$timeStart = microtime(true);
echo "Using $domClassName", PHP_EOL;
$document = match($domClassName) {
HTMLDocument::class => new HTMLDocument(),
NativeHTMLDocument::class => NativeHTMLDocument::createFromString(HTMLDocument::DOCTYPE),
};

$element = $document->createElement("example-element");
$document->body->appendChild($element);

echo str_replace("\n", "", $document->saveHTML()), PHP_EOL;
$timeEnd = microtime(true);
$timeArray[$domClassName] = $timeEnd - $timeStart;
}

echo PHP_EOL;

foreach($timeArray as $className => $time) {
echo "$className: " . number_format($time, 4), PHP_EOL;
}

$percentDifference = abs(
($timeArray[array_keys($timeArray)[0]] - $timeArray[array_keys($timeArray)[1]])
/ max($timeArray) * 100
);
echo "Speed increase: " . number_format($percentDifference, 0) . "%", PHP_EOL;
37 changes: 37 additions & 0 deletions test/benchmark/scripts/02-create-many-elements.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php /** @noinspection PhpElementIsNotAvailableInCurrentPhpVersionInspection */
use Dom\HTMLDocument as NativeHTMLDocument;
use Gt\Dom\HTMLDocument;

require __DIR__ . "/../../../vendor/autoload.php";

$timeArray = [];

foreach([HTMLDocument::class, NativeHTMLDocument::class] as $domClassName) {
$timeStart = microtime(true);
echo "Using $domClassName", PHP_EOL;
$document = match($domClassName) {
HTMLDocument::class => new HTMLDocument(),
NativeHTMLDocument::class => NativeHTMLDocument::createFromString(HTMLDocument::DOCTYPE),
};

for($i = 0; $i < 10_000; $i++) {
$element = $document->createElement("example-element");
$document->body->appendChild($element);
}

echo str_replace("\n", "", $document->saveHTML()), PHP_EOL;
$timeEnd = microtime(true);
$timeArray[$domClassName] = $timeEnd - $timeStart;
}

echo PHP_EOL;

foreach($timeArray as $className => $time) {
echo "$className: " . number_format($time, 4), PHP_EOL;
}

$percentDifference = abs(
($timeArray[array_keys($timeArray)[0]] - $timeArray[array_keys($timeArray)[1]])
/ max($timeArray) * 100
);
echo "Speed increase: " . number_format($percentDifference, 0) . "%", PHP_EOL;
47 changes: 47 additions & 0 deletions test/benchmark/scripts/03-nested.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php /** @noinspection PhpElementIsNotAvailableInCurrentPhpVersionInspection */
use Dom\HTMLDocument as NativeHTMLDocument;
use Gt\Dom\HTMLDocument;

require __DIR__ . "/../../../vendor/autoload.php";

$timeArray = [];

foreach([HTMLDocument::class, NativeHTMLDocument::class] as $domClassName) {
$timeStart = microtime(true);
echo "Using $domClassName", PHP_EOL;
$document = match($domClassName) {
HTMLDocument::class => new HTMLDocument(),
NativeHTMLDocument::class => NativeHTMLDocument::createFromString(HTMLDocument::DOCTYPE),
};

for($i = 0; $i < 1_000; $i++) {
$element = $document->createElement("parent-element");
$document->body->appendChild($element);

$nested = null;
for($j = 0; $j < 100; $j++) {
$appendTo = $element;
if($nested) {
$appendTo = $nested;
}
$nested = $document->createElement("nested-element$j");
$appendTo->appendChild($nested);
}
}

echo str_replace("\n", "", $document->saveHTML()), PHP_EOL;
$timeEnd = microtime(true);
$timeArray[$domClassName] = $timeEnd - $timeStart;
}

echo PHP_EOL;

foreach($timeArray as $className => $time) {
echo "$className: " . number_format($time, 4), PHP_EOL;
}

$percentDifference = abs(
($timeArray[array_keys($timeArray)[0]] - $timeArray[array_keys($timeArray)[1]])
/ max($timeArray) * 100
);
echo "Speed increase: " . number_format($percentDifference, 0) . "%", PHP_EOL;

0 comments on commit bc964a1

Please sign in to comment.