Browse Source

Changed template rendering and added ...

... a raw test for db (dbal instead of doctrine) as well as fortunes
(dbal + php template).

Also changed the routing from annotation to yml files, since it seems to
be more used in the symfony documentation.  This allowed to remove the
FrameworkExtraBundle from SensioLabs, from which the template and
routing annotation came.
Skamander 12 years ago
parent
commit
d70836f565

+ 1 - 1
php-symfony2/app/config/config.yml

@@ -13,7 +13,7 @@ framework:
     csrf_protection: ~
     validation:      { enable_annotations: true }
     templating:
-        engines: ['twig']
+        engines: ['twig', 'php']
         #assets_version: SomeVersionScheme
     default_locale:  "%locale%"
     trusted_proxies: ~

+ 19 - 3
php-symfony2/app/config/routing.yml

@@ -1,3 +1,19 @@
-_bench:
-    resource: "@SkamanderBenchmarkBundle/Controller/BenchController.php"
-    type:     annotation
+_json:
+    pattern:  /json
+    defaults: { _controller: SkamanderBenchmarkBundle:Bench:json }
+
+_db:
+    pattern:  /db
+    defaults: { _controller: SkamanderBenchmarkBundle:Bench:db }
+
+_dbRaw:
+    pattern:  /db-raw
+    defaults: { _controller: SkamanderBenchmarkBundle:Bench:dbRaw }
+
+_fortunes:
+    pattern:  /fortunes
+    defaults: { _controller: SkamanderBenchmarkBundle:Bench:fortunes }
+
+_fortunesRaw:
+    pattern:  /fortunes-raw
+    defaults: { _controller: SkamanderBenchmarkBundle:Bench:fortunesRaw }

+ 8 - 0
php-symfony2/benchmark_config

@@ -9,6 +9,14 @@
       "fortune_url": "/fortunes",
       "port": 8080,
       "sort": 51
+    },
+    "raw": {
+      "setup_file": "setup",
+      "db_url": "/db-raw",
+      "query_url": "/db-raw?queries=",
+      "fortune_url": "/fortunes-raw",
+      "port": 8080,
+      "sort": 83
     }
   }]
 }

+ 44 - 15
php-symfony2/src/Skamander/BenchmarkBundle/Controller/BenchController.php

@@ -3,27 +3,18 @@
 namespace Skamander\BenchmarkBundle\Controller;
 
 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
-use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
-use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
 use Symfony\Component\HttpFoundation\JsonResponse;
 use Symfony\Component\HttpFoundation\Request;
 use Skamander\BenchmarkBundle\Entity\Fortune;
 
 class BenchController extends Controller
 {
-    /**
-     * @Route("/json", name="_json")
-     */
+
     public function jsonAction()
     {
         return new JsonResponse(array('message' => 'Hello World!'));
     }
 
-    /**
-     * @Route("/db", name="_db")
-     *
-     * Used db?queries={queries} instead of db/{queries} to align the test with most of the other tests
-     */
     public function dbAction(Request $request)
     {
         $queries = $request->query->getInt('queries', 1);
@@ -40,10 +31,21 @@ class BenchController extends Controller
         return new JsonResponse($worlds);
     }
 
-    /**
-     * @Route("/fortunes", name="_fortunes")
-     * @Template
-     */
+    public function dbRawAction(Request $request)
+    {
+        $queries = $request->query->getInt('queries', 1);
+
+        // possibility for enhancement is the use of SplFixedArray -> http://php.net/manual/de/class.splfixedarray.php
+        $worlds = array();
+        $conn = $this->get('database_connection');
+
+        for($i = 0; $i < $queries; ++$i) {
+            $worlds[] =  $conn->fetchAssoc('SELECT * FROM World WHERE id = ?', array(mt_rand(1, 10000)));
+        }
+
+        return new JsonResponse($worlds);
+    }
+
     public function fortunesAction()
     {
         $repo = $this->getDoctrine()
@@ -66,6 +68,33 @@ class BenchController extends Controller
             }
         });
 
-        return ['fortunes' => $fortunes];
+        return $this->render("SkamanderBenchmarkBundle:Bench:fortunes.html.twig", [
+            'fortunes' => $fortunes
+        ]);
+    }
+
+    public function fortunesRawAction()
+    {
+        $conn = $this->get('database_connection');
+        $fortunes = $conn->fetchAll('SELECT * FROM Fortune');
+
+        $fortunes[] = [
+            'id' => 0,
+            'message' => 'Additional fortune added at request time.'
+        ];
+
+        usort($fortunes, function($left, $right) {
+            if ($left['message'] === $right['message']) {
+                return 0;
+            } else if ($left['message'] > $right['message']) {
+                return 1;
+            } else {
+                return -1;
+            }
+        });
+
+        return $this->render("SkamanderBenchmarkBundle:Bench:fortunes.html.php", [
+            'fortunes' => $fortunes
+        ]);
     }
 }

+ 25 - 0
php-symfony2/src/Skamander/BenchmarkBundle/Resources/views/Bench/fortunes.html.php

@@ -0,0 +1,25 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="utf-8">
+    <title>Benchmark</title>
+</head>
+<body>
+
+<table>
+    <tr>
+        <th>id</th>
+        <th>message</th>
+    </tr>
+
+    <?php foreach($fortunes as $fortune): ?>
+        <tr>
+            <td><?php echo $fortune['id']; ?></td>
+            <td><?php echo htmlspecialchars($fortune['message'], ENT_QUOTES, "UTF-8", false); ?></td>
+        </tr>
+    <?php endforeach; ?>
+
+</table>
+
+</body>
+</html>