Skip to content

Commit

Permalink
Merge pull request #19 from 8fold/comparisons
Browse files Browse the repository at this point in the history
add: Performance tests
  • Loading branch information
joshbruce committed Feb 26, 2024
2 parents d0c575a + a1080a7 commit 6cde59f
Show file tree
Hide file tree
Showing 7 changed files with 630 additions and 7 deletions.
33 changes: 26 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,36 @@ Output:
<input required>
```

## Details
## Performance

See [8fold XML Builder](https://github.com/8fold/php-xml-builder#readme) for
details.
The `/comparisons` folder can be used to run performance tests locally to compare the same output using:

1. straight HTML,
2. standard PHP,
3. PHP HTML Builder alongside standard PHP (the Element class),
4. PHP HTML Builder only (the Document class), and
5. PHP HTML Builder only using PSR-4 autoloading.

These results are from February 26th, 2024:

## Usage (advanced)
|Configuration |Average time in milliseconds |Median time in milliseconds |
|:---|---:|---:|
|1 |0.0399683 |0.0017415 |
|2 |0.0953241 |0.0160480 |
|3 |1.6026525 |1.4603320 |
|4 |1.7849769 |1.7918970 |
|5 |8.9957415 |8.8755520 |

TODO
Things worth noting (as rough interpretations):

HTML Builder has the ability to help create valid HTML documents based on the
HTML specification.
1. PHP being used as a template engine has a negligible impact on speed.
2. Using `require_once` without PSR-4 autoloading decreases speed by roughly 1 millisecond; however, once the code is made available, very little additional cost seems to be present.
3. Using PSR-4 decreases speed by roughly 6 milliseconds; however, ensures everything is present and available.

## Details

See [8fold XML Builder](https://github.com/8fold/php-xml-builder#readme) for more
details.

## Other

Expand Down
74 changes: 74 additions & 0 deletions comparisons/html/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
$start = hrtime(true);
?>
<!doctype html>
<html lang="en">
<head>
<title>Page title here</title>
<meta charset="utf-8">
</head>
<body>
<h1>Page title here</h1>
<p><a href="#bottom">time</a></p>
<artcle>
<section>
<h2>Blog title 1</h2>
<p>Brief description of content for blog post 1</p>
<p><a href="#">Read blog post 1</a></p>
</section>
<section>
<h2>Blog title 2</h2>
<p>Brief description of content for blog post 2</p>
<p><a href="#">Read blog post 2</a></p>
</section>
<section>
<h2>Blog title 3</h2>
<p>Brief description of content for blog post 3</p>
<p><a href="#">Read blog post 3</a></p>
</section>
<section>
<h2>Blog title 4</h2>
<p>Brief description of content for blog post 4</p>
<p><a href="#">Read blog post 4</a></p>
</section>
<section>
<h2>Blog title 5</h2>
<p>Brief description of content for blog post 5</p>
<p><a href="#">Read blog post 5</a></p>
</section>
<section>
<h2>Blog title 6</h2>
<p>Brief description of content for blog post 6</p>
<p><a href="#">Read blog post 6</a></p>
</section>
<section>
<h2>Blog title 7</h2>
<p>Brief description of content for blog post 7</p>
<p><a href="#">Read blog post 7</a></p>
</section>
<section>
<h2>Blog title 8</h2>
<p>Brief description of content for blog post 8</p>
<p><a href="#">Read blog post 8</a></p>
</section>
<section>
<h2>Blog title 9</h2>
<p>Brief description of content for blog post 9</p>
<p><a href="#">Read blog post 9</a></p>
</section>
<section>
<h2>Blog title 10</h2>
<p>Brief description of content for blog post 10</p>
<p><a href="#">Read blog post 10</a></p>
</section>
</artcle>
<?php
$end = hrtime(true);

$elapsed = $end - $start;
$ms = $elapsed/1e+6;

print('<p id="bottom"><b>' . $ms . 'ms</b></p>');
?>
</body>
</html>
21 changes: 21 additions & 0 deletions comparisons/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<ol>
<li>Start PHP server and point to `/comparisons`</li>
<li>Launch browser to the root address</li>
<li>Click link of page to be tested</li>
<li>Note displayed render time in milliseconds</li>
<li>Refresh browser 11 times; making not of render times (should result in 12 times)</li>
<li>Calculate average and median</li>
</ol>
<dl>
<dt><a href="/html/">Plain HTML</a></dt>
<dd>Has minimal PHP for measuring time</dd>
<dt><a href="/php/">PHP</a></dt>
<dd>Replicates the Plain HTML using common patterns using PHP as a template engine.</dd>
<dt><a href="/php-html-builder-element/">PHP HTML Builder (element)</a></dt>
<dd>Replicates the PHP variant using PHP HTML Builder Element to generate the `article` and `section` elements. Does not use Composer autoloading.</dd>
<dt><a href="/php-html-builder-document/">PHP HTML Builder (document)</a></dt>
<dd>Replicates the PHP variant using PHP HTML Builder Document to generate the whole HTML output. Does not use Composer autoloading.</dd>
<dt><a href="/php-html-builder-document-psr4/">PHP HTML Builder (document) w/ PSR-4</a></dt>
<dd>Replicates the PHP HTML Builder (document) variant using PSR-4 autoloading via Composer.</dd>
</dl>

122 changes: 122 additions & 0 deletions comparisons/php-html-builder-document-psr4/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

$start = hrtime(true);

require_once(__DIR__ . '/../../vendor/autoload.php');

$pageTitle = "Page title here";

$posts = [
[
"title" => "Blog title 1",
"description" => "Brief description for blog post 1",
"url" => [
"href" => "#",
"text" => "Read blog post 1"
]
],
[
"title" => "Blog title 2",
"description" => "Brief description for blog post 2",
"url" => [
"href" => "#",
"text" => "Read blog post 2"
]
],
[
"title" => "Blog title 3",
"description" => "Brief description for blog post 3",
"url" => [
"href" => "#",
"text" => "Read blog post 3"
]
],
[
"title" => "Blog title 4",
"description" => "Brief description for blog post 4",
"url" => [
"href" => "#",
"text" => "Read blog post 4"
]
],
[
"title" => "Blog title 5",
"description" => "Brief description for blog post 5",
"url" => [
"href" => "#",
"text" => "Read blog post 5"
]
],
[
"title" => "Blog title 6",
"description" => "Brief description for blog post 6",
"url" => [
"href" => "#",
"text" => "Read blog post 6"
]
],
[
"title" => "Blog title 7",
"description" => "Brief description for blog post 7",
"url" => [
"href" => "#",
"text" => "Read blog post 7"
]
],
[
"title" => "Blog title 8",
"description" => "Brief description for blog post 8",
"url" => [
"href" => "#",
"text" => "Read blog post 8"
]
],
[
"title" => "Blog title 9",
"description" => "Brief description for blog post 9",
"url" => [
"href" => "#",
"text" => "Read blog post 9"
]
],
[
"title" => "Blog title 10",
"description" => "Brief description for blog post 10",
"url" => [
"href" => "#",
"text" => "Read blog post 10"
]
]
];

$sections = [];
foreach ($posts as $post) {
$sections[] = \Eightfold\HTMLBuilder\Element::section(
\Eightfold\HTMLBuilder\Element::h2($post['title']),
\Eightfold\HTMLBuilder\Element::p($post['description']),
\Eightfold\HTMLBuilder\Element::p(
\Eightfold\HTMLBuilder\Element::a($post['url']['text'])
->props('href ' . $post['url']['href'])
)
);
}

print \Eightfold\HTMLBuilder\Document::create(
$pageTitle
)->body(
\Eightfold\HTMLBuilder\Element::h1($pageTitle),
\Eightfold\HTMLBuilder\Element::a('time')->props('href #bottom'),
\Eightfold\HTMLBuilder\Element::article(...$sections)
);

$end = hrtime(true);

$elapsed = $end - $start;
$ms = $elapsed/1e+6;

print('<p id="bottom"><b>' . $ms . 'ms</b></p>');
?>
133 changes: 133 additions & 0 deletions comparisons/php-html-builder-document/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

$start = hrtime(true);

require_once(__DIR__ . '/../../vendor/8fold/php-xml-builder/src/Contracts/Contentable.php');
require_once(__DIR__ . '/../../vendor/8fold/php-xml-builder/src/Contracts/ContentWithoutElement.php');

require_once(__DIR__ . '/../../vendor/8fold/php-xml-builder/src/Implementations/Properties.php');
require_once(__DIR__ . '/../../vendor/8fold/php-xml-builder/src/Implementations/Contentable.php');
require_once(__DIR__ . '/../../vendor/8fold/php-xml-builder/src/Implementations/ContentWithoutElement.php');

require_once(__DIR__ . '/../../vendor/8fold/php-xml-builder/src/Concatenate.php');
require_once(__DIR__ . '/../../vendor/8fold/php-xml-builder/src/Element.php');

require_once(__DIR__ . '/../../src/Document.php');
require_once(__DIR__ . '/../../src/Element.php');

$pageTitle = "Page title here";

$posts = [
[
"title" => "Blog title 1",
"description" => "Brief description for blog post 1",
"url" => [
"href" => "#",
"text" => "Read blog post 1"
]
],
[
"title" => "Blog title 2",
"description" => "Brief description for blog post 2",
"url" => [
"href" => "#",
"text" => "Read blog post 2"
]
],
[
"title" => "Blog title 3",
"description" => "Brief description for blog post 3",
"url" => [
"href" => "#",
"text" => "Read blog post 3"
]
],
[
"title" => "Blog title 4",
"description" => "Brief description for blog post 4",
"url" => [
"href" => "#",
"text" => "Read blog post 4"
]
],
[
"title" => "Blog title 5",
"description" => "Brief description for blog post 5",
"url" => [
"href" => "#",
"text" => "Read blog post 5"
]
],
[
"title" => "Blog title 6",
"description" => "Brief description for blog post 6",
"url" => [
"href" => "#",
"text" => "Read blog post 6"
]
],
[
"title" => "Blog title 7",
"description" => "Brief description for blog post 7",
"url" => [
"href" => "#",
"text" => "Read blog post 7"
]
],
[
"title" => "Blog title 8",
"description" => "Brief description for blog post 8",
"url" => [
"href" => "#",
"text" => "Read blog post 8"
]
],
[
"title" => "Blog title 9",
"description" => "Brief description for blog post 9",
"url" => [
"href" => "#",
"text" => "Read blog post 9"
]
],
[
"title" => "Blog title 10",
"description" => "Brief description for blog post 10",
"url" => [
"href" => "#",
"text" => "Read blog post 10"
]
]
];

$sections = [];
foreach ($posts as $post) {
$sections[] = \Eightfold\HTMLBuilder\Element::section(
\Eightfold\HTMLBuilder\Element::h2($post['title']),
\Eightfold\HTMLBuilder\Element::p($post['description']),
\Eightfold\HTMLBuilder\Element::p(
\Eightfold\HTMLBuilder\Element::a($post['url']['text'])
->props('href ' . $post['url']['href'])
)
);
}

print \Eightfold\HTMLBuilder\Document::create(
$pageTitle
)->body(
\Eightfold\HTMLBuilder\Element::h1($pageTitle),
\Eightfold\HTMLBuilder\Element::a('time')->props('href #bottom'),
\Eightfold\HTMLBuilder\Element::article(...$sections)
);

$end = hrtime(true);

$elapsed = $end - $start;
$ms = $elapsed/1e+6;

print('<p id="bottom"><b>' . $ms . 'ms</b></p>');
?>
Loading

0 comments on commit 6cde59f

Please sign in to comment.