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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
/vendor/
/cache/
/tests/coverage/
benchmarks/cache/
.php-cs-fixer.cache
/.phpbench/

71 changes: 71 additions & 0 deletions benchmarks/BladeBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);

namespace Millancore\Pesto\Benchmarks;

use Illuminate\Container\Container;
use Illuminate\Events\Dispatcher;
use Illuminate\Filesystem\Filesystem;
use Illuminate\View\Compilers\BladeCompiler;
use Illuminate\View\Engines\CompilerEngine;
use Illuminate\View\Engines\EngineResolver;
use Illuminate\View\Factory;
use Illuminate\View\FileViewFinder;
use PhpBench\Attributes as Bench;

#[Bench\BeforeMethods('setUp')]
class BladeBench
{
private Factory $blade;

public function setUp(): void
{
$templatesPath = __DIR__ . '/templates/blade';
$cachePath = __DIR__ . '/cache/blade';

$filesystem = new Filesystem();
$container = new Container();

$compiler = new BladeCompiler($filesystem, $cachePath);

$resolver = new EngineResolver();
$resolver->register('blade', function () use ($compiler) {
return new CompilerEngine($compiler);
});

$finder = new FileViewFinder($filesystem, [$templatesPath]);
$dispatcher = new Dispatcher($container);

$this->blade = new Factory($resolver, $finder, $dispatcher);
$this->blade->setContainer($container);
}

#[Bench\Subject]
#[Bench\Groups(['simple'])]
public function benchSimple(): void
{
$this->blade->make('simple', DataProvider::simple())->render();
}

#[Bench\Subject]
#[Bench\Groups(['loop'])]
public function benchLoop(): void
{
$this->blade->make('loop', DataProvider::loop())->render();
}

#[Bench\Subject]
#[Bench\Groups(['conditional'])]
public function benchConditional(): void
{
$this->blade->make('conditional', DataProvider::conditional())->render();
}

#[Bench\Subject]
#[Bench\Groups(['partial'])]
public function benchPartial(): void
{
$this->blade->make('partial', DataProvider::partial())->render();
}
}
64 changes: 64 additions & 0 deletions benchmarks/DataProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

namespace Millancore\Pesto\Benchmarks;

final class DataProvider
{
public static function simple(): array
{
return [
'title' => 'Benchmark Page',
'heading' => 'Hello World',
'content' => 'This is a <strong>benchmark</strong> test with special chars: &, <, >, "quotes".',
'author' => 'John Doe',
];
}

public static function loop(): array
{
$items = [];
for ($i = 0; $i < 100; $i++) {
$items[] = [
'name' => "User {$i}",
'email' => "user{$i}@example.com",
'bio' => "Bio for user {$i} with <html> entities & \"quotes\".",
];
}

return [
'title' => 'Users List',
'heading' => 'All Users',
'items' => $items,
];
}

public static function conditional(): array
{
$notifications = [];
for ($i = 0; $i < 50; $i++) {
$notifications[] = [
'message' => "Notification #{$i}: Something happened.",
'unread' => $i % 3 === 0,
];
}

return [
'title' => 'Dashboard',
'name' => 'Jane Doe',
'role' => 'editor',
'notifications' => $notifications,
];
}

public static function partial(): array
{
return [
'title' => 'Page with Partial',
'siteName' => 'My Website',
'heading' => 'Main Content',
'content' => 'This page includes a header partial.',
];
}
}
53 changes: 53 additions & 0 deletions benchmarks/PestoBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Millancore\Pesto\Benchmarks;

use Millancore\Pesto\Environment;
use Millancore\Pesto\PestoFactory;
use PhpBench\Attributes as Bench;

#[Bench\BeforeMethods('setUp')]
class PestoBench
{
private Environment $env;
private string $templatesPath;
private string $cachePath;

public function setUp(): void
{
$this->templatesPath = __DIR__ . '/templates/pesto';
$this->cachePath = __DIR__ . '/cache/pesto';

$this->env = PestoFactory::create($this->templatesPath, $this->cachePath);
}

#[Bench\Subject]
#[Bench\Groups(['simple'])]
public function benchSimple(): void
{
$this->env->make('simple.html', DataProvider::simple())->toHtml();
}

#[Bench\Subject]
#[Bench\Groups(['loop'])]
public function benchLoop(): void
{
$this->env->make('loop.html', DataProvider::loop())->toHtml();
}

#[Bench\Subject]
#[Bench\Groups(['conditional'])]
public function benchConditional(): void
{
$this->env->make('conditional.html', DataProvider::conditional())->toHtml();
}

#[Bench\Subject]
#[Bench\Groups(['partial'])]
public function benchPartial(): void
{
$this->env->make('partial.html', DataProvider::partial())->toHtml();
}
}
52 changes: 52 additions & 0 deletions benchmarks/TwigBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace Millancore\Pesto\Benchmarks;

use PhpBench\Attributes as Bench;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;

#[Bench\BeforeMethods('setUp')]
class TwigBench
{
private Environment $twig;

public function setUp(): void
{
$loader = new FilesystemLoader(__DIR__ . '/templates/twig');
$this->twig = new Environment($loader, [
'cache' => __DIR__ . '/cache/twig',
'auto_reload' => true,
]);
}

#[Bench\Subject]
#[Bench\Groups(['simple'])]
public function benchSimple(): void
{
$this->twig->render('simple.html.twig', DataProvider::simple());
}

#[Bench\Subject]
#[Bench\Groups(['loop'])]
public function benchLoop(): void
{
$this->twig->render('loop.html.twig', DataProvider::loop());
}

#[Bench\Subject]
#[Bench\Groups(['conditional'])]
public function benchConditional(): void
{
$this->twig->render('conditional.html.twig', DataProvider::conditional());
}

#[Bench\Subject]
#[Bench\Groups(['partial'])]
public function benchPartial(): void
{
$this->twig->render('partial.html.twig', DataProvider::partial());
}
}
Loading
Loading