Browse Source

Yii2: enable the ORM cache on the DB schema (#5541)

* yii2: replace PHP 7.3 references by 7.4

* yii2 fortunes: sorting with anonymous function was slower

My micro-bencharks showed that using an anonymous function
as a callable parameter was about twice slower than using
a named function or a static method.

The gain is minor but visible, about 0.5 µs/call,
so theorically about 300 µs/request.

* yii2 updates: skip model validation

No visible gain, but it makes sense to skip the call to
Fortune::validate() when the data is not an user input.

* yii2: cache the DB schema

Without this, Yii2 analyzes the DB schema at the beginning of
each request. This caused warnings in `tfb` about the surpringly
large number of SQL queries.

The gain is huge. For instance, in yii2-fortunes it was more than ×7,
from 387 req/s to 2824 req/s on my system.

* yii2: do not install Bootstrap CSS components

Minimal performance gain (less classes for the autoloader),
so this change is mostly a sensible cleanup.

Co-authored-by: François Gannaz <[email protected]>
François Gannaz 5 years ago
parent
commit
c01cf64006

+ 2 - 4
frameworks/PHP/yii2/app/controllers/SiteController.php

@@ -57,9 +57,7 @@ class SiteController extends Controller
 
 
         $fortunes[] = $runtimeFortune;
         $fortunes[] = $runtimeFortune;
 
 
-        usort($fortunes, function ($left, $right) {
-            return strcmp($left->message, $right->message);
-        });
+        usort($fortunes, [Fortune::class, 'cmp']);
 
 
         $this->view->title = 'Fortunes';
         $this->view->title = 'Fortunes';
 
 
@@ -78,7 +76,7 @@ class SiteController extends Controller
         while ($queries--) {
         while ($queries--) {
             $world = World::findOne(random_int(1, 10000));
             $world = World::findOne(random_int(1, 10000));
             $world->randomNumber = random_int(1, 10000);
             $world->randomNumber = random_int(1, 10000);
-            $world->save();
+            $world->save(false);
 
 
             $worlds[] = $world;
             $worlds[] = $world;
         }
         }

+ 10 - 1
frameworks/PHP/yii2/app/index.php

@@ -20,7 +20,16 @@ $config = [
             'charset' => 'utf8',
             'charset' => 'utf8',
             'attributes' => [
             'attributes' => [
                 PDO::ATTR_PERSISTENT => true,
                 PDO::ATTR_PERSISTENT => true,
-            ]
+            ],
+            'enableLogging' => false,
+            'enableProfiling' => false,
+            'enableSchemaCache' => true,
+            'schemaCache' => 'cache',
+            'schemaCacheDuration' => 3600,
+        ],
+        'cache' => [
+            'class' => 'yii\caching\FileCache',
+            'cachePath' => '/tmp/yii2-cache',
         ],
         ],
         'urlManager' => [
         'urlManager' => [
             'enablePrettyUrl' => true,
             'enablePrettyUrl' => true,

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

@@ -14,4 +14,9 @@ class Fortune extends ActiveRecord
     {
     {
         return 'fortune';
         return 'fortune';
     }
     }
+
+    public static function cmp(Fortune $left, Fortune $right): int
+    {
+        return \strcmp($left->message, $right->message);
+    }
 }
 }

+ 2 - 3
frameworks/PHP/yii2/composer.json

@@ -1,7 +1,6 @@
 {
 {
 	"require": {
 	"require": {
 		"yidas/yii2-composer-bower-skip": "~2.0.13",
 		"yidas/yii2-composer-bower-skip": "~2.0.13",
-		"yiisoft/yii2": "~2.0.14",
-		"yiisoft/yii2-bootstrap": "~2.0.8"
+		"yiisoft/yii2": "~2.0.14"
 	}
 	}
-}
+}

+ 0 - 60
frameworks/PHP/yii2/composer.lock

@@ -249,66 +249,6 @@
             ],
             ],
             "time": "2019-03-22T21:26:26+00:00"
             "time": "2019-03-22T21:26:26+00:00"
         },
         },
-        {
-            "name": "yiisoft/yii2-bootstrap",
-            "version": "2.0.9",
-            "source": {
-                "type": "git",
-                "url": "https://github.com/yiisoft/yii2-bootstrap.git",
-                "reference": "4677951dda712dd99d5bf2a127eaee118dfea860"
-            },
-            "dist": {
-                "type": "zip",
-                "url": "https://api.github.com/repos/yiisoft/yii2-bootstrap/zipball/4677951dda712dd99d5bf2a127eaee118dfea860",
-                "reference": "4677951dda712dd99d5bf2a127eaee118dfea860",
-                "shasum": ""
-            },
-            "require": {
-                "bower-asset/bootstrap": "3.4.* | 3.3.* | 3.2.* | 3.1.*",
-                "yiisoft/yii2": "~2.0.6"
-            },
-            "type": "yii2-extension",
-            "extra": {
-                "branch-alias": {
-                    "dev-master": "2.0.x-dev"
-                }
-            },
-            "autoload": {
-                "psr-4": {
-                    "yii\\bootstrap\\": "src"
-                }
-            },
-            "notification-url": "https://packagist.org/downloads/",
-            "license": [
-                "BSD-3-Clause"
-            ],
-            "authors": [
-                {
-                    "name": "Paul Klimov",
-                    "email": "[email protected]"
-                },
-                {
-                    "name": "Alexander Makarov",
-                    "email": "[email protected]",
-                    "homepage": "http://rmcreative.ru/"
-                },
-                {
-                    "name": "Antonio Ramirez",
-                    "email": "[email protected]"
-                },
-                {
-                    "name": "Qiang Xue",
-                    "email": "[email protected]",
-                    "homepage": "http://www.yiiframework.com/"
-                }
-            ],
-            "description": "The Twitter Bootstrap extension for the Yii framework",
-            "keywords": [
-                "bootstrap",
-                "yii2"
-            ],
-            "time": "2019-01-29T21:39:45+00:00"
-        },
         {
         {
             "name": "yiisoft/yii2-composer",
             "name": "yiisoft/yii2-composer",
             "version": "2.0.7",
             "version": "2.0.7",

+ 6 - 6
frameworks/PHP/yii2/deploy/conf/php-fpm.conf

@@ -14,14 +14,14 @@
 ; Pid file
 ; Pid file
 ; Note: the default prefix is /var
 ; Note: the default prefix is /var
 ; Default Value: none
 ; Default Value: none
-pid = /run/php/php7.3-fpm.pid
+pid = /run/php/php7.4-fpm.pid
 
 
 ; Error log file
 ; Error log file
 ; If it's set to "syslog", log is sent to syslogd instead of being written
 ; If it's set to "syslog", log is sent to syslogd instead of being written
 ; into a local file.
 ; into a local file.
 ; Note: the default prefix is /var
 ; Note: the default prefix is /var
 ; Default Value: log/php-fpm.log
 ; Default Value: log/php-fpm.log
-;error_log = /var/log/php7.3-fpm.log
+;error_log = /var/log/php7.4-fpm.log
 error_log = /dev/stderr
 error_log = /dev/stderr
 
 
 
 
@@ -124,7 +124,7 @@ systemd_interval = 0
 ; Relative path can also be used. They will be prefixed by:
 ; Relative path can also be used. They will be prefixed by:
 ;  - the global prefix if it's been set (-p argument)
 ;  - the global prefix if it's been set (-p argument)
 ;  - /usr otherwise
 ;  - /usr otherwise
-;include=/etc/php/7.3/fpm/pool.d/*.conf
+;include=/etc/php/7.4/fpm/pool.d/*.conf
 
 
 ; Start a new pool named 'www'.
 ; Start a new pool named 'www'.
 ; the variable $pool can be used in any directive and will be replaced by the
 ; the variable $pool can be used in any directive and will be replaced by the
@@ -161,7 +161,7 @@ group = www-data
 ;                            (IPv6 and IPv4-mapped) on a specific port;
 ;                            (IPv6 and IPv4-mapped) on a specific port;
 ;   '/path/to/unix/socket' - to listen on a unix socket.
 ;   '/path/to/unix/socket' - to listen on a unix socket.
 ; Note: This value is mandatory.
 ; Note: This value is mandatory.
-listen = /run/php/php7.3-fpm.sock
+listen = /run/php/php7.4-fpm.sock
 
 
 ; Set listen(2) backlog.
 ; Set listen(2) backlog.
 ; Default Value: 511 (-1 on FreeBSD and OpenBSD)
 ; Default Value: 511 (-1 on FreeBSD and OpenBSD)
@@ -357,7 +357,7 @@ pm.max_spare_servers = 512
 ;   last request memory:  0
 ;   last request memory:  0
 ;
 ;
 ; Note: There is a real-time FPM status monitoring sample web page available
 ; Note: There is a real-time FPM status monitoring sample web page available
-;       It's available in: /usr/share/php/7.3/fpm/status.html
+;       It's available in: /usr/share/php/7.4/fpm/status.html
 ;
 ;
 ; Note: The value must start with a leading slash (/). The value can be
 ; Note: The value must start with a leading slash (/). The value can be
 ;       anything, but it may not be a good idea to use the .php extension or it
 ;       anything, but it may not be a good idea to use the .php extension or it
@@ -548,4 +548,4 @@ pm.max_spare_servers = 512
 ;php_flag[display_errors] = off
 ;php_flag[display_errors] = off
 ;php_admin_value[error_log] = /var/log/fpm-php.www.log
 ;php_admin_value[error_log] = /var/log/fpm-php.www.log
 ;php_admin_flag[log_errors] = on
 ;php_admin_flag[log_errors] = on
-;php_admin_value[memory_limit] = 32M
+;php_admin_value[memory_limit] = 32M

+ 2 - 2
frameworks/PHP/yii2/deploy/nginx-fpm.conf

@@ -18,7 +18,7 @@ http {
     keepalive_requests 10000;
     keepalive_requests 10000;
 
 
     upstream fastcgi_backend {
     upstream fastcgi_backend {
-        server unix:/var/run/php/php7.3-fpm.sock;
+        server unix:/var/run/php/php7.4-fpm.sock;
         keepalive 40;
         keepalive 40;
     }
     }
 
 
@@ -37,4 +37,4 @@ http {
             include        /etc/nginx/fastcgi_params;
             include        /etc/nginx/fastcgi_params;
         }
         }
     }
     }
-}
+}