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/forums/functions/counts.php

    r6011 r6036  
    1414     */
    1515    public function test_bbp_forum_new_topic_counts() {
     16        remove_action( 'bbp_insert_topic', 'bbp_insert_topic_update_counts', 10 );
     17
    1618        $f = $this->factory->forum->create();
    1719        $t1 = $this->factory->topic->create( array(
     
    2426        $u = $this->factory->user->create();
    2527
    26         // Cheating here, but we need $_SERVER['SERVER_NAME'] to be set.
    27         $this->setUp_wp_mail( false );
     28        // Don't attempt to send an email. This is for speed and PHP errors.
     29        remove_action( 'bbp_new_topic', 'bbp_notify_forum_subscribers', 11, 4 );
    2830
    2931        // Simulate the 'bbp_new_topic' action.
    3032        do_action( 'bbp_new_topic', $t1, $f, false, bbp_get_current_user_id(), $t1 );
    3133
    32         // Reverse our changes.
    33         $this->tearDown_wp_mail( false );
    34 
    3534        $count = bbp_get_forum_topic_count( $f, true, true );
    3635        $this->assertSame( 1, $count );
     
    4645            ),
    4746        ) );
    48 
    49         // Cheating here, but we need $_SERVER['SERVER_NAME'] to be set.
    50         $this->setUp_wp_mail( false );
    5147
    5248        // Simulate the 'bbp_new_topic' action.
    5349        do_action( 'bbp_new_topic', $t2, $f, false, $u , $t2 );
    5450
    55         // Reverse our changes.
    56         $this->tearDown_wp_mail( false );
    57 
    58         $count = bbp_get_forum_topic_count( $f, true, true );
    59         $this->assertSame( 2, $count );
    60 
    61         $count = bbp_get_forum_topic_count_hidden( $f, true, true );
    62         $this->assertSame( 0, $count );
     51        $count = bbp_get_forum_topic_count( $f, true, true );
     52        $this->assertSame( 2, $count );
     53
     54        $count = bbp_get_forum_topic_count_hidden( $f, true, true );
     55        $this->assertSame( 0, $count );
     56
     57        // Re-add removed actions.
     58        add_action( 'bbp_insert_topic', 'bbp_insert_topic_update_counts', 10, 2 );
     59        add_action( 'bbp_new_topic',    'bbp_notify_forum_subscribers',   11, 4 );
    6360    }
    6461
     
    255252
    256253    /**
     254     * @covers ::bbp_increase_forum_topic_count
     255     */
     256    public function test_bbp_increase_forum_topic_count() {
     257        $f = $this->factory->forum->create();
     258
     259        $count = bbp_get_forum_topic_count( $f );
     260        $this->assertSame( '0', $count );
     261
     262        bbp_increase_forum_topic_count( $f );
     263
     264        $count = bbp_get_forum_topic_count( $f );
     265        $this->assertSame( '1', $count );
     266    }
     267
     268    /**
     269     * @covers ::bbp_decrease_forum_topic_count
     270     */
     271    public function test_bbp_decrease_forum_topic_count() {
     272        $f = $this->factory->forum->create();
     273
     274        $count = bbp_get_forum_topic_count( $f );
     275        $this->assertSame( '0', $count );
     276
     277        $t = $this->factory->topic->create_many( 2, array(
     278            'post_parent' => $f,
     279        ) );
     280
     281        bbp_update_forum_topic_count( $f );
     282
     283        $count = bbp_get_forum_topic_count( $f );
     284        $this->assertSame( '2', $count );
     285
     286        bbp_update_forum_topic_count( $f );
     287
     288        bbp_decrease_forum_topic_count( $f );
     289
     290        $count = bbp_get_forum_topic_count( $f );
     291        $this->assertSame( '1', $count );
     292    }
     293
     294    /**
    257295     * @covers ::bbp_bump_forum_topic_count_hidden
    258296     */
     
    270308
    271309    /**
     310     * @covers ::bbp_increase_forum_topic_count_hidden
     311     */
     312    public function test_bbp_increase_forum_topic_count_hidden() {
     313        $f = $this->factory->forum->create();
     314
     315        $count = bbp_get_forum_topic_count_hidden( $f );
     316        $this->assertSame( '0', $count );
     317
     318        bbp_increase_forum_topic_count_hidden( $f );
     319
     320        $count = bbp_get_forum_topic_count_hidden( $f );
     321        $this->assertSame( '1', $count );
     322    }
     323
     324    /**
     325     * @covers ::bbp_decrease_forum_topic_count_hidden
     326     */
     327    public function test_bbp_decrease_forum_topic_count_hidden() {
     328        $f = $this->factory->forum->create();
     329
     330        $count = bbp_get_forum_topic_count_hidden( $f );
     331        $this->assertSame( '0', $count );
     332
     333        $t = $this->factory->topic->create_many( 2, array(
     334            'post_parent' => $f,
     335            'post_status' => bbp_get_spam_status_id(),
     336            'topic_meta' => array(
     337                'forum_id' => $f,
     338                'spam_meta_status' => 'publish',
     339            )
     340        ) );
     341
     342        bbp_update_forum_topic_count_hidden( $f );
     343
     344        $count = bbp_get_forum_topic_count_hidden( $f );
     345        $this->assertSame( '2', $count );
     346
     347        bbp_decrease_forum_topic_count_hidden( $f );
     348
     349        $count = bbp_get_forum_topic_count_hidden( $f );
     350        $this->assertSame( '1', $count );
     351    }
     352
     353    /**
    272354     * @covers ::bbp_bump_forum_reply_count
    273355     */
     
    279361
    280362        bbp_bump_forum_reply_count( $f );
     363
     364        $count = bbp_get_forum_reply_count( $f );
     365        $this->assertSame( '1', $count );
     366    }
     367
     368    /**
     369     * @covers ::bbp_increase_forum_reply_count
     370     */
     371    public function test_bbp_increase_forum_reply_count() {
     372        $f = $this->factory->forum->create();
     373
     374        $count = bbp_get_forum_reply_count( $f );
     375        $this->assertSame( '0', $count );
     376
     377        bbp_increase_forum_reply_count( $f );
     378
     379        $count = bbp_get_forum_reply_count( $f );
     380        $this->assertSame( '1', $count );
     381    }
     382
     383    /**
     384     * @covers ::bbp_decrease_forum_reply_count
     385     */
     386    public function test_bbp_decrease_forum_reply_count() {
     387        $f = $this->factory->forum->create();
     388
     389        $count = bbp_get_forum_reply_count( $f );
     390        $this->assertSame( '0', $count );
     391
     392        $t = $this->factory->topic->create( array(
     393            'post_parent' => $f,
     394        ) );
     395
     396        $r = $this->factory->reply->create_many( 2, array(
     397            'post_parent' => $t,
     398        ) );
     399
     400        bbp_update_forum_reply_count( $f );
     401
     402        $count = bbp_get_forum_reply_count( $f );
     403        $this->assertSame( '2', $count );
     404
     405        bbp_decrease_forum_reply_count( $f );
    281406
    282407        $count = bbp_get_forum_reply_count( $f );
Note: See TracChangeset for help on using the changeset viewer.