Skip to content

Commit

Permalink
Merge pull request #6 from absolvent/issue/ATP-439
Browse files Browse the repository at this point in the history
ATP-439 Optimize swagger.yml loading
  • Loading branch information
majkel89 authored Jan 10, 2018
2 parents 4f7a8ee + 9735454 commit 46d3117
Show file tree
Hide file tree
Showing 5 changed files with 713 additions and 322 deletions.
34 changes: 34 additions & 0 deletions app/Benchmarks/YamlParsingBenchmark.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Absolvent\swagger\Benchmarks;

use Athletic\AthleticEvent;
use Symfony\Component\Yaml\Yaml;

final class YamlParsingBenchmark extends AthleticEvent
{
/** @var string */
private $swaggerFile;

public function __construct()
{
$this->swaggerFile = 'fixtures/petstore-expanded.yml';
}

/**
* @iterations 1000
*/
public function yamlExtensionParsing()
{
yaml_parse_file($this->swaggerFile);
}


/**
* @iterations 1000
*/
public function symfonyYmlParsing()
{
Yaml::parse(file_get_contents($this->swaggerFile));
}
}
12 changes: 10 additions & 2 deletions app/SwaggerSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Yaml\Yaml;
use Exception;

class SwaggerSchema extends Data
{
Expand All @@ -22,8 +23,15 @@ class SwaggerSchema extends Data

public static function fromFilename(string $filename): SwaggerSchema
{
$schema = Yaml::parse(file_get_contents($filename));

if (function_exists('yaml_parse_file')) {
try {
$schema = yaml_parse_file($filename);
} catch (Exception $e) {
$schema = [];
}
} else {
$schema = Yaml::parse(file_get_contents($filename));
}
return new static($schema, $filename);
}

Expand Down
7 changes: 7 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"symfony/yaml": "^3.2"
},
"require-dev": {
"athletic/athletic": "^0.1.8",
"barryvdh/laravel-ide-helper": "^2.3",
"fzaninotto/faker": "~1.4",
"mockery/mockery": "0.9.*",
Expand All @@ -25,6 +26,9 @@
"Absolvent\\swagger\\tests\\": "tests/"
}
},
"suggest": {
"ext-yaml": "For high performance yaml parsing (RECOMMENDED FOR PRODUCTION)"
},
"scripts": {
"post-root-package-install": [
"php -r \"file_exists('.env') || copy('.env.example', '.env');\""
Expand All @@ -39,6 +43,9 @@
"post-update-cmd": [
"Illuminate\\Foundation\\ComposerScripts::postUpdate",
"php artisan optimize"
],
"benchmark": [
"php ./vendor/bin/athletic -p ./app/Benchmarks -b ./vendor/autoload.php"
]
},
"config": {
Expand Down
Loading

0 comments on commit 46d3117

Please sign in to comment.