bench_manipulatevcard.php 1.5 KB

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