Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. <?php declare(strict_types=1);
  2.  
  3. use Burba\StrictJson\Fixtures\Docs\Address;
  4. use Burba\StrictJson\Fixtures\Docs\Event;
  5. use Burba\StrictJson\Fixtures\Docs\User;
  6. use Burba\StrictJson\StrictJson;
  7. use Performance\Lib\Handlers\ConfigHandler;
  8. use Performance\Lib\Point;
  9. use Performance\Lib\Presenters\Formatter;
  10. use Performance\Performance;
  11.  
  12.  
  13. require_once(__DIR__ . '/../../vendor/autoload.php');
  14.  
  15. $json = file_get_contents(__DIR__ . '/../Fixtures/Performance/huge_list.json');
  16.  
  17. $iteration_count = 100;
  18. $do_json_decode = true;
  19.  
  20. $mapper = StrictJson::builder()
  21. ->addParameterArrayAdapter(User::class, 'events_attended', Event::class)
  22. ->build();
  23.  
  24. for ($i = 0; $i < $iteration_count; $i++) {
  25. Performance::point("Fancy Decode $i");
  26. $classes = $mapper->mapToArrayOf($json, User::class);
  27. Performance::finish();
  28. }
  29.  
  30. for ($i = 0; $i < $iteration_count && $do_json_decode; $i++) {
  31. Performance::point("JSON Decode $i");
  32. $results = json_decode($json, true);
  33. $users = [];
  34. foreach ($results as $user_array) {
  35. $name = $user_array['name'] ?? null;
  36. $age = $user_array['age'] ?? null;
  37. $address_street = $user_array['address']['street'] ?? null;
  38. $address_zipcode = $user_array['address']['zip_code'] ?? null;
  39. if (!is_string($name) || !is_int($age) || !is_string($address_street) || !is_string($address_zipcode)) {
  40. throw new Exception('Invalid JSON');
  41. }
  42.  
  43. $address = new Address($address_street, $address_zipcode);
  44. $user = new User($name, $age, $address);
  45. $users[] = $user;
  46. }
  47. Performance::finish();
  48. }
  49.  
  50. $points = Performance::export()->get()['points'];
  51. $fancy_total = 0;
  52. $fancy_count = 0;
  53. $regular_total = 0;
  54. $regular_count = 0;
  55. /** @var Point $point */
  56. foreach ($points as $point) {
  57. $label = $point->getLabel();
  58. if (strpos($label, 'Fancy Decode') === 0) {
  59. $fancy_count++;
  60. $fancy_total += $point->getDifferenceTime();
  61. } elseif (strpos($label, 'JSON Decode') === 0) {
  62. $regular_count++;
  63. $regular_total += $point->getDifferenceTime();
  64. }
  65. }
  66.  
  67. $config = new ConfigHandler();
  68. $formatter = new Formatter($config);
  69. $fancy_average = $formatter->timeToHuman($fancy_total / $fancy_count);
  70. echo "Fancy Average time $fancy_average" . PHP_EOL;
  71. if ($regular_count) {
  72. $regular_average = $formatter->timeToHuman($regular_total / $regular_count);
  73. echo "Regular Average time $regular_average" . PHP_EOL;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement