Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php declare(strict_types=1);
- use Burba\StrictJson\Fixtures\Docs\Address;
- use Burba\StrictJson\Fixtures\Docs\Event;
- use Burba\StrictJson\Fixtures\Docs\User;
- use Burba\StrictJson\StrictJson;
- use Performance\Lib\Handlers\ConfigHandler;
- use Performance\Lib\Point;
- use Performance\Lib\Presenters\Formatter;
- use Performance\Performance;
- require_once(__DIR__ . '/../../vendor/autoload.php');
- $json = file_get_contents(__DIR__ . '/../Fixtures/Performance/huge_list.json');
- $iteration_count = 100;
- $do_json_decode = true;
- $mapper = StrictJson::builder()
- ->addParameterArrayAdapter(User::class, 'events_attended', Event::class)
- ->build();
- for ($i = 0; $i < $iteration_count; $i++) {
- Performance::point("Fancy Decode $i");
- $classes = $mapper->mapToArrayOf($json, User::class);
- Performance::finish();
- }
- for ($i = 0; $i < $iteration_count && $do_json_decode; $i++) {
- Performance::point("JSON Decode $i");
- $results = json_decode($json, true);
- $users = [];
- foreach ($results as $user_array) {
- $name = $user_array['name'] ?? null;
- $age = $user_array['age'] ?? null;
- $address_street = $user_array['address']['street'] ?? null;
- $address_zipcode = $user_array['address']['zip_code'] ?? null;
- if (!is_string($name) || !is_int($age) || !is_string($address_street) || !is_string($address_zipcode)) {
- throw new Exception('Invalid JSON');
- }
- $address = new Address($address_street, $address_zipcode);
- $user = new User($name, $age, $address);
- $users[] = $user;
- }
- Performance::finish();
- }
- $points = Performance::export()->get()['points'];
- $fancy_total = 0;
- $fancy_count = 0;
- $regular_total = 0;
- $regular_count = 0;
- /** @var Point $point */
- foreach ($points as $point) {
- $label = $point->getLabel();
- if (strpos($label, 'Fancy Decode') === 0) {
- $fancy_count++;
- $fancy_total += $point->getDifferenceTime();
- } elseif (strpos($label, 'JSON Decode') === 0) {
- $regular_count++;
- $regular_total += $point->getDifferenceTime();
- }
- }
- $config = new ConfigHandler();
- $formatter = new Formatter($config);
- $fancy_average = $formatter->timeToHuman($fancy_total / $fancy_count);
- echo "Fancy Average time $fancy_average" . PHP_EOL;
- if ($regular_count) {
- $regular_average = $formatter->timeToHuman($regular_total / $regular_count);
- echo "Regular Average time $regular_average" . PHP_EOL;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement