|
@@ -1,5 +1,12 @@
|
|
<?php
|
|
<?php
|
|
|
|
|
|
|
|
+if ( ! isset($_SERVER['argv'][1], $_SERVER['argv'][2])) {
|
|
|
|
+ echo 'Usage: php benchmark.php <format> <iterations> [output-file]'.PHP_EOL;
|
|
|
|
+ exit(1);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+list(, $format, $iterations) = $_SERVER['argv'];
|
|
|
|
+
|
|
require_once 'bootstrap.php';
|
|
require_once 'bootstrap.php';
|
|
|
|
|
|
function benchmark(\Closure $f, $times = 10) {
|
|
function benchmark(\Closure $f, $times = 10) {
|
|
@@ -29,14 +36,27 @@ function createObject() {
|
|
return $post;
|
|
return $post;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
|
|
+
|
|
$serializer = \JMS\Serializer\SerializerBuilder::create()->build();
|
|
$serializer = \JMS\Serializer\SerializerBuilder::create()->build();
|
|
$collection = createCollection();
|
|
$collection = createCollection();
|
|
$metrics = array();
|
|
$metrics = array();
|
|
|
|
+$f = function() use ($serializer, $collection, $format) {
|
|
|
|
+ $serializer->serialize($collection, $format);
|
|
|
|
+};
|
|
|
|
|
|
-foreach (array('json', 'yml', 'xml') as $format) {
|
|
|
|
- $metrics['benchmark-collection-'.$format] = benchmark(function() use ($serializer, $collection, $format) {
|
|
|
|
- $serializer->serialize($collection, $format);
|
|
|
|
- }, 10);
|
|
|
|
-}
|
|
|
|
|
|
|
|
-echo json_encode(array('metrics' => $metrics));
|
|
|
|
|
|
+// Load all necessary classes into memory.
|
|
|
|
+benchmark($f, 1);
|
|
|
|
+
|
|
|
|
+printf('Benchmarking collection for format "%s".'.PHP_EOL, $format);
|
|
|
|
+$metrics['benchmark-collection-'.$format] = benchmark($f, $iterations);
|
|
|
|
+
|
|
|
|
+$output = json_encode(array('metrics' => $metrics));
|
|
|
|
+
|
|
|
|
+if (isset($_SERVER['argv'][3])) {
|
|
|
|
+ file_put_contents($_SERVER['argv'][3], $output);
|
|
|
|
+ echo "Done.".PHP_EOL;
|
|
|
|
+} else {
|
|
|
|
+ echo $output.PHP_EOL;
|
|
|
|
+}
|