From 3b80b7c48c0c06b4a581cd64026aa508de3f0df2 Mon Sep 17 00:00:00 2001 From: SmetDenis Date: Sat, 13 Apr 2024 03:11:54 +0400 Subject: [PATCH 1/4] Add Dockerfile for PHP ZTS The commit introduces a new Dockerfile, facilitating the usage of PHP with Zend Thread Safety (ZTS) for an enhanced multi-threading support. It also alters the Makefile to integrate a Docker build command for more efficient imagery creation. --- .github/workflows/main.yml | 57 +++++++++++++++++++++++++++- Dockerfile | 18 +++------ Dockerfile.zts | 55 +++++++++++++++++++++++++++ Makefile | 4 ++ docker/build-preloader.php | 3 +- docker/php-zts.ini | 62 +++++++++++++++++++++++++++++++ docker/php.ini | 10 ++--- src/Commands/AbstractValidate.php | 14 ++++--- 8 files changed, 196 insertions(+), 27 deletions(-) create mode 100644 Dockerfile.zts create mode 100644 docker/php-zts.ini diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index e69195da..9afadc96 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -245,6 +245,58 @@ jobs: run: | ! $BLUEPRINT_DOCKER $CMD_VALIDATE $INVALID_TEST + - name: Prepare Benchmark + run: | + make build --no-print-directory + make bench-create-csv --no-print-directory + + - name: 🔥 Benchmark + run: | + ! make bench-docker-quick --no-print-directory + + - name: Push Docker Image (master) + uses: docker/build-push-action@v5 + if: github.ref == 'refs/heads/master' + with: + context: . + push: true + tags: jbzoo/csv-blueprint:master + + + verify-docker-zts: + name: Verify Docker ZTS + runs-on: ubuntu-latest + env: + BENCH_COLS: 1 + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + fetch-depth: 0 + ref: ${{ github.event.pull_request.head.ref }} + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: 8.3 + + - name: Login to Docker Hub + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + + - name: 🐳 Building Docker Image + uses: docker/build-push-action@v5 + with: + context: . + file: Dockerfile.zts + push: false + tags: jbzoo/csv-blueprint:local + + - name: 🎨 Test help and logo + run: $BLUEPRINT_DOCKER --ansi -vvv + - name: 👍 Valid CSV files (Parallel) run: $BLUEPRINT_DOCKER $CMD_VALIDATE $VALID_TEST --parallel @@ -265,13 +317,14 @@ jobs: run: | ! make bench-docker-quick-parallel --no-print-directory - - name: Push Docker Image (master) + - name: Push Docker Image (master-zts) uses: docker/build-push-action@v5 if: github.ref == 'refs/heads/master' with: context: . + file: Dockerfile.zts push: true - tags: jbzoo/csv-blueprint:master + tags: jbzoo/csv-blueprint:zts verify-ga: diff --git a/Dockerfile b/Dockerfile index b4aed754..75c9712b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -17,11 +17,11 @@ COPY . /tmp RUN make build-version ######################################################################################## -FROM php:8.3-zts-alpine +FROM php:8.3-cli-alpine # Install PHP extensions ADD --chmod=0755 https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/ -RUN install-php-extensions opcache parallel @composer +RUN install-php-extensions opcache @composer # Install application WORKDIR /app @@ -38,21 +38,13 @@ RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini" COPY ./docker/php.ini /usr/local/etc/php/conf.d/docker-z99-php.ini # Warmup caches -RUN php ./docker/random-csv.php \ - && JBZOO_BUILD_PRELOADER=1 \ - ./csv-blueprint validate:csv \ - --schema=/app/schema-examples/full.yml \ - --csv=/app/docker/random_data.csv \ - --apply-all=yes \ - --report=text --mute-errors | grep issues \ - && rm ./docker/random_data.csv \ - && php ./docker/build-preloader.php \ +RUN OPCACHE_COMPILER=1 php ./docker/build-preloader.php \ && php ./docker/preload.php \ && du -sh /app/docker # && echo "opcache.preload=/app/docker/preload.php" >> /usr/local/etc/php/conf.d/docker-z99-php.ini + # Quick test RUN time ./csv-blueprint -V -ENTRYPOINT ["/app/docker/entrypoint.sh"] -#ENTRYPOINT ["/app/csv-blueprint"] +ENTRYPOINT ["/app/csv-blueprint"] diff --git a/Dockerfile.zts b/Dockerfile.zts new file mode 100644 index 00000000..a5d63612 --- /dev/null +++ b/Dockerfile.zts @@ -0,0 +1,55 @@ +# +# JBZoo Toolbox - Csv-Blueprint. +# +# This file is part of the JBZoo Toolbox project. +# For the full copyright and license information, please view the LICENSE +# file that was distributed with this source code. +# +# @license MIT +# @copyright Copyright (C) JBZoo.com, All rights reserved. +# @see https://github.com/JBZoo/Csv-Blueprint +# + +FROM alpine:3.9 AS preparatory +RUN apk add --no-cache make git +WORKDIR /tmp +COPY . /tmp +RUN make build-version + +######################################################################################## +FROM php:8.3-zts-alpine + +# Install PHP extensions +ADD --chmod=0755 https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/ +RUN install-php-extensions opcache parallel @composer + +# Install application +WORKDIR /app +ENV COMPOSER_ALLOW_SUPERUSER=1 +COPY . /app +COPY --from=preparatory /tmp/.version /app/.version +RUN composer install --no-dev --classmap-authoritative --no-progress \ + && rm -rf ./.git \ + && composer clear-cache \ + && chmod +x ./csv-blueprint \ + && chmod +x ./docker/entrypoint.sh + +RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini" +COPY ./docker/php-zts.ini /usr/local/etc/php/conf.d/docker-z99-php.ini + +# Build custom autoloader for PHP ext-parallel +RUN php ./docker/random-csv.php \ + && JBZOO_BUILD_PRELOADER=1 \ + ./csv-blueprint validate:csv \ + --schema=/app/schema-examples/full.yml \ + --csv=/app/docker/random_data.csv \ + --apply-all=yes \ + --report=text --mute-errors | grep issues \ + && rm ./docker/random_data.csv \ + && php ./docker/build-preloader.php \ + && php ./docker/preload.php + +# Quick test +RUN time ./csv-blueprint -V + +ENTRYPOINT ["/app/docker/entrypoint.sh"] diff --git a/Makefile b/Makefile index 6a8a5525..697ed6a5 100644 --- a/Makefile +++ b/Makefile @@ -77,6 +77,10 @@ docker-build: ##@Docker (Re-)build Docker image $(call title,"Building Docker Image") @docker build -t $(DOCKER_IMAGE) --progress=plain . +docker-build-zts: ##@Docker (Re-)build Docker image with ZTS + $(call title,"Building Docker Image") + @docker build -t $(DOCKER_IMAGE)-zts -f Dockerfile.zts --progress=plain . + docker-demo: ##@Docker Run demo via Docker $(call title,"Demo - Valid CSV \(via Docker\)") @$(BLUEPRINT_DOCKER) $(VALID_CSV) $(VALID_SCHEMA) -vvv diff --git a/docker/build-preloader.php b/docker/build-preloader.php index 4d2bde89..61c41cc0 100644 --- a/docker/build-preloader.php +++ b/docker/build-preloader.php @@ -15,10 +15,11 @@ declare(strict_types=1); use JBZoo\CsvBlueprint\Tools\PreloadBuilder; +use JBZoo\Utils\Env; require_once __DIR__ . '/../vendor/autoload.php'; -(new PreloadBuilder(enableOpcacheCompiler: false)) +(new PreloadBuilder(Env::bool('OPCACHE_COMPILER'))) ->setExcludes([ \dirname(__DIR__) . '/csv-blueprint', \dirname(__DIR__) . '/csv-blueprint.php', diff --git a/docker/php-zts.ini b/docker/php-zts.ini new file mode 100644 index 00000000..6df2cbe2 --- /dev/null +++ b/docker/php-zts.ini @@ -0,0 +1,62 @@ +; +; JBZoo Toolbox - Csv-Blueprint. +; +; This file is part of the JBZoo Toolbox project. +; For the full copyright and license information, please view the LICENSE +; file that was distributed with this source code. +; +; @license MIT +; @copyright Copyright (C) JBZoo.com, All rights reserved. +; @see https://github.com/JBZoo/Csv-Blueprint +; +; Note: All ini files need to be saved as UTF-8 (no BOM) +; + +; Opcache +opcache.enable = 1 +opcache.enable_cli = 1 +opcache.interned_strings_buffer = 32 +opcache.max_accelerated_files = 10000 +opcache.fast_shutdown = 1 +opcache.save_comments = 1 +opcache.jit_buffer_size = 100M +opcache.jit = tracing + +opcache.memory_consumption = 200 + +; Experimantal for really quick script start. +; Save opcache data as files on disk inside the Docker image +; Doesn't work properly in multi-trhead mode. +;opcache.memory_consumption = 0 +;opcache.lockfile_path = /app/docker/opcache.lock +;opcache.file_cache = /app/docker/ +;opcache.file_cache_only = 1 + +; Enable aggressive opcache optimization +opcache.use_cwd = 0 +opcache.validate_root = 0 +opcache.revalidate_freq = 0 +opcache.validate_timestamps = 0 +opcache.validate_permission = 0 +opcache.enable_file_override = 0 +opcache.file_cache_consistency_checks = 0 +realpath_cache_size = 64M +realpath_cache_ttl = 100000 + +; Base config +max_execution_time = 3600 +memory_limit = 2G +date.timezone = UTC +precision = 14 + +; Security things +allow_url_fopen = 0 +allow_url_include = 0 + +; Error handling +error_reporting = E_ALL +display_errors = On +display_startup_errors = On + +; Experimental +;opcache.preload=/app/docker/preload.php diff --git a/docker/php.ini b/docker/php.ini index 6df2cbe2..965817e6 100644 --- a/docker/php.ini +++ b/docker/php.ini @@ -22,15 +22,13 @@ opcache.save_comments = 1 opcache.jit_buffer_size = 100M opcache.jit = tracing -opcache.memory_consumption = 200 - ; Experimantal for really quick script start. ; Save opcache data as files on disk inside the Docker image ; Doesn't work properly in multi-trhead mode. -;opcache.memory_consumption = 0 -;opcache.lockfile_path = /app/docker/opcache.lock -;opcache.file_cache = /app/docker/ -;opcache.file_cache_only = 1 +opcache.memory_consumption = 0 +opcache.lockfile_path = /app/docker/opcache.lock +opcache.file_cache = /app/docker/ +opcache.file_cache_only = 1 ; Enable aggressive opcache optimization opcache.use_cwd = 0 diff --git a/src/Commands/AbstractValidate.php b/src/Commands/AbstractValidate.php index d093cb09..a7aa3b59 100644 --- a/src/Commands/AbstractValidate.php +++ b/src/Commands/AbstractValidate.php @@ -103,11 +103,15 @@ protected function preparation(): void $threads = $this->getNumberOfThreads(); if ($threads !== 1) { - $this->_( - $threads > 0 - ? "Parallel mode: {$threads} threads" - : 'Parallel mode: ' . WorkerPool::getCpuCount() . ' threads (auto)', - ); + if (WorkerPool::extLoaded()) { + $this->_( + $threads > 0 + ? "Parallel mode: {$threads} threads" + : 'Parallel mode: ' . WorkerPool::getCpuCount() . ' threads (auto)', + ); + } else { + $this->_('Parallel mode: Not available. Please, see docs.'); + } } Utils::setDebugMode($this->getOptBool('debug')); From 4037d22135e14b0bcc03d8bf81140f1dc282e48e Mon Sep 17 00:00:00 2001 From: SmetDenis Date: Sat, 13 Apr 2024 03:17:50 +0400 Subject: [PATCH 2/4] Update opcache configurations in php.ini The configs have been updated for improved performance. The opcache.jit has been updated to '1255' and a commented line for opcache.memory_consumption has been added for potential future use. --- docker/php.ini | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/docker/php.ini b/docker/php.ini index 965817e6..38a7debb 100644 --- a/docker/php.ini +++ b/docker/php.ini @@ -20,7 +20,9 @@ opcache.max_accelerated_files = 10000 opcache.fast_shutdown = 1 opcache.save_comments = 1 opcache.jit_buffer_size = 100M -opcache.jit = tracing +opcache.jit = 1255 + +;opcache.memory_consumption = 200 ; Experimantal for really quick script start. ; Save opcache data as files on disk inside the Docker image @@ -30,6 +32,7 @@ opcache.lockfile_path = /app/docker/opcache.lock opcache.file_cache = /app/docker/ opcache.file_cache_only = 1 + ; Enable aggressive opcache optimization opcache.use_cwd = 0 opcache.validate_root = 0 From aee912e548d6a3d2f82c44d9320e54171297669e Mon Sep 17 00:00:00 2001 From: SmetDenis Date: Sat, 13 Apr 2024 03:19:37 +0400 Subject: [PATCH 3/4] Enable opcache memory consumption in php.ini The memory_consumption directive previously commented out has been enabled for better opcache performance. Additionally, other opcache directives related to file caching have been commented out at this time. --- docker/php.ini | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docker/php.ini b/docker/php.ini index 38a7debb..3d3e52f9 100644 --- a/docker/php.ini +++ b/docker/php.ini @@ -22,15 +22,15 @@ opcache.save_comments = 1 opcache.jit_buffer_size = 100M opcache.jit = 1255 -;opcache.memory_consumption = 200 +opcache.memory_consumption = 200 ; Experimantal for really quick script start. ; Save opcache data as files on disk inside the Docker image ; Doesn't work properly in multi-trhead mode. -opcache.memory_consumption = 0 -opcache.lockfile_path = /app/docker/opcache.lock -opcache.file_cache = /app/docker/ -opcache.file_cache_only = 1 +;opcache.memory_consumption = 0 +;opcache.lockfile_path = /app/docker/opcache.lock +;opcache.file_cache = /app/docker/ +;opcache.file_cache_only = 1 ; Enable aggressive opcache optimization From e50548eea126481955b0537a351338f652b70f39 Mon Sep 17 00:00:00 2001 From: SmetDenis Date: Sat, 13 Apr 2024 03:22:56 +0400 Subject: [PATCH 4/4] Remove docker/php-zts.ini file and update Dockerfile.zts The docker/php-zts.ini file was removed, which contained various PHP settings. The Dockerfile.zts file has been updated to replace the deleted php-zts.ini file with a php.ini file. These changes reflect a shift in PHP configuration strategy. --- Dockerfile.zts | 2 +- docker/php-zts.ini | 62 ---------------------------------------------- 2 files changed, 1 insertion(+), 63 deletions(-) delete mode 100644 docker/php-zts.ini diff --git a/Dockerfile.zts b/Dockerfile.zts index a5d63612..1f0474e8 100644 --- a/Dockerfile.zts +++ b/Dockerfile.zts @@ -35,7 +35,7 @@ RUN composer install --no-dev --classmap-authoritative --no-progress \ && chmod +x ./docker/entrypoint.sh RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini" -COPY ./docker/php-zts.ini /usr/local/etc/php/conf.d/docker-z99-php.ini +COPY ./docker/php.ini /usr/local/etc/php/conf.d/docker-z99-php.ini # Build custom autoloader for PHP ext-parallel RUN php ./docker/random-csv.php \ diff --git a/docker/php-zts.ini b/docker/php-zts.ini deleted file mode 100644 index 6df2cbe2..00000000 --- a/docker/php-zts.ini +++ /dev/null @@ -1,62 +0,0 @@ -; -; JBZoo Toolbox - Csv-Blueprint. -; -; This file is part of the JBZoo Toolbox project. -; For the full copyright and license information, please view the LICENSE -; file that was distributed with this source code. -; -; @license MIT -; @copyright Copyright (C) JBZoo.com, All rights reserved. -; @see https://github.com/JBZoo/Csv-Blueprint -; -; Note: All ini files need to be saved as UTF-8 (no BOM) -; - -; Opcache -opcache.enable = 1 -opcache.enable_cli = 1 -opcache.interned_strings_buffer = 32 -opcache.max_accelerated_files = 10000 -opcache.fast_shutdown = 1 -opcache.save_comments = 1 -opcache.jit_buffer_size = 100M -opcache.jit = tracing - -opcache.memory_consumption = 200 - -; Experimantal for really quick script start. -; Save opcache data as files on disk inside the Docker image -; Doesn't work properly in multi-trhead mode. -;opcache.memory_consumption = 0 -;opcache.lockfile_path = /app/docker/opcache.lock -;opcache.file_cache = /app/docker/ -;opcache.file_cache_only = 1 - -; Enable aggressive opcache optimization -opcache.use_cwd = 0 -opcache.validate_root = 0 -opcache.revalidate_freq = 0 -opcache.validate_timestamps = 0 -opcache.validate_permission = 0 -opcache.enable_file_override = 0 -opcache.file_cache_consistency_checks = 0 -realpath_cache_size = 64M -realpath_cache_ttl = 100000 - -; Base config -max_execution_time = 3600 -memory_limit = 2G -date.timezone = UTC -precision = 14 - -; Security things -allow_url_fopen = 0 -allow_url_include = 0 - -; Error handling -error_reporting = E_ALL -display_errors = On -display_startup_errors = On - -; Experimental -;opcache.preload=/app/docker/preload.php