Explorar o código

Merge branch 'phpalcon-mongodb' of https://github.com/kpacha/FrameworkBenchmarks into PR673

Conflicts:
	php-phalcon/README.md
	php-phalcon/app/config/routes.php
	php-phalcon/app/controllers/BenchController.php
Mike Smith %!s(int64=11) %!d(string=hai) anos
pai
achega
9d1a53f96d

+ 17 - 6
php-phalcon/README.md

@@ -9,9 +9,10 @@ Uses the PHP standard [JSON encoder](http://www.php.net/manual/en/function.json-
 
 
 ### Data-Store/Database Mapping Test
-Uses the built-in ORM of Phalcon PHP
+Uses the built-in ORM/ODM of Phalcon PHP
 
-* [DB test controller](app/controllers/BenchController.php)
+* MySQL: [DB test controller](app/controllers/BenchController.php)
+* MongoDB: [DB test controller](app/controllers/MongobenchController.php)
 
 ### Template Test
 Uses Phalcons template engine 'Volt'
@@ -26,6 +27,7 @@ The tests were run with:
 * [PHP Version 5.4.13](http://www.php.net/) with FPM, APC and Phalcon extension
 * [nginx 1.4.0](http://nginx.org/)
 * [MySQL 5.5.29](https://dev.mysql.com/)
+* [MongoDB 2.4.8](https://mongodb.org/)
 
 ## Test URLs
 ### JSON Encoding Test
@@ -34,16 +36,25 @@ http://localhost/json
 
 ### Data-Store/Database Mapping Test
 
-http://localhost/db
+MySQL: http://localhost/db
+MongoDB: http://localhost/mongodb/db
 
 ### Variable Query Test
     
-http://localhost/db?queries=2
+MySQL: http://localhost/db?queries=2
+MongoDB: http://localhost/mongodb/db?queries=2
 
-### Templating Test
+### Update Test
+    
+MySQL: http://localhost/update
 
 http://localhost/fortunes
 
 ### Plaintext Test
 
-http://localhost/plaintext
+http://localhost/plaintext
+
+### Fortunes Test
+    
+MySQL: http://localhost/fortunes
+MongoDB: http://localhost/mongodb/fortunes

+ 4 - 0
php-phalcon/app/config/config.php

@@ -8,6 +8,10 @@ return new \Phalcon\Config(array(
         'password' => 'benchmarkdbpass',
         'name'     => 'hello_world',
     ),
+    'mongodb'     => array(
+        'url'     => 'mongodb://localhost:27017',
+        'db'      => 'hello_world'
+    ),
     'application' => array(
         'controllersDir' => APP_PATH . '/app/controllers/',
         'modelsDir'      => APP_PATH . '/app/models/',

+ 11 - 0
php-phalcon/app/config/routes.php

@@ -28,4 +28,15 @@ $router->add('/plaintext', array(
     'action' => 'plaintext',
 ));
 
+// Handles "/db" as well as "/db?queries={queries}"
+$router->add('/mongodb/db', array(
+    'controller' => 'mongobench',
+    'action' => 'db',
+));
+
+$router->add('/mongodb/fortunes', array(
+    'controller' => 'mongobench',
+    'action' => 'fortunes',
+));
+
 return $router;

+ 57 - 35
php-phalcon/app/controllers/BenchController.php

@@ -5,26 +5,29 @@ use Phalcon\Mvc\View,
 
 class BenchController extends \Phalcon\Mvc\Controller
 {
+
     public function initialize()
     {
         // views must be renderd explicitly. safes processing time when not needed.
         $this->view->setRenderLevel(View::LEVEL_LAYOUT);
     }
 
-    public function jsonAction() {
+    public function jsonAction()
+    {
         return $this->sendContentAsJson(array(
             'message' => 'Hello, World!'
         ));
     }
 
-    public function dbAction() {
+    public function dbAction()
+    {
 
         $queries = $this->request->getQuery('queries', null, 1);
 
         $worlds = array();
 
         for ($i = 0; $i < $queries; ++$i) {
-            $worlds[] = Worlds::findFirst(mt_rand(1, 10000));
+            $worlds[] = $this->getRandomWorld();
         }
 
         if (count($worlds) == 1) {
@@ -35,49 +38,27 @@ class BenchController extends \Phalcon\Mvc\Controller
         }
     }
 
-    public function fortunesAction() {
-
-        // since the resultset is immutable get an array instead
-        // so we can add the new fortune
-        $fortunes = Fortunes::find()->toArray();
-
-        $fortunes[] = array(
-            'id' => 0,
-            'message' => 'Additional fortune added at request time.'
-        );
+    public function fortunesAction()
+    {
 
-        usort($fortunes, function($left, $right) {
-            $l = $left['message'];
-            $r = $right['message'];
-            if ($l === $r) {
-                return 0;
-            } else {
-                if ($l > $r) {
-                    return 1;
-                } else {
-                    return -1;
-                }
-            }
-        });
+        $fortunes = $this->getFortunesArray();
+        $fortunes[] = $this->buildFortune();
 
         $this->response->setHeader("Content-Type", "text/html; charset=utf-8");
 
-        $this->view->fortunes = $fortunes;
+        $this->view->fortunes = $this->sortFortunes($fortunes);
     }
 
-    public function updateAction() {
+    public function updateAction()
+    {
 
         $queries = $this->request->getQuery('queries', null, 1);
-        if($queries < 1) {
-            $queries = 1;
-        } else if ($queries > 500) {
-            $queries = 500;
-        }
+        $queries = max(1, min(500, $queries));
 
         $worlds = array();
 
         for ($i = 0; $i < $queries; ++$i) {
-            $world = Worlds::findFirst(mt_rand(1, 10000));
+            $world = $this->getRandomWorld();
             $world->randomNumber = mt_rand(1, 10000);
             $world->save();
             $worlds[] = $world;
@@ -95,9 +76,50 @@ class BenchController extends \Phalcon\Mvc\Controller
         $this->response->send();
     }
 
-    private function sendContentAsJson($content) {
+    protected function getRandomWorld()
+    {
+        return Worlds::findFirst(mt_rand(1, 10000));
+    }
+
+    protected function getFortunesArray()
+    {
+        // since the resultset is immutable get an array instead
+        // so we can add the new fortune
+        return Fortunes::find()->toArray();
+    }
+
+    protected function buildFortune()
+    {
+        return array(
+            'id' => 0,
+            'message' => 'Additional fortune added at request time.'
+        );
+    }
+
+    protected function sortFortunes($fortunes)
+    {
+        usort($fortunes,
+                function($left, $right) {
+                    $l = $left['message'];
+                    $r = $right['message'];
+                    if ($l === $r) {
+                        return 0;
+                    } else {
+                        if ($l > $r) {
+                            return 1;
+                        } else {
+                            return -1;
+                        }
+                    }
+                });
+        return $fortunes;
+    }
+
+    private function sendContentAsJson($content)
+    {
         $response = new Phalcon\Http\Response(json_encode($content));
         $response->setHeader("Content-Type", "application/json");
         return $response;
     }
+
 }

+ 47 - 0
php-phalcon/app/controllers/MongobenchController.php

@@ -0,0 +1,47 @@
+<?php
+
+use Phalcon\Mvc\View,
+    Phalcon\Mvc\Model\Resultset;
+
+class MongobenchController extends BenchController
+{
+
+    protected function getRandomWorld()
+    {
+        return WorldsCollection::findFirst(array(array('_id' => mt_rand(1, 10000))));
+    }
+
+    protected function getFortunesArray()
+    {
+        return FortunesCollection::find();
+    }
+
+    protected function buildFortune()
+    {
+        $fortune = parent::buildFortune();
+        $newFortune = new FortunesCollection();
+        $newFortune->_id = $fortune['id'];
+        $newFortune->message = $fortune['message'];
+        return $newFortune;
+    }
+
+    protected function sortFortunes($fortunes)
+    {
+        usort($fortunes,
+                function($left, $right) {
+                    $l = $left->message;
+                    $r = $right->message;
+                    if ($l === $r) {
+                        return 0;
+                    } else {
+                        if ($l > $r) {
+                            return 1;
+                        } else {
+                            return -1;
+                        }
+                    }
+                });
+        return $fortunes;
+    }
+
+}

+ 14 - 0
php-phalcon/app/models/FortunesCollection.php

@@ -0,0 +1,14 @@
+<?php
+
+class FortunesCollection extends \Phalcon\Mvc\Collection
+{
+
+    public $_id;
+    public $message;
+
+    public function getSource()
+    {
+        return "fortune";
+    }
+
+}

+ 14 - 0
php-phalcon/app/models/WorldsCollection.php

@@ -0,0 +1,14 @@
+<?php
+
+class WorldsCollection extends \Phalcon\Mvc\Collection
+{
+
+    public $_id;
+    public $randomNumber;
+
+    public function getSource()
+    {
+        return "world";
+    }
+
+}

+ 1 - 0
php-phalcon/app/views/mongobench/fortunes.volt

@@ -0,0 +1 @@
+<table><tr><th>id</th><th>message</th></tr>{% for fortune in fortunes %}<tr><td>{{ fortune._id }}</td><td>{{ fortune.message | e }}</td></tr>{% endfor %}</table>

+ 20 - 0
php-phalcon/benchmark_config

@@ -22,6 +22,26 @@
       "display_name": "phalcon",
       "notes": "",
       "versus": "php"
+    },
+    "mongodb": {
+      "setup_file": "setup",
+      "db_url": "/mongodb/db",
+      "query_url": "/mongodb/db?queries=",
+      "fortune_url": "/mongodb/fortunes",
+      "port": 8080,
+      "approach": "Realistic",
+      "classification": "Fullstack",
+      "database": "MongoDB",
+      "framework": "phalcon",
+      "language": "PHP",
+      "orm": "Full",
+      "platform": "PHP-FPM",
+      "webserver": "nginx",
+      "os": "Linux",
+      "database_os": "Linux",
+      "display_name": "phalcon",
+      "notes": "",
+      "versus": "php"
     }
   }]
 }

+ 17 - 0
php-phalcon/public/index.php

@@ -72,6 +72,23 @@ try {
         ));
     });
 
+    // Setting up the mongodb connection
+    $di->set('mongo', function() use ($config) {
+        $mongodbConfig = $config->mongodb;
+        
+        $mongo = new \MongoClient($mongodbConfig->url);
+        return $mongo->{$mongodbConfig->db};
+    });
+    
+    //Registering the collectionManager service
+    $di->set('collectionManager', function() {
+        // Setting a default EventsManager
+        $modelsManager = new Phalcon\Mvc\Collection\Manager();
+        $modelsManager->setEventsManager(new Phalcon\Events\Manager());
+        return $modelsManager;
+
+    }, true);
+
     // Handle the request
     $application = new \Phalcon\Mvc\Application();
     $application->setDI($di);

+ 2 - 1
php-phalcon/setup.py

@@ -6,6 +6,7 @@ from os.path import expanduser
 home = expanduser("~")
 
 def start(args, logfile, errfile):
+  setup_util.replace_text("php-phalcon/app/config/config.php", "mongodb:\/\/localhost", "mongodb://" + args.database_host)
   setup_util.replace_text("php-phalcon/app/config/config.php", "localhost", ""+ args.database_host +"")
   setup_util.replace_text("php-phalcon/deploy/nginx.conf", "root .*\/FrameworkBenchmarks", "root " + home + "/FrameworkBenchmarks")
 
@@ -23,4 +24,4 @@ def stop(logfile, errfile):
     subprocess.check_call("sudo chown -R $USER:$USER php-phalcon", shell=True, stderr=errfile, stdout=logfile)
     return 0
   except subprocess.CalledProcessError:
-    return 1
+    return 1