|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Compiler and runtime benchmark script. Default iterations: 1000. |
| 5 | + * |
| 6 | + * Usage: php -d opcache.enable_cli=1 -d opcache.jit=tracing tests/benchmark.php |
| 7 | + */ |
| 8 | + |
| 9 | +use DevTheorem\Handlebars\Handlebars; |
| 10 | + |
| 11 | +require __DIR__ . '/../vendor/autoload.php'; |
| 12 | + |
| 13 | +$iterations = (int) ($argv[1] ?? 1000); |
| 14 | +$filename = __DIR__ . "/largeTemplate.hbs"; |
| 15 | + |
| 16 | +// A large, complex template exercising as many syntax features as possible. |
| 17 | +$template = file_get_contents($filename); |
| 18 | +if ($template === false) { |
| 19 | + exit("Failed to open $filename"); |
| 20 | +} |
| 21 | + |
| 22 | +// Warm up: give the JIT a chance to compile hot paths before we measure. |
| 23 | +for ($i = 0; $i < 50; $i++) { |
| 24 | + Handlebars::precompile($template); |
| 25 | +} |
| 26 | + |
| 27 | +$start = hrtime(true); |
| 28 | + |
| 29 | +for ($i = 0; $i < $iterations; $i++) { |
| 30 | + Handlebars::precompile($template); |
| 31 | +} |
| 32 | + |
| 33 | +$elapsed = (hrtime(true) - $start) / 1e9; |
| 34 | +$perParse = $elapsed / $iterations * 1000; |
| 35 | +$php = Handlebars::precompile($template); |
| 36 | +$codeBytes = strlen($php); |
| 37 | + |
| 38 | +printf( |
| 39 | + "Compiled %d times in %.3f s | %.3f ms/compile | %.1f KB code\n", |
| 40 | + $iterations, |
| 41 | + $elapsed, |
| 42 | + $perParse, |
| 43 | + $codeBytes / 1024, |
| 44 | +); |
| 45 | + |
| 46 | +$helpers = [ |
| 47 | + 't' => function (string $a) { |
| 48 | + return $a; |
| 49 | + }, |
| 50 | + 'formatDate' => function (mixed $value, string $format) { |
| 51 | + return date($format, strtotime($value)); |
| 52 | + }, |
| 53 | + 'formatCurrency' => function (mixed $value, ?string $format) { |
| 54 | + return ($format ? "$format " : '') . number_format($value, 2); |
| 55 | + }, |
| 56 | + 'replace' => function (string $subject, string $search, ?string $replace) { |
| 57 | + return str_replace($search, $replace ?? '', $subject); |
| 58 | + }, |
| 59 | + 'eq' => function (mixed $a, mixed $b) { |
| 60 | + if ($a === null || $b === null) { |
| 61 | + // in JS, null is not equal to blank string or false or zero |
| 62 | + return $a === $b; |
| 63 | + } |
| 64 | + |
| 65 | + return $a == $b; |
| 66 | + }, |
| 67 | + 'and' => fn(mixed $a, mixed $b) => $a && $b, |
| 68 | + 'not' => fn(mixed $a) => !$a, |
| 69 | + 'gt' => fn(mixed $a, mixed $b) => $a > $b, |
| 70 | +]; |
| 71 | + |
| 72 | +$data = [ |
| 73 | + 'lang' => 'en', |
| 74 | + 'pageTitle' => 'Dashboard', |
| 75 | + 'siteName' => 'MyApp', |
| 76 | + 'stylesheets' => [ |
| 77 | + ['url' => '/css/app.css'], |
| 78 | + ['url' => '/css/print.css', 'media' => 'print'], |
| 79 | + ], |
| 80 | + 'bodyClass' => 'page-dashboard', |
| 81 | + 'sticky' => true, |
| 82 | + 'rootUrl' => '/', |
| 83 | + 'logoHtml' => '<img src="/logo.svg" alt="">', |
| 84 | + 'user' => [ |
| 85 | + 'id' => 1, |
| 86 | + 'name' => 'Alice', |
| 87 | + 'avatar' => '/avatars/alice.jpg', |
| 88 | + 'isAdmin' => true, |
| 89 | + 'verified' => true, |
| 90 | + ], |
| 91 | + 'navItems' => [ |
| 92 | + ['label' => 'Home', 'url' => '/', 'active' => true], |
| 93 | + ['label' => 'Reports', 'url' => '/reports', 'badge' => '3'], |
| 94 | + ['label' => 'More', 'url' => '#', 'icon' => 'chevron', 'children' => [ |
| 95 | + ['label' => 'Sub A', 'url' => '/a'], |
| 96 | + ['label' => 'Sub B', 'url' => '/b'], |
| 97 | + ]], |
| 98 | + ], |
| 99 | + 'alerts' => [ |
| 100 | + ['type' => 'success', 'message' => 'Saved!', 'dismissible' => true, 'icon' => 'check'], |
| 101 | + ], |
| 102 | + 'breadcrumbs' => [ |
| 103 | + ['label' => 'Home', 'url' => '/'], |
| 104 | + ['label' => 'Orders', 'url' => '/orders'], |
| 105 | + ['label' => 'List', 'url' => '/orders/list'], |
| 106 | + ], |
| 107 | + 'heading' => 'Orders', |
| 108 | + 'headingBadge' => ['type' => 'primary', 'text' => 'Live'], |
| 109 | + 'subheading' => 'All orders', |
| 110 | + 'actions' => [ |
| 111 | + ['label' => 'New', 'url' => '/orders/new', 'primary' => true, 'icon' => 'plus'], |
| 112 | + ], |
| 113 | + 'hoverable' => true, |
| 114 | + 'bordered' => false, |
| 115 | + 'sortBaseUrl' => '/orders', |
| 116 | + 'currentSort' => ['key' => 'date', 'dir' => 'asc'], |
| 117 | + 'showActions' => true, |
| 118 | + 'selectedIndex' => 2, |
| 119 | + 'columnCount' => 5, |
| 120 | + 'currency' => 'USD', |
| 121 | + 'columns' => [ |
| 122 | + ['key' => 'id', 'label' => '#', 'sortable' => true, 'type' => 'text'], |
| 123 | + ['key' => 'name', 'label' => 'Customer', 'type' => 'link', 'linkTemplate' => '/c/{id}'], |
| 124 | + ['key' => 'created', 'label' => 'Date', 'sortable' => true, 'type' => 'date', 'format' => 'M j, Y'], |
| 125 | + ['key' => 'total', 'label' => 'Total', 'type' => 'currency', 'showTotal' => true], |
| 126 | + ['key' => 'active', 'label' => 'Active', 'type' => 'boolean'], |
| 127 | + ], |
| 128 | + 'items' => array_map(fn($i) => [ |
| 129 | + 'id' => (string) $i, |
| 130 | + 'name' => "Customer $i", |
| 131 | + 'created' => '2024-03-' . str_pad((string) $i, 2, '0', STR_PAD_LEFT), |
| 132 | + 'total' => 100.0 * $i, |
| 133 | + 'active' => (bool) ($i % 2), |
| 134 | + 'deleted' => false, |
| 135 | + 'currency' => 'USD', |
| 136 | + ], range(1, 10)), |
| 137 | + 'rowActions' => [ |
| 138 | + ['icon' => 'edit', 'style' => 'secondary', 'labelKey' => 'edit', 'urlTemplate' => '/orders/{id}/edit', 'requiresAdmin' => false], |
| 139 | + ['icon' => 'trash', 'style' => 'danger', 'labelKey' => 'delete', 'urlTemplate' => '/orders/{id}', 'confirm' => true, 'confirmKey' => 'confirm_delete', 'requiresAdmin' => true], |
| 140 | + ], |
| 141 | + 'showTotals' => true, |
| 142 | + 'totals' => ['total' => 5500.00], |
| 143 | + 'pagination' => [ |
| 144 | + 'hasPrev' => false, |
| 145 | + 'hasNext' => true, |
| 146 | + 'prevUrl' => '#', |
| 147 | + 'nextUrl' => '/orders?page=2', |
| 148 | + 'start' => 1, |
| 149 | + 'end' => 10, |
| 150 | + 'total' => 42, |
| 151 | + 'pages' => [ |
| 152 | + ['active' => true, 'number' => 1, 'url' => '/orders'], |
| 153 | + ['active' => false, 'number' => 2, 'url' => '/orders?page=2'], |
| 154 | + ['ellipsis' => true, 'number' => null, 'url' => ''], |
| 155 | + ['active' => false, 'number' => 5, 'url' => '/orders?page=5'], |
| 156 | + ], |
| 157 | + ], |
| 158 | + 'sidePanels' => [ |
| 159 | + [ |
| 160 | + 'id' => 'summary', |
| 161 | + 'title' => 'Summary', |
| 162 | + 'type' => 'stats', |
| 163 | + 'collapsible' => true, |
| 164 | + 'collapsed' => false, |
| 165 | + 'stats' => [ |
| 166 | + ['label' => 'Total Orders', 'value' => 42, 'trend' => 'up', 'delta' => 5], |
| 167 | + ['label' => 'Revenue', 'value' => '$5,500', 'unit' => 'USD', 'delta' => 0], |
| 168 | + ], |
| 169 | + ], |
| 170 | + ], |
| 171 | + 'footerColumns' => [ |
| 172 | + ['heading' => 'Product', 'links' => [ |
| 173 | + ['label' => 'Features', 'url' => '/features'], |
| 174 | + ['label' => 'Pricing', 'url' => '/pricing'], |
| 175 | + ]], |
| 176 | + ['heading' => 'Legal', 'links' => [ |
| 177 | + ['label' => 'Privacy', 'url' => '/privacy'], |
| 178 | + ['label' => 'Terms', 'url' => '/terms'], |
| 179 | + ]], |
| 180 | + ], |
| 181 | + 'copyright' => '©', |
| 182 | + 'showYear' => true, |
| 183 | + 'currentYear' => 2024, |
| 184 | + 'social' => [ |
| 185 | + ['name' => 'GitHub', 'url' => 'https://github.com/myapp', 'icon' => 'github'], |
| 186 | + ], |
| 187 | + 'scripts' => [ |
| 188 | + ['url' => '/js/vendor.js'], |
| 189 | + ['url' => '/js/app.js', 'defer' => true], |
| 190 | + ], |
| 191 | +]; |
| 192 | + |
| 193 | +$renderer = Handlebars::template($php); |
| 194 | + |
| 195 | +// Warm up |
| 196 | +for ($i = 0; $i < 50; $i++) { |
| 197 | + $renderer($data, ['helpers' => $helpers]); |
| 198 | +} |
| 199 | + |
| 200 | +$start = hrtime(true); |
| 201 | + |
| 202 | +for ($i = 0; $i < $iterations; $i++) { |
| 203 | + $renderer($data, ['helpers' => $helpers]); |
| 204 | +} |
| 205 | + |
| 206 | +$elapsed = (hrtime(true) - $start) / 1e9; |
| 207 | +$perRun = $elapsed / $iterations * 1000; |
| 208 | +$outputBytes = strlen($renderer($data, ['helpers' => $helpers])); |
| 209 | + |
| 210 | +printf( |
| 211 | + "Executed %d times in %.3f s | %.3f ms/render | %.1f KB output\n", |
| 212 | + $iterations, |
| 213 | + $elapsed, |
| 214 | + $perRun, |
| 215 | + $outputBytes / 1024, |
| 216 | +); |
| 217 | + |
| 218 | +//echo "\n", $php, "\n"; |
0 commit comments