|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Keepsuit\Liquid\Performance\benchmarks; |
| 4 | + |
| 5 | +use Keepsuit\Liquid\Environment; |
| 6 | +use Keepsuit\Liquid\EnvironmentFactory; |
| 7 | +use Keepsuit\Liquid\FileSystems\LocalFileSystem; |
| 8 | +use Keepsuit\Liquid\Performance\Shopify\CommentFormTag; |
| 9 | +use Keepsuit\Liquid\Performance\Shopify\CustomFilters; |
| 10 | +use Keepsuit\Liquid\Performance\Shopify\Database; |
| 11 | +use Keepsuit\Liquid\Performance\Shopify\PaginateTag; |
| 12 | +use Keepsuit\Liquid\Support\Arr; |
| 13 | +use Keepsuit\Liquid\TemplatesCache\MemoryTemplatesCache; |
| 14 | +use Keepsuit\Liquid\TemplatesCache\SerializeTemplatesCache; |
| 15 | +use Keepsuit\Liquid\TemplatesCache\VarExportTemplatesCache; |
| 16 | +use PhpBench\Attributes\AfterMethods; |
| 17 | +use PhpBench\Attributes\BeforeMethods; |
| 18 | +use PhpBench\Attributes\Iterations; |
| 19 | +use PhpBench\Attributes\OutputMode; |
| 20 | +use PhpBench\Attributes\OutputTimeUnit; |
| 21 | +use PhpBench\Attributes\Revs; |
| 22 | +use PhpBench\Attributes\Warmup; |
| 23 | + |
| 24 | +#[Iterations(10)] |
| 25 | +#[Revs(10)] |
| 26 | +#[Warmup(1)] |
| 27 | +#[OutputMode('throughput')] |
| 28 | +#[OutputTimeUnit('seconds', precision: 3)] |
| 29 | +#[AfterMethods('clearCache')] |
| 30 | +class TemplateCacheBench |
| 31 | +{ |
| 32 | + protected Environment $environment; |
| 33 | + |
| 34 | + protected array $templates; |
| 35 | + |
| 36 | + #[BeforeMethods('setupInMemory')] |
| 37 | + public function benchInMemory(): void |
| 38 | + { |
| 39 | + $this->renderTemplates(); |
| 40 | + } |
| 41 | + |
| 42 | + public function setupInMemory(): void |
| 43 | + { |
| 44 | + $this->environment = $this->environmentBuilder() |
| 45 | + ->setTemplatesCache(new MemoryTemplatesCache) |
| 46 | + ->build(); |
| 47 | + |
| 48 | + $this->loadTemplates(); |
| 49 | + } |
| 50 | + |
| 51 | + #[BeforeMethods('setupVarExporter')] |
| 52 | + public function benchVarExporter(): void |
| 53 | + { |
| 54 | + $this->renderTemplates(); |
| 55 | + } |
| 56 | + |
| 57 | + public function setupVarExporter(): void |
| 58 | + { |
| 59 | + $this->environment = $this->environmentBuilder() |
| 60 | + ->setTemplatesCache(new VarExportTemplatesCache(__DIR__.'/cache/var_export', keepInMemory: false)) |
| 61 | + ->build(); |
| 62 | + |
| 63 | + $this->loadTemplates(); |
| 64 | + } |
| 65 | + |
| 66 | + #[BeforeMethods('setupSerialize')] |
| 67 | + public function benchSerialize(): void |
| 68 | + { |
| 69 | + $this->renderTemplates(); |
| 70 | + } |
| 71 | + |
| 72 | + public function setupSerialize(): void |
| 73 | + { |
| 74 | + $this->environment = $this->environmentBuilder() |
| 75 | + ->setTemplatesCache(new SerializeTemplatesCache(__DIR__.'/cache/serialize', keepInMemory: false)) |
| 76 | + ->build(); |
| 77 | + |
| 78 | + $this->loadTemplates(); |
| 79 | + } |
| 80 | + |
| 81 | + protected function loadTemplates(): void |
| 82 | + { |
| 83 | + $baseDir = __DIR__.'/../tests'; |
| 84 | + $files = glob($baseDir.'/**/*.liquid'); |
| 85 | + |
| 86 | + if ($files === false) { |
| 87 | + throw new \RuntimeException('Could not find any tests'); |
| 88 | + } |
| 89 | + |
| 90 | + $this->templates = Arr::map($files, function (string $path) use ($baseDir) { |
| 91 | + // relative path to the base directory |
| 92 | + $name = str_replace($baseDir.'/', '', $path); |
| 93 | + // remove .liquid extension |
| 94 | + $name = substr($name, 0, -7); |
| 95 | + |
| 96 | + // replace / with . |
| 97 | + return str_replace('/', '.', $name); |
| 98 | + }); |
| 99 | + |
| 100 | + foreach ($this->templates as $template) { |
| 101 | + $this->environment->parseTemplate($template); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + protected function environmentBuilder(): EnvironmentFactory |
| 106 | + { |
| 107 | + return EnvironmentFactory::new() |
| 108 | + ->setFilesystem(new LocalFileSystem(__DIR__.'/../tests')) |
| 109 | + ->registerTag(CommentFormTag::class) |
| 110 | + ->registerTag(PaginateTag::class) |
| 111 | + ->registerFilters(CustomFilters::class); |
| 112 | + } |
| 113 | + |
| 114 | + protected function renderTemplates(): void |
| 115 | + { |
| 116 | + foreach ($this->templates as $template) { |
| 117 | + $this->environment->parseTemplate($template) |
| 118 | + ->render($this->environment->newRenderContext(staticData: [...Database::tables()])); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + public function clearCache(): void |
| 123 | + { |
| 124 | + $this->environment->templatesCache->clear(); |
| 125 | + } |
| 126 | +} |
0 commit comments