Hello,
I have a PHP evaluation rule which adds tickets to the cart depending on the number of people signed up in a form.
The example function is a switch statement which is this:
$counting number is the number of tickets to add
$order = commerce_cart_order_id($user->uid);
//loops around each ticket and adds them to the cart.
for ($i = 0; $i < $count ; $i++) {
$product = commerce_product_load($productID[$i]);
switch ($product->field_ticket_type['und'][0]['value']) {
case 'dan':
if ($countingnumber > 0){
if($entity->type != 'hiphop_battle'){
$line_item = commerce_product_line_item_new($product, $countingnumber, $order);
commerce_cart_product_add($user->uid,$line_item);
if($hiphopcount > 0){
//apply discount if user already applied for hiphop
$discount = commerce_product_load(3);
$line_item = commerce_product_line_item_new($discount, $hiphopcount, $order);
commerce_cart_product_add($user->uid,$line_item);
}
}
}
break;
case 'spec':
if( $entity->field_spectators['und'][0]['value'] > 0){
$line_item = commerce_product_line_item_new($product, $entity->field_spectators['und'][0]['value'], $order);
commerce_cart_product_add($user->uid,$line_item);
}
break;
case 'hip':
if ($entity->type == 'hiphop_battle' && $countingnumber != 0){
$line_item = commerce_product_line_item_new($product, $countingnumber, $order);
commerce_cart_product_add($user->uid,$line_item);
}
break;
case 'style':
if ($entity->type == 'hiphop_battle'){
$countstyle = count($entity->field_select_style_hhform['und']);
$line_item = commerce_product_line_item_new($product, $countstyle, $order);
commerce_cart_product_add($user->uid,$line_item);
}
break;
}
}
however this single switch statement can take over 20 seconds to run.
I tracked down the offending function to be inside commerce_cart_product_add(), Specifically the function commerce_order_save().
I took the commerce_cart_product_add() function apart and ran a message system to display the time it took in seconds (using function microtime(true)) to run that function. Here is the dump out of the function running.
Do not add the line item if it doesn't have a unit price. Took :
5.6028366088867E-5
First attempt to load the customer's shopping cart order. took :
0.016276121139526
order is :
Set the incoming line item's order_id. took :
1.9073486328125E-6
Wrap the order for easy access to field data took :
3.6001205444336E-5
Invoke the product prepare event with the shopping cart order took :
0.0040998458862305
STARTING COMBINE FUNCTION!!!!!!!
Generate an array of properties and fields to compare
2.1457672119141E-6
Add any field that was exposed on the Add to Cart form to the array
1.215934753418E-5
Loop over each line item on the order
0.00032305717468262
Total combine time
0.0016388893127441
LEAVING COMBINE FUNCTION !!!!!!!!!!
Save the Updated Order
17.695978879929
order save type return is:
2
Test Ticket Dancer added to your cart.
total function time :
29.745914936066
Is there any reason the saving of an order should take between 17~22 seconds?
Any advice would be appreciated :)
Comments
Comment #1
LeeUDO commentedComment #2
rszrama commentedNot sure, but I am sure this isn't a normal scenario (if that helps : ).
I think you're going to have to dig deeper on your end to understand why writing to your database is taking so long. I test Drupal Commerce day in / day out and use it in a wide variety of production websites where this is not happening. In fact, I've never seen it going so slowly.
Do you know how to evaluate your MySQL usage or optimize it for performance? Is it possible you're waiting on web services to process or invoking a variety of other functions when the order save is computed? Those are the things you'll need to track down to begin to get to the bottom of it.
Comment #3
LeeUDO commentedAfter doing a bit of debugging, the issue seemed to stem from the specific order that the system was writing too. It seems that a bug was inured when setting a line item to a value of 0, then clicking remove on another line item.
While I couldn't see it in the database, I had been doing this regularly during my testing phase and it is the only occurrence I can think of which might cause this kind of issue. Maybe something to do with the allocation of memory by the database? (Kinda clutching at straws here) After deleting the order from the database and setting up with a new order, the system runs very smoothly.
Thanks for making this awesome module :)
Comment #4
rszrama commentedYou bet! Glad you got it sorted out. : )