Membership 2 Pro processes are take too much time to execute as number of users get increased.

I have a membership site using plugin MS2 Pro. Where I am giving 14 day free trial to my Customers on register. I assign trial membership to customer on register and it takes too much time. It results so much time in registration process and that is something which is not get tolerated by customers. because just registration process take more than 10s is worst.

I have debug code in deeper and found that, code I have used for assign trial membership to user is takes to much time. I have logged micro time for processes and here is results and my code please find it below..

My Code

add_action( 'ms_controller_frontend_register_user_complete', 'kr_ms_controller_frontend_register_user_complete', 99, 3 );
function kr_ms_controller_frontend_register_user_complete( $user, $request, $mcf_this ){
    $membership_id = kr_get_default_membership(); // get default membership id for give trial
    $user_id = $user->id;

    if( $user_id > 0 && $membership_id > 0 && $membership_id != '' ){
        $membership = MS_Factory::load( 'MS_Model_Membership', $membership_id );
        if( $membership->has_trial()){
            $member = MS_Factory::load( 'MS_Model_Member', $user_id );
            $subscription = $member->add_membership( $membership_id, 'free' );
            $invoice = $subscription->get_current_invoice();
            if ( 0 == $invoice->total || $invoice->uses_trial ) {
                $invoice->pay_it( 'free', '' );
            }
        }
	}
}

Code after Adding Time Logger

add_action( 'ms_controller_frontend_register_user_complete', 'kr_ms_controller_frontend_register_user_complete', 99, 3 );
function kr_ms_controller_frontend_register_user_complete( $user, $request, $mcf_this ){
    $membership_id = kr_get_default_membership(); // get default membership id for give trial
    $user_id = $user->id;

    $time1 = microtime( true);
    error_log( "Time 1:". $time1 );
    if( $user_id > 0 && $membership_id > 0 && $membership_id != '' ){
        $membership = MS_Factory::load( 'MS_Model_Membership', $membership_id );

        $time2 = microtime( true);
        error_log( "Time 2:". $time2 );
        error_log( "Time Diff:". ($time2 -$time1) );
        if( $membership->has_trial()){
            $member = MS_Factory::load( 'MS_Model_Member', $user_id );

            $time3 = microtime( true);
            error_log( "Time 3:". $time3 );
            error_log( "Time Diff:". ($time3 -$time2) );

            $subscription = $member->add_membership( $membership_id, 'free' );

            $time4 = microtime( true);
            error_log( "Time 4:". $time4 );
            error_log( "Time Diff:". ($time4 -$time3) );

            $invoice = $subscription->get_current_invoice();

            $time5 = microtime( true);
            error_log( "Time 5:". $time5 );
            error_log( "Time Diff:". ($time5 -$time4) );

            if ( 0 == $invoice->total || $invoice->uses_trial ) {
                $invoice->pay_it( 'free', '' );
            }

            $time6 = microtime( true);
            error_log( "Time 6:". $time6 );
            error_log( "Time Diff:". ($time6 -$time5 ) );
            error_log( "Total Time Diff:". ($time6 -$time1 ) );

        }
	}
}

Now Here is results ( Time Log )

[18-Apr-2018 12:18:07 UTC] Time 1:1524053887.1744
[18-Apr-2018 12:18:07 UTC] Time 2:1524053887.1769
[18-Apr-2018 12:18:07 UTC] Time Diff:0.0025389194488525
[18-Apr-2018 12:18:07 UTC] Time 3:1524053887.1771
[18-Apr-2018 12:18:07 UTC] Time Diff:0.00011706352233887
[18-Apr-2018 12:18:09 UTC] Time 4:1524053889.7229
[18-Apr-2018 12:18:09 UTC] Time Diff:2.545793056488
[18-Apr-2018 12:18:10 UTC] Time 5:1524053890.7898
[18-Apr-2018 12:18:10 UTC] Time Diff:1.0669810771942
[18-Apr-2018 12:18:13 UTC] Time 6:1524053893.4499
[18-Apr-2018 12:18:13 UTC] Time Diff:2.660092830658
[18-Apr-2018 12:18:13 UTC] Total Time Diff:6.2755229473114

It took 6.28 Sec. Which is too high and its varies between 6-11 Sec.

I have staging site with same setup but less number of members are there and this same code works very fast there. (about less then 1 sec.)

So, its clear process time is get increased based on number of users.. I am worried about it because we have more and more members soon. and this led to break my site.

Users on main site: 4.6k
Users on staging site: 1.1k

Can you please take a look and give me possible way to fix this issue.

Note:
I am on PHP:7.2.4, WP: 4.9.5 and server with digital ocean with very high Memory.

Let me know if you need any other details.

Thanks,

  • Nastia

    Hello Hazem Tasmem

    Hope you're well!

    Membership 2 is a complex and heavy plugin. Please let me know if you can create a staging site and run teh same test with all other plugins deactivated, beside Membership?

    On a staging site, please add the following lines to the wp-config.php file to froze existing Memberships accounts:

    define( "MS_LOCK_SUBSCRIPTIONS", true );
    define( "MS_STOP_EMAILS", true );

    Let us know how it went. In the meantime, I am flagging here our second level support so they could provide you some feedback. Please note, our developers' response time is longer-than-normal compared to support staff response times.

    Have a good day!

    Cheers,
    Nastia