Created
March 17, 2014 19:37
-
-
Save webmozart/9606689 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once __DIR__.'/vendor/autoload.php'; | |
use Symfony\Component\Validator\Validation; | |
use Symfony\Component\Validator\Constraints\Length; | |
use Symfony\Component\Validator\Constraints\NotNull; | |
use Symfony\Component\Validator\Constraints\Valid; | |
use Symfony\Component\Validator\Constraints\Range; | |
$validator = Validation::createValidatorBuilder() | |
// ->setApiVersion(Validation::API_VERSION_2_4) | |
->setApiVersion(Validation::API_VERSION_2_5) | |
// ->setApiVersion(Validation::API_VERSION_2_4 | Validation::API_VERSION_2_5) | |
->enableAnnotationMapping() | |
->getValidator(); | |
class BenchArticle | |
{ | |
/** | |
* @NotNull | |
* @Length(min=3) | |
*/ | |
public $title; | |
/** | |
* @NotNull | |
* @Range(min=2, max=50) | |
*/ | |
public $number; | |
/** | |
* @NotNull | |
*/ | |
public $text; | |
/** | |
* @Valid | |
*/ | |
public $tags; | |
} | |
class BenchTag | |
{ | |
/** | |
* @NotNull | |
* @Length(max=10) | |
*/ | |
public $name; | |
public function __construct($name) | |
{ | |
$this->name = $name; | |
} | |
} | |
$article = new BenchArticle(); | |
$article->title = 'fo'; | |
$article->text = 'Lorem ipsum dolor'; | |
$article->number = 60; | |
$article->tags = array(); | |
$article->tags[] = new BenchTag('symfony'); | |
$article->tags[] = new BenchTag('validator'); | |
$article->tags[] = new BenchTag('benchmark'); | |
// Validate once to autoload all needed classes | |
$validator->validate($article); | |
$time = microtime(true); | |
for ($i = 0; $i < 20; ++$i) { | |
$validator->validate($article); | |
} | |
echo 1000*(microtime(true)-$time)."ms\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment