|
| 1 | +#!/usr/bin/env php |
| 2 | +<?php |
| 3 | + |
| 4 | +use Jcupitt\Vips\Config; |
| 5 | +use Jcupitt\Vips\Image; |
| 6 | +use Jcupitt\Vips\VipsSource; |
| 7 | +use Jcupitt\Vips\VipsSourceResource; |
| 8 | +use Jcupitt\Vips\VipsTarget; |
| 9 | +use Jcupitt\Vips\VipsTargetResource; |
| 10 | + |
| 11 | +require dirname(__DIR__) . '/vendor/autoload.php'; |
| 12 | + |
| 13 | +$doBenchmark = static function () { |
| 14 | + $sourceOptions = ['access' => 'sequential']; |
| 15 | + $sourceOptionString = 'access=sequential'; |
| 16 | + $iterations = 100; |
| 17 | + $targetWidth = 100.0; |
| 18 | + $targetSuffix = '.jpg'; |
| 19 | + $targetOptions = ['optimize-coding' => true, 'strip' => true, 'Q' => 100, 'profile' => 'srgb']; |
| 20 | + $targetFile = dirname(__DIR__) . "/tests/images/target.jpg"; |
| 21 | + $sourceFile = dirname(__DIR__) . '/tests/images/img_0076.jpg'; |
| 22 | + |
| 23 | +### Callbacks |
| 24 | + $start = microtime(true); |
| 25 | + |
| 26 | + for ($i = 0; $i < $iterations; $i++) { |
| 27 | + $source = new VipsSourceResource(fopen($sourceFile, 'rb')); |
| 28 | + $target = new VipsTargetResource(fopen($targetFile, 'wb+')); |
| 29 | + $image = Image::newFromSource($source, '', $sourceOptions); |
| 30 | + $image = $image->resize($targetWidth / $image->width); |
| 31 | + $image->writeToTarget( |
| 32 | + $target, |
| 33 | + $targetSuffix, |
| 34 | + $targetOptions |
| 35 | + ); |
| 36 | + unlink($targetFile); |
| 37 | + } |
| 38 | + |
| 39 | + echo (microtime(true) - $start) . ' Seconds for Streaming with callbacks' . PHP_EOL; |
| 40 | + |
| 41 | +### Builtin |
| 42 | + $start = microtime(true); |
| 43 | + |
| 44 | + for ($i = 0; $i < $iterations; $i++) { |
| 45 | + $source = VipsSource::newFromFile($sourceFile); |
| 46 | + $target = VipsTarget::newToFile($targetFile); |
| 47 | + $image = Image::newFromSource($source, '', $sourceOptions); |
| 48 | + $image = $image->resize($targetWidth / $image->width); |
| 49 | + $image->writeToTarget( |
| 50 | + $target, |
| 51 | + $targetSuffix, |
| 52 | + $targetOptions |
| 53 | + ); |
| 54 | + unlink($targetFile); |
| 55 | + } |
| 56 | + |
| 57 | + echo (microtime(true) - $start) . ' Seconds for Streaming with builtin source/target' . PHP_EOL; |
| 58 | + |
| 59 | +### Callbacks Thumbnail |
| 60 | + $start = microtime(true); |
| 61 | + |
| 62 | + for ($i = 0; $i < $iterations; $i++) { |
| 63 | + $source = new VipsSourceResource(fopen($sourceFile, 'rb')); |
| 64 | + $target = new VipsTargetResource(fopen($targetFile, 'wb+')); |
| 65 | + $image = Image::thumbnail_source($source, $targetWidth); |
| 66 | + $image->writeToTarget( |
| 67 | + $target, |
| 68 | + $targetSuffix, |
| 69 | + $targetOptions |
| 70 | + ); |
| 71 | + unlink($targetFile); |
| 72 | + } |
| 73 | + |
| 74 | + echo (microtime(true) - $start) . ' Seconds for Streaming Thumbnail with callbacks' . PHP_EOL; |
| 75 | + |
| 76 | +### Builtin Thumbnail |
| 77 | + $start = microtime(true); |
| 78 | + |
| 79 | + for ($i = 0; $i < $iterations; $i++) { |
| 80 | + $source = VipsSource::newFromFile($sourceFile); |
| 81 | + $target = VipsTarget::newToFile($targetFile); |
| 82 | + $image = Image::thumbnail_source($source, $targetWidth); |
| 83 | + $image->writeToTarget( |
| 84 | + $target, |
| 85 | + $targetSuffix, |
| 86 | + $targetOptions |
| 87 | + ); |
| 88 | + unlink($targetFile); |
| 89 | + } |
| 90 | + |
| 91 | + echo (microtime(true) - $start) . ' Seconds for Streaming Thumbnail with builtin source/target' . PHP_EOL; |
| 92 | + |
| 93 | +### Thumbnail |
| 94 | + $start = microtime(true); |
| 95 | + |
| 96 | + for ($i = 0; $i < $iterations; $i++) { |
| 97 | + $image = Image::thumbnail($sourceFile . "[$sourceOptionString]", $targetWidth); |
| 98 | + $image->writeToFile( |
| 99 | + $targetFile, |
| 100 | + $targetOptions |
| 101 | + ); |
| 102 | + unlink($targetFile); |
| 103 | + } |
| 104 | + |
| 105 | + echo (microtime(true) - $start) . ' Seconds for Thumbnail API' . PHP_EOL; |
| 106 | + |
| 107 | +### Classic |
| 108 | + $start = microtime(true); |
| 109 | + |
| 110 | + for ($i = 0; $i < $iterations; $i++) { |
| 111 | + $image = Image::newFromFile($sourceFile, $sourceOptions); |
| 112 | + $image = $image->resize($targetWidth / $image->width); |
| 113 | + $image->writeToFile( |
| 114 | + $targetFile, |
| 115 | + $targetOptions |
| 116 | + ); |
| 117 | + unlink($targetFile); |
| 118 | + } |
| 119 | + |
| 120 | + echo (microtime(true) - $start) . ' Seconds for Classic API' . PHP_EOL; |
| 121 | +}; |
| 122 | + |
| 123 | +$doBenchmark(); |
| 124 | + |
| 125 | +echo "=== NOW NO CACHE ===" . PHP_EOL; |
| 126 | + |
| 127 | +Config::cacheSetMax(0); |
| 128 | +Config::cacheSetMaxFiles(0); |
| 129 | +Config::cacheSetMaxMem(0); |
| 130 | + |
| 131 | +$doBenchmark(); |
0 commit comments