Make WordPress Core

Changeset 61612


Ignore:
Timestamp:
02/11/2026 08:14:09 PM (9 months ago)
Author:
westonruter
Message:

Site Health: Add test and debug data for Opcode Cache.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/9260

Props rollybueno, westonruter, swissspidy, peterwilsoncc, szepeviktor, ozgursar, oglekler, johnbillion, ugyensupport, abcd95, shailu25, noruzzaman.
Fixes #63697.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/class-wp-debug-data.php

    r61606 r61612  
    472472                );
    473473
     474                // Opcode Cache.
     475                if ( function_exists( 'opcache_get_status' ) ) {
     476                        $opcache_status = @opcache_get_status( false ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- Warning emitted in failure case.
     477
     478                        if ( false === $opcache_status ) {
     479                                $fields['opcode_cache'] = array(
     480                                        'label' => __( 'Opcode cache' ),
     481                                        'value' => __( 'Disabled by configuration' ),
     482                                        'debug' => 'not available',
     483                                );
     484                        } else {
     485                                $fields['opcode_cache'] = array(
     486                                        'label' => __( 'Opcode cache' ),
     487                                        'value' => $opcache_status['opcache_enabled'] ? __( 'Enabled' ) : __( 'Disabled' ),
     488                                        'debug' => $opcache_status['opcache_enabled'],
     489                                );
     490
     491                                if ( true === $opcache_status['opcache_enabled'] ) {
     492                                        $fields['opcode_cache_memory_usage'] = array(
     493                                                'label' => __( 'Opcode cache memory usage' ),
     494                                                'value' => sprintf(
     495                                                        /* translators: 1: Used memory, 2: Total memory */
     496                                                        __( '%1$s of %2$s' ),
     497                                                        size_format( $opcache_status['memory_usage']['used_memory'] ),
     498                                                        size_format( $opcache_status['memory_usage']['free_memory'] + $opcache_status['memory_usage']['used_memory'] )
     499                                                ),
     500                                                'debug' => sprintf(
     501                                                        '%s of %s',
     502                                                        $opcache_status['memory_usage']['used_memory'],
     503                                                        $opcache_status['memory_usage']['free_memory'] + $opcache_status['memory_usage']['used_memory']
     504                                                ),
     505                                        );
     506
     507                                        if ( 0 !== $opcache_status['interned_strings_usage']['buffer_size'] ) {
     508                                                $fields['opcode_cache_interned_strings_usage'] = array(
     509                                                        'label' => __( 'Opcode cache interned strings usage' ),
     510                                                        'value' => sprintf(
     511                                                                /* translators: 1: Percentage used, 2: Total memory, 3: Free memory */
     512                                                                __( '%1$s%% of %2$s (%3$s free)' ),
     513                                                                number_format_i18n( ( $opcache_status['interned_strings_usage']['used_memory'] / $opcache_status['interned_strings_usage']['buffer_size'] ) * 100, 2 ),
     514                                                                size_format( $opcache_status['interned_strings_usage']['buffer_size'] ),
     515                                                                size_format( $opcache_status['interned_strings_usage']['free_memory'] )
     516                                                        ),
     517                                                        'debug' => sprintf(
     518                                                                '%s%% of %s (%s free)',
     519                                                                round( ( $opcache_status['interned_strings_usage']['used_memory'] / $opcache_status['interned_strings_usage']['buffer_size'] ) * 100, 2 ),
     520                                                                $opcache_status['interned_strings_usage']['buffer_size'],
     521                                                                $opcache_status['interned_strings_usage']['free_memory']
     522                                                        ),
     523                                                );
     524                                        }
     525
     526                                        $fields['opcode_cache_hit_rate'] = array(
     527                                                'label' => __( 'Opcode cache hit rate' ),
     528                                                'value' => sprintf(
     529                                                        /* translators: %s: Hit rate percentage */
     530                                                        __( '%s%%' ),
     531                                                        number_format_i18n( $opcache_status['opcache_statistics']['opcache_hit_rate'], 2 )
     532                                                ),
     533                                                'debug' => round( $opcache_status['opcache_statistics']['opcache_hit_rate'], 2 ),
     534                                        );
     535
     536                                        $fields['opcode_cache_full'] = array(
     537                                                'label' => __( 'Is the Opcode cache full?' ),
     538                                                'value' => $opcache_status['cache_full'] ? __( 'Yes' ) : __( 'No' ),
     539                                                'debug' => $opcache_status['cache_full'],
     540                                        );
     541                                }
     542                        }
     543                } else {
     544                        $fields['opcode_cache'] = array(
     545                                'label' => __( 'Opcode cache' ),
     546                                'value' => __( 'Disabled' ),
     547                                'debug' => 'not available',
     548                        );
     549                }
     550
    474551                // Pretty permalinks.
    475552                $pretty_permalinks_supported = got_url_rewrite();
  • trunk/src/wp-admin/includes/class-wp-site-health.php

    r61459 r61612  
    27512751                                __( 'Your site is hidden from search engines. Consider enabling indexing if this is a public site.', 'default' )
    27522752                        );
     2753                }
     2754
     2755                return $result;
     2756        }
     2757
     2758        /**
     2759         * Tests if opcode cache is enabled and available.
     2760         *
     2761         * @since 7.0.0
     2762         *
     2763         * @return array<string, string|array<string, string>> The test result.
     2764         */
     2765        public function get_test_opcode_cache(): array {
     2766                $opcode_cache_enabled = false;
     2767                if ( function_exists( 'opcache_get_status' ) ) {
     2768                        $status = @opcache_get_status( false ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- Warning emitted in failure case.
     2769                        if ( $status && true === $status['opcache_enabled'] ) {
     2770                                $opcode_cache_enabled = true;
     2771                        }
     2772                }
     2773
     2774                $result = array(
     2775                        'label'       => __( 'Opcode cache is enabled' ),
     2776                        'status'      => 'good',
     2777                        'badge'       => array(
     2778                                'label' => __( 'Performance' ),
     2779                                'color' => 'blue',
     2780                        ),
     2781                        'description' => sprintf(
     2782                                '<p>%s</p>',
     2783                                __( 'Opcode cache improves PHP performance by storing precompiled script bytecode in memory, reducing the need for PHP to load and parse scripts on each request.' )
     2784                        ),
     2785                        'actions'     => sprintf(
     2786                                '<p><a href="%s" target="_blank">%s<span class="screen-reader-text"> %s</span><span aria-hidden="true" class="dashicons dashicons-external"></span></a></p>',
     2787                                esc_url( 'https://www.php.net/manual/en/book.opcache.php' ),
     2788                                __( 'Learn more about OPcache.' ),
     2789                                /* translators: Hidden accessibility text. */
     2790                                __( '(opens in a new tab)' )
     2791                        ),
     2792                        'test'        => 'opcode_cache',
     2793                );
     2794
     2795                if ( ! $opcode_cache_enabled ) {
     2796                        $result['status']       = 'recommended';
     2797                        $result['label']        = __( 'Opcode cache is not enabled' );
     2798                        $result['description'] .= '<p>' . __( 'Enabling this cache can significantly improve the performance of your site.' ) . '</p>';
    27532799                }
    27542800
    … …  
    28482894                                        'test'  => 'search_engine_visibility',
    28492895                                ),
     2896                                'opcode_cache'                 => array(
     2897                                        'label' => __( 'Opcode cache' ),
     2898                                        'test'  => 'opcode_cache',
     2899                                ),
    28502900                        ),
    28512901                        'async'  => array(
    … …  
    34163466
    34173467                        // Generic caching proxies (Nginx, Varnish, etc.)
    3418                         'x-cache'           => $cache_hit_callback,
    3419                         'x-cache-status'    => $cache_hit_callback,
    3420                         'x-litespeed-cache' => $cache_hit_callback,
    3421                         'x-proxy-cache'     => $cache_hit_callback,
    3422                         'via'               => '',
     3468                        'x-cache'                => $cache_hit_callback,
     3469                        'x-cache-status'         => $cache_hit_callback,
     3470                        'x-litespeed-cache'      => $cache_hit_callback,
     3471                        'x-proxy-cache'          => $cache_hit_callback,
     3472                        'via'                    => '',
    34233473
    34243474                        // Cloudflare
    3425                         'cf-cache-status' => $cache_hit_callback,
     3475                        'cf-cache-status'        => $cache_hit_callback,
    34263476                );
    34273477
  • trunk/tests/phpunit/tests/admin/wpSiteHealth.php

    r61562 r61612  
    573573                add_option( 'test_set_autoloaded_option', $heavy_option_string, '', true );
    574574        }
     575
     576        /**
     577         * Tests get_test_opcode_cache() return structure.
     578         *
     579         * @ticket 63697
     580         *
     581         * @covers ::get_test_opcode_cache()
     582         */
     583        public function test_get_test_opcode_cache_return_structure() {
     584                $result = $this->instance->get_test_opcode_cache();
     585
     586                $this->assertIsArray( $result );
     587                $this->assertArrayHasKey( 'label', $result );
     588                $this->assertArrayHasKey( 'status', $result );
     589                $this->assertArrayHasKey( 'badge', $result );
     590                $this->assertArrayHasKey( 'description', $result );
     591                $this->assertArrayHasKey( 'actions', $result );
     592                $this->assertArrayHasKey( 'test', $result );
     593
     594                $this->assertSame( 'opcode_cache', $result['test'] );
     595                $this->assertSame(
     596                        array(
     597                                'label' => __( 'Performance' ),
     598                                'color' => 'blue',
     599                        ),
     600                        $result['badge']
     601                );
     602                $this->assertContains( $result['status'], array( 'good', 'recommended' ), 'Status must be good or recommended.' );
     603        }
     604
     605        /**
     606         * Tests get_test_opcode_cache() result when opcode cache is enabled or not.
     607         *
     608         * Covers: opcache enabled, disabled, not available, and opcache_get_status() returns false.
     609         *
     610         * @ticket 63697
     611         *
     612         * @covers ::get_test_opcode_cache()
     613         */
     614        public function test_get_test_opcode_cache_result_by_environment() {
     615                $result = $this->instance->get_test_opcode_cache();
     616
     617                $opcache_enabled = false;
     618                if ( function_exists( 'opcache_get_status' ) ) {
     619                        $status = @opcache_get_status( false ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- Warning emitted in failure case.
     620                        if ( $status && true === $status['opcache_enabled'] ) {
     621                                $opcache_enabled = true;
     622                        }
     623                }
     624
     625                if ( $opcache_enabled ) {
     626                        $this->assertSame( 'good', $result['status'], 'When opcache is enabled, status should be "good".' );
     627                        $this->assertSame( __( 'Opcode cache is enabled' ), $result['label'] );
     628                } else {
     629                        $this->assertSame( 'recommended', $result['status'] );
     630                        $this->assertSame( __( 'Opcode cache is not enabled' ), $result['label'] );
     631                        $this->assertStringContainsString( __( 'Enabling this cache can significantly improve the performance of your site.' ), $result['description'] );
     632                }
     633        }
    575634}
Note: See TracChangeset for help on using the changeset viewer.