Browse Source

Test re-implemented using Slim3 framework

Slim3 managed as Composer dependency.
DB tests done using PDO.
Adam Brodziak 9 years ago
parent
commit
c9cfac5932

+ 1 - 0
frameworks/PHP/slim/.gitignore

@@ -9,3 +9,4 @@ deploy/php-fpm.pid
 .DS_Store
 .DS_Store
 /tags
 /tags
 .idea
 .idea
+/vendor

+ 2 - 3
frameworks/PHP/slim/README.md

@@ -9,7 +9,7 @@ Uses the PHP standard [JSON encoder](http://www.php.net/manual/en/function.json-
 
 
 
 
 ### Data-Store/Database Mapping Test
 ### Data-Store/Database Mapping Test
-Uses the RedBeanPHP ORM
+Uses the PDO access layer
 
 
 * [DB test controller](index.php)
 * [DB test controller](index.php)
 
 
@@ -17,8 +17,7 @@ Uses the RedBeanPHP ORM
 ## Infrastructure Software Versions
 ## Infrastructure Software Versions
 The tests were run with:
 The tests were run with:
 
 
-* [Slim 2.2.0](http://www.slimframework.com/)
-* [RedBeanPHP 3.4.2](http://redbeanphp.com/)
+* [Slim 3.3.0](http://www.slimframework.com/)
 * [PHP Version 7.0.1](http://www.php.net/) with FPM and APC
 * [PHP Version 7.0.1](http://www.php.net/) with FPM and APC
 * [nginx 1.9.9](http://nginx.org/)
 * [nginx 1.9.9](http://nginx.org/)
 * [MySQL 5.5.29](https://dev.mysql.com/)
 * [MySQL 5.5.29](https://dev.mysql.com/)

+ 1 - 17
frameworks/PHP/slim/composer.json

@@ -1,21 +1,5 @@
 {
 {
-    "name": "slim/slim",
-    "type": "library",
-    "description": "Slim Framework, a PHP micro framework",
-    "keywords": ["microframework","rest","router"],
-    "homepage": "http://github.com/codeguy/Slim",
-    "license": "MIT",
-    "authors": [
-        {
-            "name": "Josh Lockhart",
-            "email": "[email protected]",
-            "homepage": "http://www.joshlockhart.com/"
-        }
-    ],
     "require": {
     "require": {
-        "php": ">=5.3.0"
-    },
-    "autoload": {
-        "psr-0": { "Slim": "." }
+        "slim/slim": "3.3.0"
     }
     }
 }
 }

+ 38 - 62
frameworks/PHP/slim/index.php

@@ -1,85 +1,61 @@
 <?php
 <?php
-/**
- * Step 1: Require the Slim Framework
- *
- * If you are not using Composer, you need to require the
- * Slim Framework and register its PSR-0 autoloader.
- *
- * If you are using Composer, you can skip this step.
- */
-require 'Slim/Slim.php';
-require 'Slim/RedBean/rb.php';
+error_reporting(-1);
 
 
-\Slim\Slim::registerAutoloader();
+require_once __DIR__.'/vendor/autoload.php';
 
 
-# Turn off 'beautiful' column names (converting tables names from camelCase to snake_case).
-RedBean_OODBBean::setFlagBeautifulColumnNames(false); 
+$app = new \Slim\App;
 
 
-R::setup('mysql:host=127.0.0.1;dbname=hello_world','benchmarkdbuser','benchmarkdbpass');
-R::freeze(true);
-
-/**
- * Step 2: Instantiate a Slim application
- *
- * This example instantiates a Slim application using
- * its default settings. However, you will usually configure
- * your Slim application now by passing an associative array
- * of setting names and values into the application constructor.
- */
-$app = new \Slim\Slim();
-
-/**
- * Step 3: Define the Slim application routes
- *
- * Here we define several Slim application routes that respond
- * to appropriate HTTP request methods. In this example, the second
- * argument for `Slim::get`, `Slim::post`, `Slim::put`, and `Slim::delete`
- * is an anonymous function.
- */
-
-$app->get('/json', function () use($app) {
-    $app->contentType('application/json');
-    echo json_encode(array('message' => 'Hello, World!'));
+// Test 1: JSON serialization
+$app->get('/json', function ($request, $response) {
+    return $response
+        ->withJson(array('message' => 'Hello, World!'))
+        ->withHeader('Content-Type', 'application/json') // fixes utf-8 warning
+        ;
 });
 });
 
 
-$app->get('/db', function () use($app) {
-    $world = R::load('World', mt_rand(1, 10000))->export();
+$container = $app->getContainer();
+$container['db'] = function ($c) {
+    $db = $c['settings']['db'];
+    $pdo = new PDO('mysql:host=localhost;dbname=hello_world', 'benchmarkdbuser', 'benchmarkdbpass');
+    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+    $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
+    return $pdo;
+};
+
+// Test 2: Single database query
+$app->get('/db', function ($request, $response) {
+    $sth = $this->db->prepare('SELECT * FROM World WHERE id = ?');
+    $sth->execute(array(mt_rand(1, 10000)));
+    $world = $sth->fetch();
     # Cast fields to int so they don't get wrapped with quotes
     # Cast fields to int so they don't get wrapped with quotes
     $world['id'] = (int) $world['id'];
     $world['id'] = (int) $world['id'];
     $world['randomNumber'] = (int) $world['randomNumber'];
     $world['randomNumber'] = (int) $world['randomNumber'];
 
 
-    $app->contentType('application/json');
-    echo json_encode($world);
+    return $response
+        ->withJson($world)
+        ->withHeader('Content-Type', 'application/json') // fixes utf-8 warning
+        ;
 });
 });
 
 
-$app->get('/dbs', function () use($app) {
-    $queries = ($app->request()->get('queries') !== null)
-        ? $app->request()->get('queries')
-        : 1;
-    if ($queries < 1) {
-        $queries = 1;
-    }
-    else if ($queries > 500) {
-        $queries = 500;
-    }
-    $worlds = array();
+// Test 3: Multiple database queries
+$app->get('/dbs', function ($request, $response) {
+    $queries = max(1, min($request->getParam('queries'), 500));
 
 
+    $sth = $this->db->prepare('SELECT * FROM World WHERE id = ?');
+    $worlds = array();
     for ($i = 0; $i < $queries; ++$i) {
     for ($i = 0; $i < $queries; ++$i) {
-        $world = R::load('World', mt_rand(1, 10000))->export();
+        $sth->execute(array(mt_rand(1, 10000)));
+        $world = $sth->fetch();
         # Cast fields to int so they don't get wrapped with quotes
         # Cast fields to int so they don't get wrapped with quotes
         $world['id'] = (int) $world['id'];
         $world['id'] = (int) $world['id'];
         $world['randomNumber'] = (int) $world['randomNumber'];
         $world['randomNumber'] = (int) $world['randomNumber'];
         $worlds[] = $world;
         $worlds[] = $world;
     }
     }
 
 
-    $app->contentType('application/json');
-    echo json_encode($worlds);
+    return $response
+        ->withJson($worlds)
+        ->withHeader('Content-Type', 'application/json') // fixes utf-8 warning
+        ;
 });
 });
 
 
-/**
- * Step 4: Run the Slim application
- *
- * This method should be called last. This executes the Slim application
- * and returns the HTTP response to the HTTP client.
- */
 $app->run();
 $app->run();

+ 1 - 1
frameworks/PHP/slim/setup.sh

@@ -1,6 +1,6 @@
 #!/bin/bash
 #!/bin/bash
 
 
-fw_depends php7 nginx
+fw_depends php7 nginx composer
 
 
 sed -i 's|localhost|'"${DBHOST}"'|g' index.php
 sed -i 's|localhost|'"${DBHOST}"'|g' index.php
 sed -i 's|root .*/FrameworkBenchmarks/php-slim| root '"${TROOT}"'|g' deploy/nginx.conf
 sed -i 's|root .*/FrameworkBenchmarks/php-slim| root '"${TROOT}"'|g' deploy/nginx.conf

+ 1 - 1
frameworks/PHP/slim/setup_hhvm.sh

@@ -1,6 +1,6 @@
 #!/bin/bash
 #!/bin/bash
 
 
-fw_depends php nginx hhvm
+fw_depends php nginx composer hhvm
 
 
 sed -i 's|localhost|'"${DBHOST}"'|g' index.php
 sed -i 's|localhost|'"${DBHOST}"'|g' index.php
 sed -i 's|SourceRoot = .*/FrameworkBenchmarks/php-slim|SourceRoot = '"${TROOT}"'|g' deploy/config.hdf
 sed -i 's|SourceRoot = .*/FrameworkBenchmarks/php-slim|SourceRoot = '"${TROOT}"'|g' deploy/config.hdf

+ 1 - 1
frameworks/PHP/slim/setup_php5.sh

@@ -1,6 +1,6 @@
 #!/bin/bash
 #!/bin/bash
 
 
-fw_depends php nginx
+fw_depends php nginx composer
 
 
 sed -i 's|localhost|'"${DBHOST}"'|g' index.php
 sed -i 's|localhost|'"${DBHOST}"'|g' index.php
 sed -i 's|root .*/FrameworkBenchmarks/php-slim| root '"${TROOT}"'|g' deploy/nginx.conf
 sed -i 's|root .*/FrameworkBenchmarks/php-slim| root '"${TROOT}"'|g' deploy/nginx.conf