An OPCache Preloader for CakePHP 4.x applications

Overview

CakePHP Preloader

Latest Version on Packagist Build Coverage Status License: MIT CakePHP Minimum PHP Version

An OPCache preloader for CakePHP.

Reference: https://www.php.net/manual/en/opcache.preloading.php

This package is meant to provide an easy way for CakePHP application developers to generate preload files. Goals:

  • Generate an OPCache preloader with a simple command.
  • Allow optionally loading additional resources such as CakePHP plugins, userland app, and composer packages.
  • Provide a simplistic API for writing a custom preloader.

For an alternative approach, checkout DarkGhostHunter/Preloader.

For an OPCache UI, checkout amnuts/opcache-gui.

Installation

You can install this plugin into your CakePHP application using composer.

The recommended way to install composer packages is:

composer require cnizzardini/cakephp-preloader

Next, load the plugin in your src/Application.php bootstrapCli method:

$this->addPlugin('CakePreloader');

Usage

The easiest way to use CakePreloader is via the console command. This command can easily be included as part of your applications build process.

/srv/app $ bin/cake preloader --help
Generate a preload file

Usage:
cake preloader [options]

Options:

--app           Add your applications src directory into the preloader
--help, -h      Display this help.
--name          The preload file path. (default: ROOT . DS . 'preload.php')
--packages      A comma separated list of packages (e.g. vendor-name/package-name) to add to the preloader
--plugins       A comma separated list of your plugins to load or `*` to load all plugins/*
--quiet, -q     Enable quiet output.
--verbose, -v   Enable verbose output.

Examples:

Default loads in CakePHP core files excluding TestSuite, Console, Command, and Shell namespaces. Preload file is written to ROOT . DS . 'preload.php':

bin/cake preloader

Include a list of composer packages:

bin/cake preloader --packages=cakephp/authentication,cakephp/chronos

Include your APP code:

bin/cake preloader --app

Include all your projects plugins:

bin/cake preloader --plugins=*

Include a list of your projects plugins:

bin/cake preloader --plugins=MyPlugin,MyOtherPlugin

Before Write Event

You can extend functionality by listening for the CakePreloader.beforeWrite event. This is dispatched just before your preloader file is written.

(\Cake\Event\EventManager::instance())->on('CakePreloader.beforeWrite', function(Event $event){
    /** @var Preloader $preloader */
    $preloader = $event->getSubject();
    $resources = $preloader->getPreloadResources();
    // modify resources or whatever...
    $preloader->setPreloadResources($resources);
});

For more on events, read the CakePHP Events System documentation.

Preloader Class

You can customize your OPCache Preloader using the same class used by the console command. Preloader uses CakePHP's FileSystem class under the hood.

use CakePreloader\Preloader;

$preloader = new Preloader();
$preloader->loadPath('/required/path/to/files', function (\SplFileInfo $file) {
    // optional call back method, return true to add the file to the preloader
    return true;
});

// default path is ROOT . DS . 'preload.php'
$preloader->write('/optional/path/to/preloader-file.php');

Performance:

Obviously, these types of benchmarks should be taken with a bit of a gain of salt. I benchmarked this using apache bench with this project here: https://github.com/mixerapi/demo which is a dockerized REST API (LEMP stack on alpine + php-fpm 7.4). DEBUG was set to false.

[php]
session.auto_start = Off
short_open_tag = Off
opcache.preload_user=root
opcache.preload=/srv/app/preload.php
opcache.interned_strings_buffer = 16
opcache.max_accelerated_files = 20000
opcache.memory_consumption = 256
opcache.enable_cli = 0
opcache.enable = 1
opcache.revalidate_freq = 360
opcache.fast_shutdown = 1
realpath_cache_size = 4096K
realpath_cache_ttl = 600

Note: opcache.preload_user=root and opcache.preload=/srv/app/preload.php were disabled for the no preload run.

Command:

ab -n 10000 -c 10 http://localhost:8080/public/actors.json

I ran each 3 times:

Type Run 1 Run 2 Run 3
No Preload 301.30 [#/sec] (mean) 335.12 [#/sec] (mean) 322.41 [#/sec] (mean)
CAKE only 447.92 [#/sec] (mean) 448.48 [#/sec] (mean) 446.53 [#/sec] (mean)
CAKE + APP 457.62 [#/sec] (mean) 455.40 [#/sec] (mean) 394.89 [#/sec] (mean)

Tests / Analysis

Test Suite:

composer test

Test Suite + Static Analysis:

composer check
You might also like...
This component may look complex, weird and full of hacks but it is a game changer for how we run PHP applications.
This component may look complex, weird and full of hacks but it is a game changer for how we run PHP applications.

PHP Runtimes In early 2021, Symfony created a "Runtime component". This component may look complex, weird and full of hacks but it is a game changer f

Framework for building extensible server-side progressive applications for modern PHP.
Framework for building extensible server-side progressive applications for modern PHP.

Chevere 🔔 Subscribe to the newsletter to don't miss any update regarding Chevere. Framework for building extensible server-side progressive applicati

🍃Termwind allows you to build unique and beautiful PHP command-line applications, using the Tailwind CSS API
🍃Termwind allows you to build unique and beautiful PHP command-line applications, using the Tailwind CSS API

Termwind allows you to build unique and beautiful PHP command-line applications, using the Tailwind CSS API. In short, it's like Tailwind CSS, but for the PHP command-line applications.

TrailLamp is a lightweight, easy-to-use Php MVC framework that can be used to build web applications and REST APIs.

TrailLamp Introduction TrailLamp is a lightweight, easy-to-use Php MVC framework that can be used to build web applications and REST APIs. Installatio

FlyCubePHP is an MVC Web Framework developed in PHP and repeating the ideology and principles of building WEB applications, embedded in Ruby on Rails.

FlyCubePHP FlyCubePHP is an MVC Web Framework developed in PHP and repeating the ideology and principles of building WEB applications, embedded in Rub

Simple and universal connection pool for ReactPHP applications.

szado/reactphp-connection-pool Async and flexible pool for any type of connections built on top of ReactPHP. Connection pooling allows you to easily m

Framework X – the simple and fast micro framework for building reactive web applications that run anywhere.

Framework X Framework X – the simple and fast micro framework for building reactive web applications that run anywhere. Quickstart Documentation Tests

Silex Skeleton - a fully-functional Silex application that you can use as the skeleton for your new applications

Silex Skeleton - a fully-functional Silex application that you can use as the skeleton for your new applications

Elgg is an open source rapid development framework for socially aware web applications.

Elgg Elgg is an open source rapid development framework for socially aware web applications. Features Well-documented core API that allows developers

Comments
  • Add Mailer Preview to application ignore paths

    Add Mailer Preview to application ignore paths

    Mailer Preview Classes are a feature of the CakePHP debug kit.

    It's extends from debug kit and that is in require-dev and not available on composer install --no-dev. And these classes are only use for developing mails.

    opened by Iandenh 6
  • Only load classes and function files

    Only load classes and function files

    • Resolves #9
    • Adds new --cli option
    • Only loads Classes and function files now
    • Adds docker test to ensure php-fpm/cakephp loads with opcache preload enabled.
    opened by cnizzardini 0
  • Preloader Failing on deprecated cakephp core classes

    Preloader Failing on deprecated cakephp core classes

    CakePHP version: 4.3 (but likely affects other versions)

    Preloader fails on deprecated classes:

    • src/Database/Exception.php
    • src/Database/Expression/Comparison.php
    • src/Http/ControllerFactory.php
    • src/Http/Exception/MissingControllerException.php
    • src/Routing/Exception/MissingControllerException.php
    php_1  | waiting for fpm...
    php_1  | <br />
    php_1  | <b>Warning</b>:  Cannot declare class Cake\Database\Exception, because the name is already in use in <b>/srv/app/vendor/cakephp/cakephp/src/Database/Exception.php</b> on line <b>16</b><br />
    php_1  | <br />
    php_1  | <b>Fatal error</b>:  Uncaught Error: Undefined constant &quot;ROOT&quot; in /srv/app/vendor/cakephp/cakephp/src/Core/functions.php:290
    php_1  | Stack trace:
    php_1  | #0 /srv/app/vendor/cakephp/cakephp/src/Database/Expression/Comparison.php(6): deprecationWarning('`Comparison` de...')
    php_1  | #1 /srv/app/preload.php(121): require_once('/srv/app/vendor...')
    php_1  | #2 {main}
    php_1  |   thrown in <b>/srv/app/vendor/cakephp/cakephp/src/Core/functions.php</b> on line <b>290</b><br />
    demo_php_1 exited with code 70
    
    bug 
    opened by cnizzardini 0
Releases(v0.1.5)
Owner
Chris Nizzardini
Warp drives, not bombs.
Chris Nizzardini
CakePHP: The Rapid Development Framework for PHP - Official Repository

CakePHP is a rapid development framework for PHP which uses commonly known design patterns like Associative Data Mapping, Front Controller, and MVC. O

CakePHP 8.6k Dec 31, 2022
Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs.

Slim Framework Slim is a PHP micro-framework that helps you quickly write simple yet powerful web applications and APIs. Installation It's recommended

Slim Framework 11.5k Jan 4, 2023
Asynchronous & Fault-tolerant PHP Framework for Distributed Applications.

Kraken PHP Framework ~ Release the Kraken! Note: This repository contains the core of the Kraken Framework. If you want to start developing new applic

Kraken 1.1k Dec 27, 2022
PPM is a process manager, supercharger and load balancer for modern PHP applications.

PPM - PHP Process Manager PHP-PM is a process manager, supercharger and load balancer for PHP applications. It's based on ReactPHP and works best with

PPM - PHP Process Manager 6.5k Dec 27, 2022
Asynchronous server-side framework for network applications implemented in PHP using libevent

phpDaemon https://github.com/kakserpom/phpdaemon Asynchronous framework in PHP. It has a huge number of features. Designed for highload. Each worker i

Vasily Zorin 1.5k Nov 30, 2022
A powerful yet easy-to-use PHP micro-framework designed to help you build dynamic and robust Web applications - fast!

A powerful yet easy-to-use PHP micro-framework designed to help you build dynamic and robust Web applications - fast! Condensed in a single ~65KB file

Bong Cosca 2.6k Dec 30, 2022
Mind is the PHP code framework designed for developers. It offers a variety of solutions for creating design patterns, applications and code frameworks.

Mind Mind is the PHP code framework designed for developers. It offers a variety of solutions for creating design patterns, applications and code fram

null 0 Dec 13, 2021
Woski is a fast and simple lightweight PHP Framework for building applications in the realm of the web.

Woski is a simple fast PHP framework for the Realm The Project Installation Clone the repository $ composer create-project clintonnzedimma/woski myApp

Clinton Nzedimma 19 Aug 15, 2022
The Runtime Component enables decoupling applications from global state.

Runtime Component Symfony Runtime enables decoupling applications from global state. This Component is experimental. Experimental features are not cov

Symfony 409 Jan 3, 2023
Biny is a tiny, high-performance PHP framework for web applications

Biny is high performance. Framework comes default with response time of less than 1ms. Stand-alone QPS easily up to 3000.

Tencent 1.7k Dec 9, 2022