Browse Source

Add to Yii2 orm test (#4705)

Sergei Sergeev 6 years ago
parent
commit
de72b6898e

+ 84 - 0
frameworks/PHP/yii2/app/controllers/RawController.php

@@ -0,0 +1,84 @@
+<?php
+
+namespace app\controllers;
+
+use app\helpers\Query;
+use Yii;
+use yii\web\Controller;
+
+class RawController extends Controller
+{
+    /**
+     * Test #2: Single Database Query
+     */
+    public function actionDb()
+    {
+        $statement = Yii::$app->db->createCommand('SELECT id, randomNumber FROM World WHERE id = :id');
+        $world = $statement->bindValue(':id', mt_rand(1, 10000))->queryOne();
+        $world['id'] = (int)$world['id'];
+        $world['randomNumber'] = (int)$world['randomNumber'];
+
+        return $this->asJson($world);
+    }
+
+    /**
+     * Test #3: Multiple Database Queries
+     */
+    public function actionQueries($queries)
+    {
+        $queries = Query::clamp($queries);
+
+        $statement = Yii::$app->db->createCommand('SELECT id, randomNumber FROM World WHERE id = :id');
+
+        $worlds = [];
+        while (0 < $queries--) {
+            $result = $statement->bindValue(':id', mt_rand(1, 10000))->queryOne();
+            $result['id'] = (int)$result['id'];
+            $result['randomNumber'] = (int)$result['randomNumber'];
+            $worlds[] = $result;
+        }
+
+        return $this->asJson($worlds);
+    }
+
+    /**
+     * Test #4: Fortunes
+     */
+    public function actionFortunes()
+    {
+        $fortunes = Yii::$app->db->createCommand('SELECT id, message FROM Fortune')->queryAll();
+        $fortunes[] = ['id' => 0, 'message' => 'Additional fortune added at request time.'];
+
+        usort($fortunes, function ($left, $right) {
+            return strcmp($left['message'], $right['message']);
+        });
+
+        $this->view->title = 'Fortunes';
+
+        return $this->render('fortunes', ['fortunes' => $fortunes]);
+    }
+
+    /**
+     * Test #5: Database Updates
+     */
+    public function actionUpdates($queries)
+    {
+        $queries = Query::clamp($queries);
+
+        $selectCommand = Yii::$app->db->createCommand('SELECT randomNumber FROM World WHERE id = :id');
+        $updateCommand = Yii::$app->db->createCommand('UPDATE World SET randomNumber = :num WHERE id = :id');
+
+        $worlds = [];
+
+        while (0 < $queries--) {
+            $id = mt_rand(1, 10000);
+            $randomNumber = mt_rand(1, 1000);
+            $selectCommand->bindParam(':id', $id)->queryScalar();
+            $updateCommand->bindValues([':id' => $id, ':num' => $randomNumber])->execute();
+
+            $worlds[] = ['id' => $id, 'randomNumber' => $randomNumber];
+        }
+
+        return $this->asJson($worlds);
+    }
+}

+ 71 - 103
frameworks/PHP/yii2/app/controllers/SiteController.php

@@ -2,131 +2,99 @@
 
 namespace app\controllers;
 
+use app\helpers\Query;
+use app\models\Fortune;
+use app\models\World;
 use Yii;
-use yii\helpers\Html;
 use yii\web\Controller;
+use yii\web\Response;
 
 class SiteController extends Controller
 {
+    /**
+     * Test #1: JSON Serialization
+     */
+    public function actionJson()
+    {
+        return $this->asJson(['message' => 'Hello, World!']);
+    }
 
-    public function actionJson() {
-        return $this->asJson(array('message'=>'Hello, World!'));
+    /**
+     * Test #2: Single Database Query
+     */
+    public function actionDb()
+    {
+        return $this->asJson(World::findOne(random_int(1, 10000)));
     }
 
-    public function actionDb($queries = 1) {
-        // Set up for Test
-//        $cmd = Yii::$app->db->createCommand('insert into World (randomNumber) values (:v)');
-//        for($i = 1; $i <=10000 ; $i ++ ) {
-//            $cmd->bindValue(':v',mt_rand(1, 10000))->execute();
-//        }
-
-        $statement =  Yii::$app->db->createCommand('select id,randomNumber from World where id = :id');
-
-        if ($queries == 1) {
-            $arr = $statement->bindValue(':id',mt_rand(1, 10000))->queryOne();
-            $arr['id'] = (int) $arr['id'];
-            $arr['randomNumber'] = (int) $arr['randomNumber'];
-        } else {
-            if ($queries > 500) $queries = 500;
-            elseif ($queries <= 0 ) $queries = 1;
-            // Create an array with the response string.
-            $arr = array();
-            // For each query, store the result set values in the response array
-            while (0 < $queries--) {
-                // Store result in array.
-                $result = $statement->bindValue(':id',mt_rand(1, 10000))->queryOne();
-                $result['id'] = (int) $result['id'];
-                $result['randomNumber'] = (int) $result['randomNumber'];
-                $arr[] = $result;
-            }
+    /**
+     * Test #3: Multiple Database Queries
+     */
+    public function actionQueries($queries)
+    {
+        $queries = Query::clamp($queries);
+
+        $worlds = [];
+
+        while (0 < $queries--) {
+            $world = World::findOne(random_int(1, 10000));
+            $worlds[] = $world;
         }
 
-        return $this->asJson($arr);
+        return $this->asJson($worlds);
     }
 
-    private static function cmp($a, $b) {
+    /**
+     * Test #4: Fortunes
+     */
+    public function actionFortunes()
+    {
+        $fortunes = Fortune::find()->all();
 
-	return ($a["message"] < $b["message"]) ? -1 : 1;
+        $runtimeFortune = new Fortune();
+        $runtimeFortune->id = 0;
+        $runtimeFortune->message = 'Additional fortune added at request time.';
 
-    }
+        $fortunes[] = $runtimeFortune;
 
-    public function actionFortunes() {
-        // Test Data
-//        $arr = [
-//            11=>'<script>alert("This should not be displayed in a browser alert box");</script>',
-//            4=>'A bad random number generator: 1, 1, 1, 1, 1, 4.33e+67, 1, 1, 1',
-//            5=>'A computer program does what you tell it to do, not what you want it to do.',
-//            2=>'A computer scientist is someone who fixes things that aren\'t broken.',
-//            8=>'A list is only as strong as its weakest link. — Donald Knuth',
-//            //0=>'Additional fortune added at request time.',
-//            //0=>'Additional fortune added at request time.',
-//            3=>'After enough decimal places, nobody gives a damn.',
-//            7=>'Any program that runs right is obsolete.',
-//            10=>'Computers make very fast, very accurate mistakes.',
-//            6=>'Emacs is a nice operating system, but I prefer UNIX. — Tom Christaensen',
-//            9=>'Feature: A bug with seniority.',
-//            1=>'fortune: No such file or directory',
-//            12=>'フレームワークのベンチマーク'
-//        ];
-//        foreach($arr as $k=>$v) {
-//            Yii::$app->db->createCommand('insert into Fortune (id,message) values (:id,:message)',[':id'=>$k,':message'=>$v])->execute();
-//        }
-
-
-        $arr = Yii::$app->db->createCommand('select id, message from Fortune')->queryAll();
-        $arr[] = ['id'=>0,'message'=>'Additional fortune added at request time.'];
-
-	usort($arr, array($this, 'cmp'));
-
-        header("Content-Type: text/html; charset=utf-8");
-        echo <<<EOM
-            <!DOCTYPE html>
-            <html>
-            <head>
-            <title>Fortunes</title>
-            </head>
-            <body>
-            <table>
-            <tr>
-            <th>id</th>
-            <th>message</th>
-            </tr>
-EOM;
-        foreach ( $arr as $val ) {
-            echo '<tr>';
-            echo '<td>'.$val['id'].'</td>';
-            echo '<td>'.Html::encode($val['message']).'</td>';
-            echo '</tr>';
-        }
-        echo <<<EOM
-        </table>
-        </body>
-        </html>
-EOM;
+        usort($fortunes, function ($left, $right) {
+            return strcmp($left->message, $right->message);
+        });
 
+        $this->view->title = 'Fortunes';
+
+        return $this->render('fortunes', ['fortunes' => $fortunes]);
     }
 
-    public function actionUpdates($queries = 1) {
-        if ($queries > 500) $queries = 500;
-        elseif ($queries <= 0 ) $queries = 1;
-        $selectCommand = Yii::$app->db->createCommand('select randomNumber from World where id = :id');
-        $updateCommand = Yii::$app->db->createCommand('update World set randomNumber = :num where id = :id');
-        $arr = [];
+    /**
+     * Test #5: Database Updates
+     */
+    public function actionUpdates($queries)
+    {
+        $queries = Query::clamp($queries);
+
+        $worlds = [];
+
         while (0 < $queries--) {
-            // Store result in array.
-            $id = mt_rand(1,10000);
-            $randomNumber = mt_rand(1, 1000);
-            $selectCommand->bindParam(':id',$id)->queryScalar();
-            $updateCommand->bindValues([':id'=>$id,':num'=>$randomNumber])->execute();
-            $arr[] = array('id' => $id, 'randomNumber' => $randomNumber);
+            $world = World::findOne(random_int(1, 10000));
+            $world->randomNumber = random_int(1, 10000);
+            $world->save();
+
+            $worlds[] = $world;
         }
 
-        return $this->asJson($arr);
+        return $this->asJson($worlds);
     }
 
-    public function actionPlaintext() {
-        Yii::$app->response->format = \yii\web\Response::FORMAT_RAW;
-        header("Content-Type: text/plain");
-        echo 'Hello, World!';
+    /**
+     * Test #6: Plaintext
+     */
+    public function actionPlaintext()
+    {
+        Yii::$app->response->format = Response::FORMAT_RAW;
+        Yii::$app->response->getHeaders()->add('Content-Type', 'text/plain');
+        Yii::$app->response->content = 'Hello, World!';
+
+        return Yii::$app->response;
     }
 }

+ 17 - 0
frameworks/PHP/yii2/app/helpers/Query.php

@@ -0,0 +1,17 @@
+<?php
+
+namespace app\helpers;
+
+class Query
+{
+    public static function clamp($value): int
+    {
+        if (!is_numeric($value) || $value < 1) {
+            return 1;
+        } elseif ($value > 500) {
+            return 500;
+        } else {
+            return $value;
+        }
+    }
+}

+ 17 - 0
frameworks/PHP/yii2/app/models/Fortune.php

@@ -0,0 +1,17 @@
+<?php
+
+namespace app\models;
+
+use yii\db\ActiveRecord;
+
+/**
+ * @property $id
+ * @property $message
+ */
+class Fortune extends ActiveRecord
+{
+    public static function tableName()
+    {
+        return 'fortune';
+    }
+}

+ 16 - 0
frameworks/PHP/yii2/app/models/World.php

@@ -0,0 +1,16 @@
+<?php
+
+namespace app\models;
+
+use yii\db\ActiveRecord;
+
+/**
+ * @property $randomNumber
+ */
+class World extends ActiveRecord
+{
+    public static function tableName()
+    {
+        return 'world';
+    }
+}

+ 7 - 0
frameworks/PHP/yii2/app/views/layouts/main.php

@@ -0,0 +1,7 @@
+<?php
+/** @var $content */
+
+use yii\helpers\Html;
+
+?>
+<!DOCTYPE html><html><head><title><?= Html::encode($this->title) ?></title></head><body><?= $content ?></body></html>

+ 6 - 0
frameworks/PHP/yii2/app/views/raw/fortunes.php

@@ -0,0 +1,6 @@
+<?php
+/** @var $fortunes */
+
+use yii\helpers\Html;
+
+?><table><tr><th>id</th><th>message</th></tr><?php foreach ($fortunes as $fortune): ?><tr><td><?=$fortune['id']?></td><td><?=Html::encode($fortune['message'])?></td></tr><?php endforeach?></table>

+ 6 - 0
frameworks/PHP/yii2/app/views/site/fortunes.php

@@ -0,0 +1,6 @@
+<?php
+/** @var $fortunes */
+
+use yii\helpers\Html;
+
+?><table><tr><th>id</th><th>message</th></tr><?php foreach ($fortunes as $fortune): ?><tr><td><?=$fortune->id?></td><td><?=Html::encode($fortune->message)?></td></tr><?php endforeach?></table>

+ 22 - 2
frameworks/PHP/yii2/benchmark_config.json

@@ -2,12 +2,32 @@
   "framework": "yii2",
   "tests": [{
     "default": {
-      "plaintext_url": "/site/plaintext",
       "json_url": "/site/json",
       "db_url": "/site/db",
-      "query_url": "/site/db?queries=",
+      "query_url": "/site/queries?queries=",
       "fortune_url": "/site/fortunes",
       "update_url": "/site/updates?queries=",
+      "plaintext_url": "/site/plaintext",
+      "port": 8080,
+      "approach": "Realistic",
+      "classification": "Fullstack",
+      "database": "MySQL",
+      "framework": "yii2",
+      "language": "PHP",
+      "flavor": "PHP7",
+      "orm": "Full",
+      "platform": "None",
+      "webserver": "nginx",
+      "os": "Linux",
+      "database_os": "Linux",
+      "display_name": "yii2",
+      "notes": "",
+      "versus": "php"
+    },
+    "raw": {
+      "query_url": "/raw/queries?queries=",
+      "fortune_url": "/raw/fortunes",
+      "update_url": "/raw/updates?queries=",
       "port": 8080,
       "approach": "Realistic",
       "classification": "Fullstack",

+ 29 - 25
frameworks/PHP/yii2/composer.lock

@@ -1,23 +1,23 @@
 {
     "_readme": [
         "This file locks the dependencies of your project to a known state",
-        "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
+        "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
         "This file is @generated automatically"
     ],
     "content-hash": "9a54116619bf45fa0405305cf9abddbe",
     "packages": [
         {
             "name": "cebe/markdown",
-            "version": "1.1.2",
+            "version": "1.2.1",
             "source": {
                 "type": "git",
                 "url": "https://github.com/cebe/markdown.git",
-                "reference": "25b28bae8a6f185b5030673af77b32e1163d5c6e"
+                "reference": "9bac5e971dd391e2802dca5400bbeacbaea9eb86"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/cebe/markdown/zipball/25b28bae8a6f185b5030673af77b32e1163d5c6e",
-                "reference": "25b28bae8a6f185b5030673af77b32e1163d5c6e",
+                "url": "https://api.github.com/repos/cebe/markdown/zipball/9bac5e971dd391e2802dca5400bbeacbaea9eb86",
+                "reference": "9bac5e971dd391e2802dca5400bbeacbaea9eb86",
                 "shasum": ""
             },
             "require": {
@@ -35,7 +35,7 @@
             "type": "library",
             "extra": {
                 "branch-alias": {
-                    "dev-master": "1.1.x-dev"
+                    "dev-master": "1.2.x-dev"
                 }
             },
             "autoload": {
@@ -64,7 +64,7 @@
                 "markdown",
                 "markdown-extra"
             ],
-            "time": "2017-07-16T21:13:23+00:00"
+            "time": "2018-03-26T11:24:36+00:00"
         },
         {
             "name": "ezyang/htmlpurifier",
@@ -151,24 +151,24 @@
         },
         {
             "name": "yiisoft/yii2",
-            "version": "2.0.14.1",
+            "version": "2.0.17",
             "source": {
                 "type": "git",
                 "url": "https://github.com/yiisoft/yii2-framework.git",
-                "reference": "d99969394c66dc3606134af092b8ec561ea5d7c6"
+                "reference": "b56159228c3b4990e38cf41ddd69af27ab015bea"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/yiisoft/yii2-framework/zipball/d99969394c66dc3606134af092b8ec561ea5d7c6",
-                "reference": "d99969394c66dc3606134af092b8ec561ea5d7c6",
+                "url": "https://api.github.com/repos/yiisoft/yii2-framework/zipball/b56159228c3b4990e38cf41ddd69af27ab015bea",
+                "reference": "b56159228c3b4990e38cf41ddd69af27ab015bea",
                 "shasum": ""
             },
             "require": {
                 "bower-asset/inputmask": "~3.2.2 | ~3.3.5",
-                "bower-asset/jquery": "3.2.*@stable | 3.1.*@stable | 2.2.*@stable | 2.1.*@stable | 1.11.*@stable | 1.12.*@stable",
+                "bower-asset/jquery": "3.3.*@stable | 3.2.*@stable | 3.1.*@stable | 2.2.*@stable | 2.1.*@stable | 1.11.*@stable | 1.12.*@stable",
                 "bower-asset/punycode": "1.3.*",
                 "bower-asset/yii2-pjax": "~2.0.1",
-                "cebe/markdown": "~1.0.0 | ~1.1.0",
+                "cebe/markdown": "~1.0.0 | ~1.1.0 | ~1.2.0",
                 "ext-ctype": "*",
                 "ext-mbstring": "*",
                 "ezyang/htmlpurifier": "~4.6",
@@ -247,24 +247,24 @@
                 "framework",
                 "yii2"
             ],
-            "time": "2018-02-24T20:23:06+00:00"
+            "time": "2019-03-22T21:26:26+00:00"
         },
         {
             "name": "yiisoft/yii2-bootstrap",
-            "version": "2.0.8",
+            "version": "2.0.9",
             "source": {
                 "type": "git",
                 "url": "https://github.com/yiisoft/yii2-bootstrap.git",
-                "reference": "3f49c47924bb9fa5363c3fc7b073d954168cf438"
+                "reference": "4677951dda712dd99d5bf2a127eaee118dfea860"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/yiisoft/yii2-bootstrap/zipball/3f49c47924bb9fa5363c3fc7b073d954168cf438",
-                "reference": "3f49c47924bb9fa5363c3fc7b073d954168cf438",
+                "url": "https://api.github.com/repos/yiisoft/yii2-bootstrap/zipball/4677951dda712dd99d5bf2a127eaee118dfea860",
+                "reference": "4677951dda712dd99d5bf2a127eaee118dfea860",
                 "shasum": ""
             },
             "require": {
-                "bower-asset/bootstrap": "3.3.* | 3.2.* | 3.1.*",
+                "bower-asset/bootstrap": "3.4.* | 3.3.* | 3.2.* | 3.1.*",
                 "yiisoft/yii2": "~2.0.6"
             },
             "type": "yii2-extension",
@@ -307,20 +307,20 @@
                 "bootstrap",
                 "yii2"
             ],
-            "time": "2018-02-16T10:41:52+00:00"
+            "time": "2019-01-29T21:39:45+00:00"
         },
         {
             "name": "yiisoft/yii2-composer",
-            "version": "2.0.5",
+            "version": "2.0.7",
             "source": {
                 "type": "git",
                 "url": "https://github.com/yiisoft/yii2-composer.git",
-                "reference": "3f4923c2bde6caf3f5b88cc22fdd5770f52f8df2"
+                "reference": "1439e78be1218c492e6cde251ed87d3f128b9534"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/yiisoft/yii2-composer/zipball/3f4923c2bde6caf3f5b88cc22fdd5770f52f8df2",
-                "reference": "3f4923c2bde6caf3f5b88cc22fdd5770f52f8df2",
+                "url": "https://api.github.com/repos/yiisoft/yii2-composer/zipball/1439e78be1218c492e6cde251ed87d3f128b9534",
+                "reference": "1439e78be1218c492e6cde251ed87d3f128b9534",
                 "shasum": ""
             },
             "require": {
@@ -349,6 +349,10 @@
                 {
                     "name": "Qiang Xue",
                     "email": "[email protected]"
+                },
+                {
+                    "name": "Carsten Brandt",
+                    "email": "[email protected]"
                 }
             ],
             "description": "The composer plugin for Yii extension installer",
@@ -357,7 +361,7 @@
                 "extension installer",
                 "yii2"
             ],
-            "time": "2016-12-20T13:26:02+00:00"
+            "time": "2018-07-05T15:44:47+00:00"
         }
     ],
     "packages-dev": [],

+ 22 - 0
frameworks/PHP/yii2/yii2-raw.dockerfile

@@ -0,0 +1,22 @@
+FROM ubuntu:18.10
+
+ARG DEBIAN_FRONTEND=noninteractive
+
+RUN apt-get update -yqq && apt-get install -yqq software-properties-common > /dev/null
+RUN LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php
+RUN apt-get update -yqq > /dev/null && \
+    apt-get install -yqq nginx git unzip php7.3 php7.3-common php7.3-cli php7.3-fpm php7.3-mysql php7.3-mbstring  > /dev/null
+
+RUN apt-get install -yqq composer > /dev/null
+
+COPY deploy/conf/* /etc/php/7.3/fpm/
+
+ADD ./ /yii2
+WORKDIR /yii2
+
+RUN if [ $(nproc) = 2 ]; then sed -i "s|pm.max_children = 1024|pm.max_children = 512|g" /etc/php/7.3/fpm/php-fpm.conf ; fi;
+
+RUN composer install --optimize-autoloader --classmap-authoritative --no-dev --quiet
+
+CMD service php7.3-fpm start && \
+    nginx -c /yii2/deploy/nginx-fpm.conf -g "daemon off;"