Ubiquity optimization

Ubiquity is fast, but can be even faster by optimizing a few elements.

Note

The integrated test server (accessible by Ubiquity serve) uses its own configuration and launch files (in the .ubiquity folder of your project).
It should therefore not be used to assess the results of the changes made.

Test your pages using a software and hardware configuration similar to the one used in production.
Use a benchmark tool to assess your changes as they happen (Apache bench for example).

Cache

System

Choose and test among the different cache systems (ArrayCache, PhpFastCache, MemCached).
The cache system is defined in the configuration file:

app/config/config.php
     "cache" => [
             "directory" => "cache/",
             "system" => "Ubiquity\\cache\\system\\ArrayCache",
             "params" => []
     ]

default ArrayCache is often the most optimized solution.

Generation

Generate the router and ORM cache (Think that annotations are never used at runtime):

Ubiquity init-cache

Static contents

If your application has pages that are being generated by PHP but that actually rarely change, you can cache:

  • The query results (using DAO methods)
  • The route response (with @route annotation)

index file

Remove the line defining error reporting at runtime, and make sure that error display is disabled in php.ini.

index.php
error_reporting(\E_ALL);//To be removed

Config optimization

The configuration is accessible from the app/config/config.php file.

Keep only those items that are essential to your application.

key role Optimization
siteUrl Used by Ajax methods, and by Twig’s url and path functions To be removed if these functions are not used
database Used by Ubiquity ORM To be removed if the ORM is not used
sessionName If assigned, starts or retrieves the php session for each request To be removed if the session is useless
templateEngine If assigned, instanciates a new Engine object for each request To be removed if the views are not used
templateEngineOptions Options assigned to the template engine instance set the cache option to true if Twig is used
test To remove (deprecated)  
debug Enables or disables logs Set to false in production
logger Defines the logger instance To remove in production
di Defines the services to be injected Only the @exec key is read at runtime
cache Defines the cache path and base class of the cache, used by models, router, dependency injection  
mvcNS Defines the paths or namespaces used by Rest controllers, models and controllers  
isRest Defines the condition to detect if a path corresponds to a controller Rest To be removed if you do not explicitly use this condition in your code

Example of configuration without session, and without dependency injection:

app/config/config.php
1
2
3
4
5
6
7
8
<?php
return array(
             "templateEngine"=>'Ubiquity\\views\\engine\\Twig',
             "templateEngineOptions"=>array("cache"=>true),
             "debug"=>false,
             "cache"=>["directory"=>"cache/","system"=>"Ubiquity\\cache\\system\\ArrayCache","params"=>[]],
             "mvcNS"=>["models"=>"models","controllers"=>"controllers","rest"=>""]
);

Services optimization

The loaded services are accessibles from the app/config/services.php file.

As for the configuration file, keep only those items that are essential to your application.

Lines Role
\Ubiquity\cache\CacheManager::startProd($config) Starts the cache for ORM, database, router, dependency injection
UbiquityormDAO::start() To be used only with multiple databases
Router::start() To be used only if the routes are defined with annotations
Router::addRoute(“_default”, “controllers\IndexController”) Defines the default route (to remove in production)
\Ubiquity\assets\AssetsManager::start($config) Assigns the variable siteUrl to the ThemeManager, to be used only if the css and js functions of twig are used

Example of a Services file with a database and starting the router :

app/config/services.php
1
2
3
<?php
\Ubiquity\cache\CacheManager::startProd($config);
\Ubiquity\controllers\Router::start();

Autoloader optimization

In production, remove dependencies used only in development, and generate the optimized class map file:

composer install --no-dev --classmap-authoritative

If the dependencies used have already been removed and you only want to update the map file (after adding or removing a class):

composer dump-autoload -o  --classmap-authoritative

Note

The --no-dev parameter removes the ubiquity-dev dependency required by webtools. If you use webtools in production, add the phpmv/ubiquity-dev dependency:

composer require phpmv/ubiquity-dev

PHP optimization

Please note that other applications can use the modified values on the same server.

OP-Cache

OPcache improves PHP performance by storing precompiled script bytecode in shared memory, thereby removing the need for PHP to load and parse scripts on each request.

php.ini
[opcache]
; Determines if Zend OPCache is enabled
opcache.enable=1
php.ini
; The OPcache shared memory storage size.
opcache.memory_consumption=256

; The maximum number of keys (scripts) in the OPcache hash table.
; Only numbers between 200 and 1000000 are allowed.
opcache.max_accelerated_files=10000

; When disabled, you must reset the OPcache manually or restart the
; webserver for changes to the filesystem to take effect.
opcache.validate_timestamps=0

; Allow file existence override (file_exists, etc.) performance feature.
opcache.enable_file_override=1

; Enables or disables copying of PHP code (text segment) into HUGE PAGES.
; This should improve performance, but requires appropriate OS configuration.
opcache.huge_code_pages=1

If you use ubiquity-swoole web server:

php.ini
; Determines if Zend OPCache is enabled for the CLI version of PHP
opcache.enable_cli=1

To complete

Remember that the framework used does not do everything. You also need to optimize your own code.