|
| 1 | +#!/usr/bin/env php |
| 2 | +<?php |
| 3 | + |
| 4 | +declare(strict_types=1); |
| 5 | + |
| 6 | +use JsonMachine\Items; |
| 7 | +use PhpJsonChunk\JsonChunkReader; |
| 8 | + |
| 9 | +require_once __DIR__ . '/../vendor/autoload.php'; |
| 10 | + |
| 11 | +/** |
| 12 | + * @param array<int, string> $argv |
| 13 | + * |
| 14 | + * @return array{runs: int, sizes: array<int, int>} |
| 15 | + */ |
| 16 | +function parseOptions(array $argv): array |
| 17 | +{ |
| 18 | + $runs = 3; |
| 19 | + $sizes = [10_000, 30_000, 50_000, 100_000]; |
| 20 | + |
| 21 | + foreach ($argv as $argument) { |
| 22 | + if ($argument === '--help' || $argument === '-h') { |
| 23 | + fwrite( |
| 24 | + STDOUT, |
| 25 | + "Usage: php bin/benchmark.php [--runs=3] [--sizes=10000,30000]\n", |
| 26 | + ); |
| 27 | + exit(0); |
| 28 | + } |
| 29 | + |
| 30 | + if (str_starts_with($argument, '--runs=')) { |
| 31 | + $parsedRuns = (int) substr($argument, strlen('--runs=')); |
| 32 | + if ($parsedRuns > 0) { |
| 33 | + $runs = $parsedRuns; |
| 34 | + } |
| 35 | + continue; |
| 36 | + } |
| 37 | + |
| 38 | + if (str_starts_with($argument, '--sizes=')) { |
| 39 | + $sizesRaw = substr($argument, strlen('--sizes=')); |
| 40 | + $parsedSizes = []; |
| 41 | + |
| 42 | + foreach (explode(',', $sizesRaw) as $sizeRaw) { |
| 43 | + $size = (int) trim($sizeRaw); |
| 44 | + if ($size > 0) { |
| 45 | + $parsedSizes[] = $size; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + if ($parsedSizes !== []) { |
| 50 | + $sizes = $parsedSizes; |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + return [ |
| 56 | + 'runs' => $runs, |
| 57 | + 'sizes' => $sizes, |
| 58 | + ]; |
| 59 | +} |
| 60 | + |
| 61 | +function buildDatasetFile(int $count): string |
| 62 | +{ |
| 63 | + $path = tempnam(sys_get_temp_dir(), 'jcmp_'); |
| 64 | + if ($path === false) { |
| 65 | + throw new RuntimeException('Cannot create temp file.'); |
| 66 | + } |
| 67 | + |
| 68 | + $fh = fopen($path, 'wb'); |
| 69 | + if ($fh === false) { |
| 70 | + throw new RuntimeException('Cannot open temp file.'); |
| 71 | + } |
| 72 | + |
| 73 | + fwrite($fh, '{"count":' . $count . ',"data":['); |
| 74 | + for ($i = 1; $i <= $count; $i++) { |
| 75 | + if ($i > 1) { |
| 76 | + fwrite($fh, ','); |
| 77 | + } |
| 78 | + |
| 79 | + fwrite($fh, sprintf( |
| 80 | + '{"id":%d,"name":"test","surname":"test","createdAt":"2023-01-01T00:00:00.000Z"}', |
| 81 | + $i, |
| 82 | + )); |
| 83 | + } |
| 84 | + fwrite($fh, ']}'); |
| 85 | + fclose($fh); |
| 86 | + |
| 87 | + return $path; |
| 88 | +} |
| 89 | + |
| 90 | +/** |
| 91 | + * @return array{elapsedMs: float, peakDeltaMb: float, total: int} |
| 92 | + */ |
| 93 | +function measurePhpJsonChunk(string $filePath): array |
| 94 | +{ |
| 95 | + gc_collect_cycles(); |
| 96 | + memory_reset_peak_usage(); |
| 97 | + $memStart = memory_get_usage(false); |
| 98 | + $timeStart = hrtime(true); |
| 99 | + |
| 100 | + $reader = new JsonChunkReader(); |
| 101 | + $total = 0; |
| 102 | + |
| 103 | + foreach ($reader->readGenerator($filePath, keyPath: 'data') as $item) { |
| 104 | + $total++; |
| 105 | + /** @phpstan-ignore-next-line */ |
| 106 | + $_ = $item; |
| 107 | + } |
| 108 | + |
| 109 | + return [ |
| 110 | + 'elapsedMs' => (hrtime(true) - $timeStart) / 1_000_000, |
| 111 | + 'peakDeltaMb' => max(0, (memory_get_peak_usage(false) - $memStart) / 1024 / 1024), |
| 112 | + 'total' => $total, |
| 113 | + ]; |
| 114 | +} |
| 115 | + |
| 116 | +/** |
| 117 | + * @return array{elapsedMs: float, peakDeltaMb: float, total: int} |
| 118 | + */ |
| 119 | +function measureJsonMachine(string $filePath): array |
| 120 | +{ |
| 121 | + gc_collect_cycles(); |
| 122 | + memory_reset_peak_usage(); |
| 123 | + $memStart = memory_get_usage(false); |
| 124 | + $timeStart = hrtime(true); |
| 125 | + |
| 126 | + $total = 0; |
| 127 | + $items = Items::fromFile($filePath, ['pointer' => '/data']); |
| 128 | + |
| 129 | + foreach ($items as $item) { |
| 130 | + $total++; |
| 131 | + /** @phpstan-ignore-next-line */ |
| 132 | + $_ = $item; |
| 133 | + } |
| 134 | + |
| 135 | + return [ |
| 136 | + 'elapsedMs' => (hrtime(true) - $timeStart) / 1_000_000, |
| 137 | + 'peakDeltaMb' => max(0, (memory_get_peak_usage(false) - $memStart) / 1024 / 1024), |
| 138 | + 'total' => $total, |
| 139 | + ]; |
| 140 | +} |
| 141 | + |
| 142 | +/** |
| 143 | + * @param array<int, array{elapsedMs: float, peakDeltaMb: float, total: int}> $results |
| 144 | + */ |
| 145 | +function medianMetric(array $results, string $key): float |
| 146 | +{ |
| 147 | + $values = []; |
| 148 | + |
| 149 | + foreach ($results as $result) { |
| 150 | + $values[] = (float) $result[$key]; |
| 151 | + } |
| 152 | + |
| 153 | + sort($values); |
| 154 | + |
| 155 | + return $values[(int) floor(count($values) / 2)]; |
| 156 | +} |
| 157 | + |
| 158 | +/** |
| 159 | + * @param array<int, int> $sizes |
| 160 | + */ |
| 161 | +function runBenchmark(int $runs, array $sizes): void |
| 162 | +{ |
| 163 | + printf("Synthetic benchmark (median of %d runs)\n\n", $runs); |
| 164 | + printf("%-8s %-12s %-10s %-12s %-10s %-11s %-9s %s\n", 'Records', 'PC time', 'PC mem', 'JM time', 'JM mem', 'Time delta', 'Time %', 'Speed winner'); |
| 165 | + echo str_repeat('-', 100) . "\n"; |
| 166 | + |
| 167 | + foreach ($sizes as $count) { |
| 168 | + $filePath = buildDatasetFile($count); |
| 169 | + |
| 170 | + try { |
| 171 | + $pcResults = []; |
| 172 | + $jmResults = []; |
| 173 | + |
| 174 | + for ($run = 0; $run < $runs; $run++) { |
| 175 | + $pcResults[] = measurePhpJsonChunk($filePath); |
| 176 | + $jmResults[] = measureJsonMachine($filePath); |
| 177 | + } |
| 178 | + |
| 179 | + $pcTime = medianMetric($pcResults, 'elapsedMs'); |
| 180 | + $pcMem = medianMetric($pcResults, 'peakDeltaMb'); |
| 181 | + $jmTime = medianMetric($jmResults, 'elapsedMs'); |
| 182 | + $jmMem = medianMetric($jmResults, 'peakDeltaMb'); |
| 183 | + |
| 184 | + $timeDeltaMs = $pcTime - $jmTime; |
| 185 | + $timeDeltaPct = $jmTime > 0 ? ($timeDeltaMs / $jmTime) * 100 : 0.0; |
| 186 | + $winner = $timeDeltaMs <= 0 ? 'PhpJsonChunk' : 'JsonMachine'; |
| 187 | + |
| 188 | + printf("%-8d %8.1f ms %6.2f MB %8.1f ms %6.2f MB %+9.1f ms %+7.1f%% %s\n", $count, $pcTime, $pcMem, $jmTime, $jmMem, $timeDeltaMs, $timeDeltaPct, $winner); |
| 189 | + } finally { |
| 190 | + if (is_file($filePath)) { |
| 191 | + unlink($filePath); |
| 192 | + } |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + echo "\nLegend: Delta = PhpJsonChunk - JSON Machine (negative = PhpJsonChunk is faster)\n"; |
| 197 | +} |
| 198 | + |
| 199 | +$options = parseOptions(array_slice($_SERVER['argv'], 1)); |
| 200 | +runBenchmark($options['runs'], $options['sizes']); |
0 commit comments