|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Micro-benchmark for hot paths in the Cloudinary PHP SDK. |
| 4 | + * |
| 5 | + * Usage: |
| 6 | + * php tools/benchmark.php |
| 7 | + * |
| 8 | + * Run on each branch/stash to compare before/after. |
| 9 | + */ |
| 10 | + |
| 11 | +require_once __DIR__ . '/../vendor/autoload.php'; |
| 12 | + |
| 13 | +use Cloudinary\Asset\Image; |
| 14 | +use Cloudinary\Asset\Video; |
| 15 | +use Cloudinary\Configuration\Configuration; |
| 16 | + |
| 17 | +// ── Setup ──────────────────────────────────────────────────────────────────── |
| 18 | + |
| 19 | +putenv('CLOUDINARY_URL=cloudinary://api_key:api_secret@my_cloud'); |
| 20 | +Configuration::instance()->init(); |
| 21 | +Configuration::instance()->url->analytics(false); |
| 22 | + |
| 23 | +const ITERATIONS = 10_000; |
| 24 | +const RUNS = 3; |
| 25 | + |
| 26 | +// ── Benchmark helpers ───────────────────────────────────────────────────────── |
| 27 | + |
| 28 | +function bench(string $label, callable $fn): void |
| 29 | +{ |
| 30 | + $times = []; |
| 31 | + |
| 32 | + for ($run = 0; $run < RUNS; $run++) { |
| 33 | + $start = hrtime(true); |
| 34 | + for ($i = 0; $i < ITERATIONS; $i++) { |
| 35 | + $fn(); |
| 36 | + } |
| 37 | + $times[] = (hrtime(true) - $start) / 1e6; // ns → ms |
| 38 | + } |
| 39 | + |
| 40 | + $avg = array_sum($times) / count($times); |
| 41 | + $min = min($times); |
| 42 | + $max = max($times); |
| 43 | + |
| 44 | + printf( |
| 45 | + " %-50s avg: %7.2f ms min: %7.2f ms max: %7.2f ms\n", |
| 46 | + $label, |
| 47 | + $avg, |
| 48 | + $min, |
| 49 | + $max |
| 50 | + ); |
| 51 | +} |
| 52 | + |
| 53 | +// ── Scenarios ───────────────────────────────────────────────────────────────── |
| 54 | + |
| 55 | +echo str_repeat('─', 90) . "\n"; |
| 56 | +echo sprintf(" Cloudinary PHP SDK benchmark — %d iterations × %d runs\n", ITERATIONS, RUNS); |
| 57 | +echo str_repeat('─', 90) . "\n"; |
| 58 | + |
| 59 | +// 1. Asset construction (exercises configuration() fast path) |
| 60 | +bench('new Image($source)', function () { |
| 61 | + $img = new Image('sample/image.jpg'); |
| 62 | +}); |
| 63 | + |
| 64 | +// 2. Asset construction + URL generation (exercises finalizeSource, finalizeVersion) |
| 65 | +bench('(string) new Image($source)', function () { |
| 66 | + $img = (string) new Image('sample/image.jpg'); |
| 67 | +}); |
| 68 | + |
| 69 | +// 3. URL generation on a pre-built asset (isolates toUrl() overhead) |
| 70 | +$image = new Image('sample/image.jpg'); |
| 71 | +bench('$image->toUrl() [pre-built asset]', function () use ($image) { |
| 72 | + $url = (string) $image->toUrl(); |
| 73 | +}); |
| 74 | + |
| 75 | +// 4. Asset with suffix (exercises setSuffix + finalizeAssetType) |
| 76 | +bench('new Image + setSuffix()', function () { |
| 77 | + $img = new Image('sample/image.jpg'); |
| 78 | + $img->asset->suffix = 'my-seo-name'; |
| 79 | + $url = (string) $img->toUrl(); |
| 80 | +}); |
| 81 | + |
| 82 | +// 5. Video asset (different asset type path) |
| 83 | +bench('(string) new Video($source)', function () { |
| 84 | + $img = (string) new Video('sample/video.mp4'); |
| 85 | +}); |
| 86 | + |
| 87 | +// 6. Configuration::jsonSerialize() (exercises array_merge → += fix) |
| 88 | +$config = Configuration::instance(); |
| 89 | +bench('Configuration::jsonSerialize()', function () use ($config) { |
| 90 | + $config->jsonSerialize(); |
| 91 | +}); |
| 92 | + |
| 93 | +// 7. Asset construction from Configuration array (slow path, for comparison) |
| 94 | +$configArray = $config->jsonSerialize(); |
| 95 | +bench('new Image($source, $configArray) [array config]', function () use ($configArray) { |
| 96 | + $img = new Image('sample/image.jpg', $configArray); |
| 97 | +}); |
| 98 | + |
| 99 | +echo str_repeat('─', 90) . "\n"; |
0 commit comments