Make WordPress Core


Ignore:
Timestamp:
06/04/2024 02:07:13 PM (11 months ago)
Author:
joemcgill
Message:

Site Health: Add test for large autoloaded options.

This adds a new Site Health check that will alert site owners if they are autoloading a large amount of data from the options table, as it could result in poor performance. The issue will be shown if the size of autoloaded options is greater than 800 KB, which can be adjusted using the new site_status_autoloaded_options_size_limit filter.

Props mukesh27, joemcgill, rajinsharwar, costdev, audrasjb, krupajnanda, pooja1210, Ankit K Gupta, johnbillion, oglekler.
Fixes #61276.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/admin/wpSiteHealth.php

    r56971 r58332  
    500500        );
    501501    }
     502
     503    /**
     504     * Tests get_test_autoloaded_options() when autoloaded options less than warning size.
     505     *
     506     * @ticket 61276
     507     *
     508     * @covers ::get_test_autoloaded_options()
     509     */
     510    public function test_wp_autoloaded_options_test_no_warning() {
     511        $expected_label  = esc_html__( 'Autoloaded options are acceptable' );
     512        $expected_status = 'good';
     513
     514        $result = $this->instance->get_test_autoloaded_options();
     515        $this->assertSame( $expected_label, $result['label'], 'The label should indicate that autoloaded options are acceptable.' );
     516        $this->assertSame( $expected_status, $result['status'], 'The status should be "good" when autoloaded options are acceptable.' );
     517    }
     518
     519    /**
     520     * Tests get_test_autoloaded_options() when autoloaded options more than warning size.
     521     *
     522     * @ticket 61276
     523     *
     524     * @covers ::get_test_autoloaded_options()
     525     */
     526    public function test_wp_autoloaded_options_test_warning() {
     527        self::set_autoloaded_option( 800000 );
     528
     529        $expected_label  = esc_html__( 'Autoloaded options could affect performance' );
     530        $expected_status = 'critical';
     531
     532        $result = $this->instance->get_test_autoloaded_options();
     533        $this->assertSame( $expected_label, $result['label'], 'The label should indicate that autoloaded options could affect performance.' );
     534        $this->assertSame( $expected_status, $result['status'], 'The status should be "critical" when autoloaded options could affect performance.' );
     535    }
     536
     537    /**
     538     * Tests get_autoloaded_options_size().
     539     *
     540     * @ticket 61276
     541     *
     542     * @covers ::get_autoloaded_options_size()
     543     */
     544    public function test_get_autoloaded_options_size() {
     545        global $wpdb;
     546
     547        $autoload_values = wp_autoload_values_to_autoload();
     548
     549        $autoloaded_options_size = (int) $wpdb->get_var(
     550            $wpdb->prepare(
     551                sprintf(
     552                    "SELECT SUM(LENGTH(option_value)) FROM $wpdb->options WHERE autoload IN (%s)",
     553                    implode( ',', array_fill( 0, count( $autoload_values ), '%s' ) )
     554                ),
     555                $autoload_values
     556            )
     557        );
     558        $this->assertSame( $autoloaded_options_size, $this->instance->get_autoloaded_options_size(), 'The size of autoloaded options should match the calculated size from the database.' );
     559
     560        // Add autoload option.
     561        $test_option_string       = 'test';
     562        $test_option_string_bytes = mb_strlen( $test_option_string, '8bit' );
     563        self::set_autoloaded_option( $test_option_string_bytes );
     564        $this->assertSame( $autoloaded_options_size + $test_option_string_bytes, $this->instance->get_autoloaded_options_size(), 'The size of autoloaded options should increase by the size of the newly added option.' );
     565    }
     566
     567    /**
     568     * Sets a test autoloaded option.
     569     *
     570     * @param int $bytes bytes to load in options.
     571     */
     572    public static function set_autoloaded_option( $bytes = 800000 ) {
     573        $heavy_option_string = wp_generate_password( $bytes );
     574
     575        // Force autoloading so that WordPress core does not override it. See https://core.trac.wordpress.org/changeset/57920.
     576        add_option( 'test_set_autoloaded_option', $heavy_option_string, '', true );
     577    }
    502578}
Note: See TracChangeset for help on using the changeset viewer.