1<?php 2 3include __DIR__ . '/../vendor/autoload.php'; 4 5if ($argc < 2) { 6 echo "sabre/vobject ", Sabre\VObject\Version::VERSION, " manipulation benchmark\n"; 7 echo "\n"; 8 echo "This script can be used to measure the speed of opening a large amount of\n"; 9 echo "vcards, making a few alterations and serializing them again.\n"; 10 echo "system."; 11 echo "\n"; 12 echo "Usage: " . $argv[0] . " inputfile.vcf\n"; 13 die(); 14} 15 16list(, $inputFile) = $argv; 17 18$input = file_get_contents($inputFile); 19 20$splitter = new Sabre\VObject\Splitter\VCard($input); 21 22$bench = new Hoa\Bench\Bench(); 23 24while (true) { 25 26 $bench->parse->start(); 27 $vcard = $splitter->getNext(); 28 $bench->parse->pause(); 29 30 if (!$vcard) break; 31 32 $bench->manipulate->start(); 33 $vcard->{'X-FOO'} = 'Random new value!'; 34 $emails = []; 35 if (isset($vcard->EMAIL)) foreach ($vcard->EMAIL as $email) { 36 $emails[] = (string)$email; 37 } 38 $bench->manipulate->pause(); 39 40 $bench->serialize->start(); 41 $vcard2 = $vcard->serialize(); 42 $bench->serialize->pause(); 43 44 $vcard->destroy(); 45 46} 47 48 49 50echo $bench,"\n"; 51 52function formatMemory($input) { 53 54 if (strlen($input) > 6) { 55 56 return round($input / (1024 * 1024)) . 'M'; 57 58 } elseif (strlen($input) > 3) { 59 60 return round($input / 1024) . 'K'; 61 62 } 63 64} 65 66unset($input, $splitter); 67 68echo "peak memory usage: " . formatMemory(memory_get_peak_usage()), "\n"; 69echo "current memory usage: " . formatMemory(memory_get_usage()), "\n"; 70