3636 exit (0 );
3737}
3838
39- $ iterations = (int ) ($ options ['iterations ' ] ?? 100 );
40- $ warmup = (int ) ($ options ['warmup ' ] ?? 10 );
39+ $ iterations = (int )($ options ['iterations ' ] ?? 100 );
40+ $ warmup = (int )($ options ['warmup ' ] ?? 10 );
4141$ jsonOutput = isset ($ options ['json ' ]);
4242
4343// Test fixtures
@@ -61,6 +61,7 @@ function generateFixture(string $name, int $paragraphs): string
6161 $ content .= "This is paragraph {$ i } with some *bold* and _italic_ text. " ;
6262 $ content .= "Here's a [link](https://example.com) and `inline code`. \n\n" ;
6363 }
64+
6465 return $ content ;
6566}
6667
@@ -189,6 +190,7 @@ function generateTableFixture(): string
189190 }
190191 $ content .= "\n" ;
191192 }
193+
192194 return $ content ;
193195}
194196
@@ -208,6 +210,7 @@ function generateCodeHeavyFixture(): string
208210 $ content .= "} \n" ;
209211 $ content .= "``` \n\n" ;
210212 }
213+
211214 return $ content ;
212215}
213216
@@ -216,13 +219,14 @@ function generateInlineHeavyFixture(): string
216219 $ content = "# Inline-Heavy Document \n\n" ;
217220 for ($ p = 0 ; $ p < 100 ; $ p ++) {
218221 $ content .= "Paragraph {$ p }: " ;
219- $ content .= " *bold* _italic_ `code` " ;
220- $ content .= " [link](url)  " ;
221- $ content .= " {+ins+} {-del-} {=mark=} " ;
222- $ content .= " H{~2~}O x{^2^} " ;
223- $ content .= " :symbol: \ $math \$ " ;
222+ $ content .= ' *bold* _italic_ `code` ' ;
223+ $ content .= ' [link](url)  ' ;
224+ $ content .= ' {+ins+} {-del-} {=mark=} ' ;
225+ $ content .= ' H{~2~}O x{^2^} ' ;
226+ $ content .= ' :symbol: $math$ ' ;
224227 $ content .= "\"smart quotes \" and---dashes. \n\n" ;
225228 }
229+
226230 return $ content ;
227231}
228232
@@ -247,6 +251,7 @@ function generateNestedListsFixture(): string
247251 $ content .= "2. Ordered 2 \n" ;
248252 $ content .= "3. Ordered 3 \n\n" ;
249253 }
254+
250255 return $ content ;
251256}
252257
@@ -275,9 +280,9 @@ function benchmark(callable $fn, int $iterations, int $warmup): array
275280 'mean ' => array_sum ($ times ) / $ count ,
276281 'median ' => $ count % 2 === 0
277282 ? ($ times [$ count / 2 - 1 ] + $ times [$ count / 2 ]) / 2
278- : $ times [(int ) ($ count / 2 )],
279- 'p95 ' => $ times [(int ) ($ count * 0.95 )],
280- 'p99 ' => $ times [(int ) ($ count * 0.99 )],
283+ : $ times [(int )($ count / 2 )],
284+ 'p95 ' => $ times [(int )($ count * 0.95 )],
285+ 'p99 ' => $ times [(int )($ count * 0.99 )],
281286 'stddev ' => calculateStdDev ($ times ),
282287 'iterations ' => $ iterations ,
283288 ];
@@ -286,10 +291,13 @@ function benchmark(callable $fn, int $iterations, int $warmup): array
286291function calculateStdDev (array $ values ): float
287292{
288293 $ count = count ($ values );
289- if ($ count < 2 ) return 0.0 ;
294+ if ($ count < 2 ) {
295+ return 0.0 ;
296+ }
290297
291298 $ mean = array_sum ($ values ) / $ count ;
292- $ variance = array_sum (array_map (fn ($ x ) => pow ($ x - $ mean , 2 ), $ values )) / ($ count - 1 );
299+ $ variance = array_sum (array_map (fn ($ x ) => pow ($ x - $ mean , 2 ), $ values )) / ($ count - 1 );
300+
293301 return sqrt ($ variance );
294302}
295303
@@ -301,13 +309,19 @@ function formatMs(float $ms): string
301309 if ($ ms < 1000 ) {
302310 return sprintf ('%.2f ms ' , $ ms );
303311 }
312+
304313 return sprintf ('%.2f s ' , $ ms / 1000 );
305314}
306315
307316function formatSize (int $ bytes ): string
308317{
309- if ($ bytes < 1024 ) return "{$ bytes } B " ;
310- if ($ bytes < 1024 * 1024 ) return sprintf ('%.1f KB ' , $ bytes / 1024 );
318+ if ($ bytes < 1024 ) {
319+ return "{$ bytes } B " ;
320+ }
321+ if ($ bytes < 1024 * 1024 ) {
322+ return sprintf ('%.1f KB ' , $ bytes / 1024 );
323+ }
324+
311325 return sprintf ('%.1f MB ' , $ bytes / (1024 * 1024 ));
312326}
313327
@@ -318,23 +332,30 @@ function formatSize(int $bytes): string
318332if (!$ jsonOutput ) {
319333 echo "Djot-PHP Performance Benchmark \n" ;
320334 echo "============================== \n" ;
321- echo " PHP Version: " . PHP_VERSION . "\n" ;
335+ echo ' PHP Version: ' . PHP_VERSION . "\n" ;
322336 echo "Iterations: {$ iterations }, Warmup: {$ warmup }\n" ;
323337 echo "\n" ;
324338}
325339
326340// Basic conversion benchmarks
327341if (!$ jsonOutput ) {
328342 echo "## Document Size Benchmarks \n\n" ;
329- printf ("%-15s %10s %12s %12s %12s %12s \n" ,
330- "Fixture " , "Size " , "Mean " , "Median " , "P95 " , "Throughput " );
331- echo str_repeat ("- " , 75 ) . "\n" ;
343+ printf (
344+ "%-15s %10s %12s %12s %12s %12s \n" ,
345+ 'Fixture ' ,
346+ 'Size ' ,
347+ 'Mean ' ,
348+ 'Median ' ,
349+ 'P95 ' ,
350+ 'Throughput ' ,
351+ );
352+ echo str_repeat ('- ' , 75 ) . "\n" ;
332353}
333354
334355foreach ($ fixtures as $ name => $ content ) {
335356 $ size = strlen ($ content );
336357
337- $ stats = benchmark (function () use ($ converter , $ content ) {
358+ $ stats = benchmark (function () use ($ converter , $ content ) {
338359 $ converter ->convert ($ content );
339360 }, $ iterations , $ warmup );
340361
@@ -347,22 +368,23 @@ function formatSize(int $bytes): string
347368 ];
348369
349370 if (!$ jsonOutput ) {
350- printf ("%-15s %10s %12s %12s %12s %10s/s \n" ,
371+ printf (
372+ "%-15s %10s %12s %12s %12s %10s/s \n" ,
351373 $ name ,
352374 formatSize ($ size ),
353375 formatMs ($ stats ['mean ' ]),
354376 formatMs ($ stats ['median ' ]),
355377 formatMs ($ stats ['p95 ' ]),
356- formatSize ((int ) $ throughput )
378+ formatSize ((int )$ throughput ),
357379 );
358380 }
359381}
360382
361383// Profile benchmarks
362384if (!$ jsonOutput ) {
363385 echo "\n## Profile Benchmarks (medium fixture) \n\n" ;
364- printf ("%-15s %12s %12s %12s \n" , " Profile " , " Mean " , " Median " , " P95 " );
365- echo str_repeat (" - " , 55 ) . "\n" ;
386+ printf ("%-15s %12s %12s %12s \n" , ' Profile ' , ' Mean ' , ' Median ' , ' P95 ' );
387+ echo str_repeat (' - ' , 55 ) . "\n" ;
366388}
367389
368390$ profiles = [
@@ -378,27 +400,28 @@ function formatSize(int $bytes): string
378400foreach ($ profiles as $ name => $ profile ) {
379401 $ conv = $ profile ? new DjotConverter (profile: $ profile ) : new DjotConverter ();
380402
381- $ stats = benchmark (function () use ($ conv , $ mediumContent ) {
403+ $ stats = benchmark (function () use ($ conv , $ mediumContent ) {
382404 $ conv ->convert ($ mediumContent );
383405 }, $ iterations , $ warmup );
384406
385407 $ results ['profiles ' ][$ name ] = $ stats ;
386408
387409 if (!$ jsonOutput ) {
388- printf ("%-15s %12s %12s %12s \n" ,
410+ printf (
411+ "%-15s %12s %12s %12s \n" ,
389412 $ name ,
390413 formatMs ($ stats ['mean ' ]),
391414 formatMs ($ stats ['median ' ]),
392- formatMs ($ stats ['p95 ' ])
415+ formatMs ($ stats ['p95 ' ]),
393416 );
394417 }
395418}
396419
397420// SafeMode benchmark
398421if (!$ jsonOutput ) {
399422 echo "\n## SafeMode Benchmarks (medium fixture) \n\n" ;
400- printf ("%-15s %12s %12s %12s \n" , " Mode " , " Mean " , " Median " , " P95 " );
401- echo str_repeat (" - " , 55 ) . "\n" ;
423+ printf ("%-15s %12s %12s %12s \n" , ' Mode ' , ' Mean ' , ' Median ' , ' P95 ' );
424+ echo str_repeat (' - ' , 55 ) . "\n" ;
402425}
403426
404427$ safeModes = [
@@ -409,56 +432,59 @@ function formatSize(int $bytes): string
409432foreach ($ safeModes as $ name => $ safeMode ) {
410433 $ conv = new DjotConverter (safeMode: $ safeMode );
411434
412- $ stats = benchmark (function () use ($ conv , $ mediumContent ) {
435+ $ stats = benchmark (function () use ($ conv , $ mediumContent ) {
413436 $ conv ->convert ($ mediumContent );
414437 }, $ iterations , $ warmup );
415438
416439 $ results ['safeMode ' ][$ name ] = $ stats ;
417440
418441 if (!$ jsonOutput ) {
419- printf ("%-15s %12s %12s %12s \n" ,
442+ printf (
443+ "%-15s %12s %12s %12s \n" ,
420444 $ name ,
421445 formatMs ($ stats ['mean ' ]),
422446 formatMs ($ stats ['median ' ]),
423- formatMs ($ stats ['p95 ' ])
447+ formatMs ($ stats ['p95 ' ]),
424448 );
425449 }
426450}
427451
428452// Parse-only benchmark
429453if (!$ jsonOutput ) {
430454 echo "\n## Parse vs Render (medium fixture) \n\n" ;
431- printf ("%-15s %12s %12s %12s \n" , " Phase " , " Mean " , " Median " , " P95 " );
432- echo str_repeat (" - " , 55 ) . "\n" ;
455+ printf ("%-15s %12s %12s %12s \n" , ' Phase ' , ' Mean ' , ' Median ' , ' P95 ' );
456+ echo str_repeat (' - ' , 55 ) . "\n" ;
433457}
434458
435459// Full conversion (parse + render)
436- $ stats = benchmark (function () use ($ converter , $ mediumContent ) {
460+ $ stats = benchmark (function () use ($ converter , $ mediumContent ) {
437461 $ converter ->convert ($ mediumContent );
438462}, $ iterations , $ warmup );
439463$ results ['phases ' ]['full ' ] = $ stats ;
440464
441465if (!$ jsonOutput ) {
442- printf ("%-15s %12s %12s %12s \n" ,
443- "full " ,
466+ printf (
467+ "%-15s %12s %12s %12s \n" ,
468+ 'full ' ,
444469 formatMs ($ stats ['mean ' ]),
445470 formatMs ($ stats ['median ' ]),
446- formatMs ($ stats ['p95 ' ])
471+ formatMs ($ stats ['p95 ' ]),
447472 );
448473}
449474
450475// Parse only
451- $ stats = benchmark (function () use ($ converter , $ mediumContent ) {
476+ $ stats = benchmark (function () use ($ converter , $ mediumContent ) {
452477 $ converter ->parse ($ mediumContent );
453478}, $ iterations , $ warmup );
454479$ results ['phases ' ]['parse ' ] = $ stats ;
455480
456481if (!$ jsonOutput ) {
457- printf ("%-15s %12s %12s %12s \n" ,
458- "parse " ,
482+ printf (
483+ "%-15s %12s %12s %12s \n" ,
484+ 'parse ' ,
459485 formatMs ($ stats ['mean ' ]),
460486 formatMs ($ stats ['median ' ]),
461- formatMs ($ stats ['p95 ' ])
487+ formatMs ($ stats ['p95 ' ]),
462488 );
463489}
464490
@@ -493,16 +519,23 @@ function formatSize(int $bytes): string
493519$ results ['memory ' ] = $ memoryResults ;
494520
495521if (!$ jsonOutput ) {
496- printf ("%-10s %12s %12s %12s %12s \n" ,
497- "Fixture " , "Input " , "Output " , "Delta " , "Peak " );
498- echo str_repeat ("- " , 60 ) . "\n" ;
522+ printf (
523+ "%-10s %12s %12s %12s %12s \n" ,
524+ 'Fixture ' ,
525+ 'Input ' ,
526+ 'Output ' ,
527+ 'Delta ' ,
528+ 'Peak ' ,
529+ );
530+ echo str_repeat ('- ' , 60 ) . "\n" ;
499531 foreach ($ memoryResults as $ name => $ mem ) {
500- printf ("%-10s %12s %12s %12s %12s \n" ,
532+ printf (
533+ "%-10s %12s %12s %12s %12s \n" ,
501534 $ name ,
502535 formatSize ($ mem ['input_size ' ]),
503536 formatSize ($ mem ['output_size ' ]),
504537 formatSize ($ mem ['memory_delta ' ]),
505- formatSize ($ mem ['memory_peak ' ])
538+ formatSize ($ mem ['memory_peak ' ]),
506539 );
507540 }
508541}
@@ -522,7 +555,7 @@ function formatSize(int $bytes): string
522555 $ complexStats = $ results ['conversion ' ]['complex ' ]['stats ' ];
523556 $ throughput = $ results ['conversion ' ]['complex ' ]['throughput_bps ' ];
524557 echo "Complex document ( {$ results ['conversion ' ]['complex ' ]['size_bytes ' ]} bytes): \n" ;
525- echo " Mean: " . formatMs ($ complexStats ['mean ' ]) . "\n" ;
526- echo " Throughput: " . formatSize ((int ) $ throughput ) . "/s \n" ;
558+ echo ' Mean: ' . formatMs ($ complexStats ['mean ' ]) . "\n" ;
559+ echo ' Throughput: ' . formatSize ((int )$ throughput ) . "/s \n" ;
527560 echo "\nBenchmark complete. \n" ;
528561}
0 commit comments