From c8380c3f1611115b8985dff582bd88868c9037d6 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Fri, 12 Mar 2021 16:11:49 -0800 Subject: [PATCH 1/4] feat(functions): add sample for PHP 7.4 preloading --- functions/tips_preload/ClassToPreload.php | 28 ++++++++++ functions/tips_preload/composer.json | 10 ++++ functions/tips_preload/index.php | 33 ++++++++++++ functions/tips_preload/php.ini | 1 + functions/tips_preload/phpunit.xml.dist | 34 ++++++++++++ functions/tips_preload/preload.php | 13 +++++ functions/tips_preload/test/DeployTest.php | 52 +++++++++++++++++++ .../tips_preload/test/IntegrationTest.php | 52 +++++++++++++++++++ 8 files changed, 223 insertions(+) create mode 100644 functions/tips_preload/ClassToPreload.php create mode 100644 functions/tips_preload/composer.json create mode 100644 functions/tips_preload/index.php create mode 100644 functions/tips_preload/php.ini create mode 100644 functions/tips_preload/phpunit.xml.dist create mode 100644 functions/tips_preload/preload.php create mode 100644 functions/tips_preload/test/DeployTest.php create mode 100644 functions/tips_preload/test/IntegrationTest.php diff --git a/functions/tips_preload/ClassToPreload.php b/functions/tips_preload/ClassToPreload.php new file mode 100644 index 0000000000..3483dfd946 --- /dev/null +++ b/functions/tips_preload/ClassToPreload.php @@ -0,0 +1,28 @@ + + + + + + test + + + + + + + + . + + ./vendor + + + + diff --git a/functions/tips_preload/preload.php b/functions/tips_preload/preload.php new file mode 100644 index 0000000000..a6791cc10c --- /dev/null +++ b/functions/tips_preload/preload.php @@ -0,0 +1,13 @@ +client->post('', [ + // Uncomment and CURLOPT_VERBOSE debug content will be sent to stdout. + // 'debug' => true + ]); + + $output = trim((string) $resp->getBody()); + + $this->assertEquals('200', $resp->getStatusCode()); + $this->assertStringContainsString('Class is preloaded: true', $output); + } +} diff --git a/functions/tips_preload/test/IntegrationTest.php b/functions/tips_preload/test/IntegrationTest.php new file mode 100644 index 0000000000..c12b29440e --- /dev/null +++ b/functions/tips_preload/test/IntegrationTest.php @@ -0,0 +1,52 @@ +client->get('/'); + $output = trim((string) $resp->getBody()); + + $this->assertEquals('200', $resp->getStatusCode()); + $this->assertStringContainsString('Class is preloaded: true', $output); + } + + private static function doRun() + { + $phpBin = (new PhpExecutableFinder())->find(); + + // add opcache.preload ini + $phpBin .= ' -d opcache.preload=preload.php'; + + return self::$fn->run([], '8080', $phpBin); + } +} From 0d3d5073827bd2fbc7d2b4776101d07f3d4830c9 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Fri, 12 Mar 2021 17:01:40 -0800 Subject: [PATCH 2/4] add composer,json optimize-autoload --- functions/tips_preload/composer.json | 9 +++++++++ functions/tips_preload/php.ini | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/functions/tips_preload/composer.json b/functions/tips_preload/composer.json index 8a421224e7..2e71085f4c 100644 --- a/functions/tips_preload/composer.json +++ b/functions/tips_preload/composer.json @@ -2,9 +2,18 @@ "require": { "google/cloud-functions-framework": "^0.7.2" }, + "config": { + "optimize-autoloader": true + }, "autoload": { "psr-4": { "Google\\Cloud\\Samples\\Functions\\TipsPreload\\": "" } + }, + "scripts": { + "start": [ + "Composer\\Config::disableProcessTimeout", + "FUNCTION_TARGET=preloadDemo php -d opcache.preload=preload.php -S localhost:${PORT:-8080} vendor/bin/router.php" + ] } } diff --git a/functions/tips_preload/php.ini b/functions/tips_preload/php.ini index c5313cbd71..9b26560020 100644 --- a/functions/tips_preload/php.ini +++ b/functions/tips_preload/php.ini @@ -1 +1 @@ -opcache.preload=preload.php \ No newline at end of file +opcache.preload=preload.php From b5b9c632a38780aeb23b12d82432098f9aabcdf5 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Fri, 12 Mar 2021 17:13:40 -0800 Subject: [PATCH 3/4] add .gcloudignore to ensure test classes are not deployed --- functions/tips_preload/.gcloudignore | 15 +++++++++++++++ functions/tips_preload/test/DeployTest.php | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 functions/tips_preload/.gcloudignore diff --git a/functions/tips_preload/.gcloudignore b/functions/tips_preload/.gcloudignore new file mode 100644 index 0000000000..d65708d3b0 --- /dev/null +++ b/functions/tips_preload/.gcloudignore @@ -0,0 +1,15 @@ +# The .gcloudignore file excludes file from upload to Cloud Build. +# If this file is deleted, gcloud will default to .gitignore. +# +# https://cloud.google.com/cloud-build/docs/speeding-up-builds#gcloudignore +# https://cloud.google.com/sdk/gcloud/reference/topic/gcloudignore + +# Exclude our local tests +test/ + +# Exclude locally vendored dependencies. +vendor/ + +# Exclude git history and configuration. +.git/ +.gitignore diff --git a/functions/tips_preload/test/DeployTest.php b/functions/tips_preload/test/DeployTest.php index 62d77a1541..334233de1c 100644 --- a/functions/tips_preload/test/DeployTest.php +++ b/functions/tips_preload/test/DeployTest.php @@ -17,7 +17,7 @@ declare(strict_types=1); -namespace Google\Cloud\Samples\Functions\HelloworldGet\Test; +namespace Google\Cloud\Samples\Functions\TipsPreload\Test; use Google\Cloud\TestUtils\CloudFunctionDeploymentTrait; use PHPUnit\Framework\TestCase; From 38020137c096d3d69e99269758c901d5077b660b Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Fri, 12 Mar 2021 17:18:18 -0800 Subject: [PATCH 4/4] better varnames --- functions/tips_preload/preload.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/functions/tips_preload/preload.php b/functions/tips_preload/preload.php index a6791cc10c..d06039c4b4 100644 --- a/functions/tips_preload/preload.php +++ b/functions/tips_preload/preload.php @@ -8,6 +8,6 @@ $classesToPreload = require $composerClassmap; -foreach ($classesToPreload as $class) { - require_once $class; +foreach ($classesToPreload as $class => $file) { + require_once $file; }