Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: feat(functions): PHP 7.4 preloading #1304

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

bshaffer
Copy link
Contributor

@bshaffer bshaffer commented Mar 13, 2021

WIP for preloading in PHP 7.4 Cloud Functions

This demo suggests the users do the following:

  1. Add the following to composer.json

    {
        "config": {
            "optimize-autoloader": true
        },
    }
  2. Add a preload.php file containing the following:

    require_once __DIR__ . '/vendor/autoload.php';
    
    // By running "composer dump-autoload --optimize", the classmap file contains a
    // mapping of optimized class files.
    $composerClassmap = __DIR__ . '/vendor/composer/autoload_classmap.php';
    
    $classesToPreload = require $composerClassmap;
    
    foreach ($classesToPreload as $class => $file) {
        require_once $file;
    }
  3. Add a php.ini containing the following:

    opcache.preload=preload.php

And that's it! Preloading will work for any/all classes handled by Composer's autoloader

@bshaffer bshaffer added the do not merge Indicates a pull request not ready for merge, due to either quality or timing. label Mar 13, 2021
@bshaffer bshaffer requested a review from a team as a code owner March 13, 2021 00:25
@google-cla google-cla bot added the cla: yes This human has signed the Contributor License Agreement. label Mar 13, 2021
@product-auto-label product-auto-label bot added the api: cloudfunctions Issues related to the Cloud Functions API. label Mar 13, 2021
@snippet-bot
Copy link

snippet-bot bot commented Mar 13, 2021

Here is the summary of changes.

You are about to add 1 region tag.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

@product-auto-label product-auto-label bot added the samples Issues that are directly related to samples. label Mar 13, 2021
@bshaffer
Copy link
Contributor Author

bshaffer commented Mar 13, 2021

I am was receiving the following error when running the deployment:

PHP Fatal error:  Uncaught Error: Class 'PHPUnit\Framework\TestCase' not found in /workspace/test/IntegrationTest.php:28
Stack trace:
#0 /workspace/preload.php(12): require_once()
#1 {main}
  thrown in /workspace/test/IntegrationTest.php on line 28 

This is very strange, since test/IntegrationTest.php does not show up (locally) when I run the same composer commands. I tried ignoring test/ via .gcloudignore, and the error changed to Failed opening required '/workspace/test/IntegrationTest.php', which isn't any better.

To fix this, I deployed with a new function NAME. I believe the reason is the vendor directory is cached for multiple deployments with the same function name if composer.json has not changed. So this is important to note...

I believe the main fix here is to upgrade the server's version of composer. The newer versions will skip adding classes to the classmap if they do not conform to the autoloading rules inside composer.json, allowing test classes and other dev classes to be deployed to the server without throwing errors like this.

@bshaffer bshaffer requested review from ace-n and grayside March 13, 2021 01:17
@grayside
Copy link
Contributor

Do we have any data on the benefits? E.g., A/B performance?

@bshaffer
Copy link
Contributor Author

@grayside no benchmarks, no, but we should definitely do some sanity-checking here. In theory, it should provide significant improvements on all requests, as the entire classmap is loaded into memory before the first request.

@bshaffer bshaffer changed the title WIP: feat(functions): add sample for PHP 7.4 preloading RFC: feat(functions): add sample for PHP 7.4 preloading Jun 18, 2021
@bshaffer bshaffer added the type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design. label Jan 27, 2022
@bshaffer bshaffer changed the title RFC: feat(functions): add sample for PHP 7.4 preloading RFC: feat(functions): PHP 7.4 preloading Apr 26, 2022
@vishwarajanand
Copy link
Member

Closing since we are not actively tracking this work. Maybe we'll want to come back to this if optimize flag proves optimistic.

@bshaffer
Copy link
Contributor Author

This is frustrating, since a lot of work was done to add this sample which could help the performance of Cloud Functions for a lot of our users. Maybe @grayside could suggest someone that might want to pick this up and add it to our docs?

@bshaffer bshaffer reopened this Sep 25, 2023
@bshaffer bshaffer requested review from a team as code owners September 25, 2023 16:11
@rogerthatdev rogerthatdev self-assigned this Sep 28, 2023
@rogerthatdev rogerthatdev removed the request for review from ace-n September 28, 2023 22:38
Copy link

@rogerthatdev rogerthatdev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified that this is deployable via the following:

gcloud functions deploy php-http-function-w-preload --gen2 --runtime=php74 --region=us-central1 --source=. --entry-point=preloadDemo --trigger-http --allow-unauthenticated

@rogerthatdev rogerthatdev removed the do not merge Indicates a pull request not ready for merge, due to either quality or timing. label Sep 29, 2023
@rogerthatdev rogerthatdev removed their assignment Nov 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: cloudfunctions Issues related to the Cloud Functions API. cla: yes This human has signed the Contributor License Agreement. samples Issues that are directly related to samples. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants