Browse Source

Merge branch 'symfony-fortunes' of https://github.com/Skamander/FrameworkBenchmarks into Skamander-symfony-fortunes

Patrick Falls 12 years ago
parent
commit
d8196590bd

+ 1 - 0
php-symfony2/.gitignore

@@ -2,6 +2,7 @@
 /app/logs
 /bin
 /vendors
+/vendor
 /build
 /dist
 .DS_Store

+ 9 - 0
php-symfony2/README.md

@@ -14,6 +14,11 @@ Uses the Symfony 2/Doctrine 2 Entity functionality.
 * [DB test controller](src/Skamander/BenchmarkBundle/Controller/BenchController.php)
 * [DB test model](src/Skamander/BenchmarkBundle/Entity/World.php)
 
+### Template Test
+Uses Symfony's template engine 'Twig'
+
+* [Template test controller](src/Skamander/BenchmarkBundle/Controller/BenchController.php)
+
 
 ## Infrastructure Software Versions
 The tests were run with:
@@ -35,3 +40,7 @@ http://localhost/db
 ### Variable Query Test
     
 http://localhost/db?queries=2
+
+### Templating Test
+
+http://localhost/fortunes

+ 12 - 0
php-symfony2/app/Resources/views/base.html.php

@@ -0,0 +1,12 @@
+<!DOCTYPE html>
+<html>
+    <head>
+        <meta charset="UTF-8" />
+        <title><?php $view['slots']->output('title') ?></title>
+        <?php $view['slots']->output('stylesheets') ?>
+    </head>
+    <body>
+        <?php $view['slots']->output('body') ?>
+        <?php $view['slots']->output('javascripts') ?>
+    </body>
+</html>

+ 0 - 1
php-symfony2/app/Resources/views/base.html.twig

@@ -4,7 +4,6 @@
         <meta charset="UTF-8" />
         <title>{% block title %}Welcome!{% endblock %}</title>
         {% block stylesheets %}{% endblock %}
-        <link rel="icon" type="image/x-icon" href="{{ asset('favicon.ico') }}" />
     </head>
     <body>
         {% block body %}{% endblock %}

+ 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: ~

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

@@ -14,7 +14,7 @@ framework:
 doctrine:
     orm:
         metadata_cache_driver: apc
-#        result_cache_driver: apc
+        #result_cache_driver: apc
         query_cache_driver: apc
 
 monolog:

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

@@ -1,3 +1,23 @@
-_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 }
+
+_fortunesPhp:
+    pattern:  /fortunes-php
+    defaults: { _controller: SkamanderBenchmarkBundle:Bench:fortunesPhp }
+
+_fortunesRaw:
+    pattern:  /fortunes-raw
+    defaults: { _controller: SkamanderBenchmarkBundle:Bench:fortunesRaw }

+ 15 - 0
php-symfony2/benchmark_config

@@ -6,8 +6,23 @@
       "json_url": "/json",
       "db_url": "/db",
       "query_url": "/db?queries=",
+      "fortune_url": "/fortunes",
       "port": 8080,
       "sort": 51
+    },
+    "php-templates": {
+      "setup_file": "setup",
+      "fortune_url": "/fortunes-php",
+      "port": 8080,
+      "sort": 83
+    },
+    "raw": {
+      "setup_file": "setup",
+      "db_url": "/db-raw",
+      "query_url": "/db-raw?queries=",
+      "fortune_url": "/fortunes-raw",
+      "port": 8080,
+      "sort": 91
     }
   }]
 }

+ 90 - 9
php-symfony2/src/Skamander/BenchmarkBundle/Controller/BenchController.php

@@ -3,25 +3,19 @@
 namespace Skamander\BenchmarkBundle\Controller;
 
 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
-use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
 use Symfony\Component\HttpFoundation\JsonResponse;
 use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\Response;
+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);
@@ -37,4 +31,91 @@ class BenchController extends Controller
 
         return new JsonResponse($worlds);
     }
+
+    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()
+            ->getRepository('SkamanderBenchmarkBundle:Fortune');
+        $fortunes = $repo->findAll();
+
+        $runtimeFortune = new Fortune();
+        $runtimeFortune->setId(0)
+            ->setMessage('Additional fortune added at request time.');
+
+        $fortunes[] = $runtimeFortune;
+
+        usort($fortunes, function($left, $right) {
+            return strcmp($left->message, $right->message);
+        });
+
+        return $this->render("SkamanderBenchmarkBundle:Bench:fortunes.html.twig", [
+            'fortunes' => $fortunes
+        ]);
+    }
+
+    public function fortunesPhpAction()
+    {
+        $repo = $this->getDoctrine()
+            ->getRepository('SkamanderBenchmarkBundle:Fortune');
+        $fortunes = $repo->findAll();
+
+        $runtimeFortune = new Fortune();
+        $runtimeFortune->setId(0)
+            ->setMessage('Additional fortune added at request time.');
+
+        $fortunes[] = $runtimeFortune;
+
+        usort($fortunes, function($left, $right) {
+            return strcmp($left->message, $right->message);
+        });
+
+        return $this->render("SkamanderBenchmarkBundle:Bench:fortunes.html.php", [
+            'fortunes' => $fortunes
+        ]);
+    }
+
+    public function fortunesRawAction()
+    {
+        $repo = $this->getDoctrine()
+            ->getRepository('SkamanderBenchmarkBundle:Fortune');
+        $fortunes = $repo->findAll();
+
+        $runtimeFortune = new Fortune();
+        $runtimeFortune->setId(0)
+            ->setMessage('Additional fortune added at request time.');
+
+        $fortunes[] = $runtimeFortune;
+
+        usort($fortunes, function($left, $right) {
+            return strcmp($left->message, $right->message);
+        });
+
+        // This is not the symfony way to work with templates! It's implemented to show users
+        // who don't want to use template engines (like twig), or template sugar (like the slots etc.
+        // from symfony 2), because in their opinion already built-in php constructs like foreach +
+        // if else + include etc. are enough, that the performance impact should be neglectable, and
+        // that the advantages outweigh the disadvantages (performance).
+        $title = 'Fortunes';
+
+        ob_start();
+        include __DIR__ . '/../Resources/views/Bench/raw/content.php';
+        $response = ob_get_clean();
+
+        return new Response($response);
+    }
 }

+ 69 - 0
php-symfony2/src/Skamander/BenchmarkBundle/Entity/Fortune.php

@@ -0,0 +1,69 @@
+<?php
+
+namespace Skamander\BenchmarkBundle\Entity;
+
+use Doctrine\ORM\Mapping as ORM;
+
+/**
+ * @ORM\Entity
+ * @ORM\Table(name="Fortune")
+ */
+class Fortune
+{
+    /**
+     * @ORM\Id
+     * @ORM\Column(type="integer")
+     */
+    public $id;
+
+    /**
+     * @ORM\Column(type="string")
+     */
+    public $message;
+
+    /**
+     * Set id
+     *
+     * @param integer $id
+     * @return Fortune
+     */
+    public function setId($id)
+    {
+        $this->id = $id;
+
+        return $this;
+    }
+
+    /**
+     * Get id
+     *
+     * @return integer
+     */
+    public function getId()
+    {
+        return $this->id;
+    }
+
+    /**
+     * Set message
+     *
+     * @param string $message
+     * @return Fortune
+     */
+    public function setMessage($message)
+    {
+        $this->message = $message;
+
+        return $this;
+    }
+
+    /**
+     * Get message
+     *
+     * @return string
+     */
+    public function getMessage()
+    {
+        return $this->message;
+    }
+}

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

@@ -0,0 +1,18 @@
+<?php $view->extend('SkamanderBenchmarkBundle::layout.html.php') ?>
+
+<?php $view['slots']->set('title', 'Fortunes') ?>
+
+<?php $view['slots']->start('content') ?>
+<table>
+    <tr>
+        <th>id</th>
+        <th>message</th>
+    </tr>
+    <?php foreach($fortunes as $fortune): ?>
+        <tr>
+            <td><?php echo $view->escape($fortune->id); ?></td>
+            <td><?php echo $view->escape($fortune->message); ?></td>
+        </tr>
+    <?php endforeach; ?>
+</table>
+<?php $view['slots']->stop() ?>

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

@@ -0,0 +1,20 @@
+{% extends "SkamanderBenchmarkBundle::layout.html.twig" %}
+
+{% block title "Fortunes" %}
+
+{% block content %}
+    <table>
+        <tr>
+            <th>id</th>
+            <th>message</th>
+        </tr>
+
+        {% for fortune in fortunes %}
+            <tr>
+                <td>{{ fortune.id }}</td>
+                <td>{{ fortune.message }}</td>
+            </tr>
+        {% endfor %}
+
+    </table>
+{% endblock %}

+ 7 - 0
php-symfony2/src/Skamander/BenchmarkBundle/Resources/views/Bench/raw/content.php

@@ -0,0 +1,7 @@
+<?php include 'template_header.php'; ?>
+
+<div class="block">
+    <?php include 'fortunes.php'; ?>
+</div>
+
+<?php include 'template_footer.php'; ?>

+ 14 - 0
php-symfony2/src/Skamander/BenchmarkBundle/Resources/views/Bench/raw/fortunes.php

@@ -0,0 +1,14 @@
+<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>

+ 2 - 0
php-symfony2/src/Skamander/BenchmarkBundle/Resources/views/Bench/raw/template_footer.php

@@ -0,0 +1,2 @@
+</body>
+</html>

+ 7 - 0
php-symfony2/src/Skamander/BenchmarkBundle/Resources/views/Bench/raw/template_header.php

@@ -0,0 +1,7 @@
+<!DOCTYPE html>
+<html>
+<head>
+    <meta charset="UTF-8" />
+    <title><?php echo $title ?></title>
+</head>
+<body>

+ 9 - 0
php-symfony2/src/Skamander/BenchmarkBundle/Resources/views/layout.html.php

@@ -0,0 +1,9 @@
+<?php $view->extend('::base.html.php') ?>
+
+<?php $view['slots']->set('title', 'Benchmark Bundle') ?>
+
+<?php $view['slots']->start('body') ?>
+    <div class="block">
+        <?php $view['slots']->output('content') ?>
+    </div>
+<?php $view['slots']->stop() ?>

+ 9 - 0
php-symfony2/src/Skamander/BenchmarkBundle/Resources/views/layout.html.twig

@@ -0,0 +1,9 @@
+{% extends '::base.html.twig' %}
+
+{% block title 'Benchmark Bundle' %}
+
+{% block body %}
+    <div class="block">
+        {% block content %}{% endblock %}
+    </div>
+{% endblock %}