|
| 1 | +<?php |
| 2 | + |
| 3 | +require_once(__DIR__.'/init.php'); |
| 4 | + |
| 5 | +use Composer\InstalledVersions; |
| 6 | +use Solarium\Core\Client\Response; |
| 7 | +use Solarium\Core\Event\Events; |
| 8 | +use Solarium\Core\Event\PreExecuteRequest; |
| 9 | +use Solarium\QueryType\Update\Query\Query; |
| 10 | + |
| 11 | +set_time_limit(0); |
| 12 | +ini_set('memory_limit', -1); |
| 13 | +ob_implicit_flush(true); |
| 14 | +@ob_end_flush(); |
| 15 | + |
| 16 | +htmlHeader(); |
| 17 | + |
| 18 | +echo '<h2>Note: These benchmarks build the requests but don\'t execute them</h2>'; |
| 19 | + |
| 20 | +$requestFormats = [ |
| 21 | + Query::REQUEST_FORMAT_XML, |
| 22 | + Query::REQUEST_FORMAT_JSON, |
| 23 | +]; |
| 24 | + |
| 25 | +if (InstalledVersions::isInstalled('spomky-labs/cbor-php')) { |
| 26 | + $requestFormats[] = Query::REQUEST_FORMAT_CBOR; |
| 27 | +} else { |
| 28 | + echo '<h2>Note: The CBOR benchmark requires spomky-labs/cbor-php</h2>'; |
| 29 | +} |
| 30 | + |
| 31 | +// memory usage is only useful with PHP 8.2+, earlier version don't allow resetting between benchmarks |
| 32 | +$withMemoryUsage = function_exists('memory_reset_peak_usage'); |
| 33 | + |
| 34 | +// create a client instance |
| 35 | +$client = new Solarium\Client($adapter, $eventDispatcher, $config); |
| 36 | + |
| 37 | +// autoload the buffered add plugin |
| 38 | +$addBuffer = $client->getPlugin('bufferedaddlite'); |
| 39 | + |
| 40 | +// return a dummy response instead of executing the query |
| 41 | +$response = new Response('', ['HTTP/1.0 200 OK']); |
| 42 | + |
| 43 | +// keep measures for individual build times |
| 44 | +$start = 0; |
| 45 | +$buildTimes = []; |
| 46 | + |
| 47 | +$client->getEventDispatcher()->addListener( |
| 48 | + Events::PRE_EXECUTE_REQUEST, |
| 49 | + function (PreExecuteRequest $event) use ($response, &$start, &$buildTimes) { |
| 50 | + $buildTimes[] = (hrtime(true) - $start) / 1000000; |
| 51 | + $event->setResponse($response); |
| 52 | + $start = hrtime(true); |
| 53 | + } |
| 54 | +); |
| 55 | + |
| 56 | +$docs = 1200000; |
| 57 | + |
| 58 | +foreach ($requestFormats as $requestFormat) { |
| 59 | + $addBuffer->setRequestFormat($requestFormat); |
| 60 | + |
| 61 | + echo '<h3>'.strtoupper($requestFormat).'</h3>'; |
| 62 | + echo '<table><thead>'; |
| 63 | + echo '<tr><th rowspan="2">buffer size</th><th colspan="5">build time</th>'; |
| 64 | + if ($withMemoryUsage) { |
| 65 | + echo '<th rowspan="2">mem peak usage</th>'; |
| 66 | + } |
| 67 | + echo '</tr>'; |
| 68 | + echo '<tr><th>min</th><th>max</th><th>mean</th><th>median</th><th>total</th></tr>'; |
| 69 | + echo '</thead><tbody style="text-align:right">'; |
| 70 | + |
| 71 | + foreach ([2000, 200, 20, 2] as $flushes) { |
| 72 | + $bufferSize = $docs / $flushes; |
| 73 | + |
| 74 | + $addBuffer->setBufferSize($bufferSize); |
| 75 | + |
| 76 | + echo '<tr><td>'.$bufferSize.'</td>'; |
| 77 | + |
| 78 | + $buildTimes = []; |
| 79 | + |
| 80 | + if ($withMemoryUsage) { |
| 81 | + memory_reset_peak_usage(); |
| 82 | + } |
| 83 | + |
| 84 | + $start = hrtime(true); |
| 85 | + |
| 86 | + for ($i = 0; $i < $docs; ++$i) { |
| 87 | + $data = [ |
| 88 | + 'id' => sprintf('test-%08d', $i), |
| 89 | + 'name' => 'test for buffered add', |
| 90 | + 'cat' => ['solarium-test', 'solarium-test-bufferedadd'], |
| 91 | + ]; |
| 92 | + $addBuffer->createDocument($data); |
| 93 | + } |
| 94 | + |
| 95 | + sort($buildTimes); |
| 96 | + $halfway = $flushes / 2; |
| 97 | + $total = array_sum($buildTimes); |
| 98 | + $min = reset($buildTimes); |
| 99 | + $max = end($buildTimes); |
| 100 | + $mean = $total / $flushes; |
| 101 | + $median = ($buildTimes[$halfway - 1] + $buildTimes[$halfway]) / 2; |
| 102 | + echo '<td>'.(int) $min.' ms</td>'; |
| 103 | + echo '<td>'.(int) $max.' ms</td>'; |
| 104 | + echo '<td>'.(int) $mean.' ms</td>'; |
| 105 | + echo '<td>'.(int) $median.' ms</td>'; |
| 106 | + echo '<td>'.(int) $total.' ms</td>'; |
| 107 | + |
| 108 | + if ($withMemoryUsage) { |
| 109 | + $memoryPeakUsage = memory_get_peak_usage() / 1024; |
| 110 | + echo '<td>'.(int) $memoryPeakUsage.' KiB</td>'; |
| 111 | + } |
| 112 | + |
| 113 | + echo '</tr>'; |
| 114 | + } |
| 115 | + |
| 116 | + echo '</tbody></table>'; |
| 117 | +} |
| 118 | + |
| 119 | +htmlFooter(); |
0 commit comments