Skip to:
Content

bbPress.org


Ignore:
Timestamp:
05/30/2016 10:28:36 AM (8 years ago)
Author:
netweb
Message:

General Performance: Introduce increase/decrease helper count functions

Previously when a new topic or reply was created, a bunch of queries to recalculate the topic and reply counts for topics and forums were ran. Now these have been replaced with more efficient increase/decrease helper functions to get the current value and just "bump" the count based on the action (new topic-reply/split-topic/move-topic/spam-trash-topic/etc...)

Props thebrandonallen, tharsheblows, netweb
See #1799

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/testcases/topics/functions/counts.php

    r5922 r6036  
    1414     */
    1515    public function test_bbp_topic_new_reply_counts() {
     16        remove_action( 'bbp_insert_reply', 'bbp_insert_reply_update_counts', 10 );
     17
    1618        $f = $this->factory->forum->create();
    1719        $t = $this->factory->topic->create( array(
     
    3234        $u = $this->factory->user->create();
    3335
    34         // Cheating here, but we need $_SERVER['SERVER_NAME'] to be set.
    35         $this->setUp_wp_mail( false );
     36        // Don't attempt to send an email. This is for speed and PHP errors.
     37        remove_action( 'bbp_new_reply', 'bbp_notify_topic_subscribers', 11, 5 );
    3638
    3739        // Simulate the 'bbp_new_reply' action.
    3840        do_action( 'bbp_new_reply', $r1, $t, $f, false, bbp_get_current_user_id() );
    3941
    40         // Reverse our changes.
    41         $this->tearDown_wp_mail( false );
    42 
    4342        $count = bbp_get_topic_reply_count( $t, true );
    4443        $this->assertSame( 1, $count );
     
    5857            ),
    5958        ) );
    60 
    61         // Cheating here, but we need $_SERVER['SERVER_NAME'] to be set.
    62         $this->setUp_wp_mail( false );
    6359
    6460        // Simulate the 'bbp_new_topic' action.
    6561        do_action( 'bbp_new_reply', $r2, $t, $f, false, $u );
    6662
    67         // Reverse our changes.
    68         $this->tearDown_wp_mail( false );
    69 
    70         $count = bbp_get_topic_reply_count( $t, true );
    71         $this->assertSame( 2, $count );
    72 
    73         $count = bbp_get_topic_reply_count_hidden( $t, true );
    74         $this->assertSame( 0, $count );
    75 
    76         $count = bbp_get_topic_voice_count( $t, true );
    77         $this->assertSame( 2, $count );
     63        $count = bbp_get_topic_reply_count( $t, true );
     64        $this->assertSame( 2, $count );
     65
     66        $count = bbp_get_topic_reply_count_hidden( $t, true );
     67        $this->assertSame( 0, $count );
     68
     69        $count = bbp_get_topic_voice_count( $t, true );
     70        $this->assertSame( 2, $count );
     71
     72        // Re-add removed actions.
     73        add_action( 'bbp_insert_reply', 'bbp_insert_reply_update_counts', 10, 2 );
     74        add_action( 'bbp_new_reply',    'bbp_notify_topic_subscribers',   11, 5 );
     75
    7876    }
    7977
     
    342340
    343341    /**
     342     * @covers ::bbp_increase_topic_reply_count
     343     */
     344    public function test_bbp_increase_topic_reply_count() {
     345        $t = $this->factory->topic->create();
     346
     347        $count = bbp_get_topic_reply_count( $t );
     348        $this->assertSame( '0', $count );
     349
     350        bbp_increase_topic_reply_count( $t );
     351
     352        $count = bbp_get_topic_reply_count( $t );
     353        $this->assertSame( '1', $count );
     354    }
     355
     356    /**
     357     * @covers ::bbp_decrease_topic_reply_count
     358     */
     359    public function test_bbp_decrease_topic_reply_count() {
     360        $t = $this->factory->topic->create();
     361
     362        $count = bbp_get_topic_reply_count( $t );
     363        $this->assertSame( '0', $count );
     364
     365        // Set the count manually to 2
     366        bbp_update_topic_reply_count( $t, 2 );
     367
     368        $count = bbp_get_topic_reply_count( $t );
     369        $this->assertSame( '2', $count );
     370
     371        bbp_decrease_topic_reply_count( $t );
     372
     373        $count = bbp_get_topic_reply_count( $t );
     374        $this->assertSame( '1', $count );
     375    }
     376
     377    /**
    344378     * @covers ::bbp_bump_topic_reply_count_hidden
    345379     */
     
    357391        $count = bbp_get_topic_reply_count_hidden( $t );
    358392        $this->assertSame( '4', $count );
     393    }
     394
     395    /**
     396     * @covers ::bbp_increase_topic_reply_count_hidden
     397     */
     398    public function test_bbp_increase_topic_reply_count_hidden() {
     399        $t = $this->factory->topic->create();
     400
     401        $count = bbp_get_topic_reply_count_hidden( $t );
     402        $this->assertSame( '0', $count );
     403
     404        bbp_increase_topic_reply_count_hidden( $t );
     405
     406        $count = bbp_get_topic_reply_count_hidden( $t );
     407        $this->assertSame( '1', $count );
     408    }
     409
     410    /**
     411     * @covers ::bbp_decrease_topic_reply_count_hidden
     412     */
     413    public function test_bbp_decrease_topic_reply_count_hidden() {
     414        $t = $this->factory->topic->create();
     415
     416        $count = bbp_get_topic_reply_count_hidden( $t );
     417        $this->assertSame( '0', $count );
     418
     419        // Set the count manually to 2
     420        bbp_update_topic_reply_count_hidden( $t, 2 );
     421
     422        $count = bbp_get_topic_reply_count_hidden( $t );
     423        $this->assertSame( '2', $count );
     424
     425        bbp_decrease_topic_reply_count_hidden( $t );
     426
     427        $count = bbp_get_topic_reply_count_hidden( $t );
     428        $this->assertSame( '1', $count );
    359429    }
    360430
Note: See TracChangeset for help on using the changeset viewer.