0

I have the following three functions that are interacting with the Amazon API. When I use the parse_orders_result function, it appears to take approximately 30 seconds to complete. I discovered this by running this get_orders function with the parse_orders_result commented out, and it completes each iteration of the do while loop in 3 seconds. However, when I run the parsing stuff, it takes the 30 seconds I mentioned below. How can I improve the performance of this operation?

GET_ORDERS function:

public function get_orders($date)
        {
        $this->throttle = amazon_throttle::list_orders_throttle();
        $this->endpoint = "mws.amazonservices.com";
        $this->url = "https://mws.amazonservices.com/Orders/$date";
        $this->action = "ListOrders";
        $this->version = '2011-01-01';
        $this->api = "/Orders/$date\n"; 
        $this->options = array( 
                                'Action' => 'ListOrders',
                                'OrderStatus.Status.1'=>'Unshipped',
                                'OrderStatus.Status.2'=>'PartiallyShipped', 
                                'OrderStatus.Status.3'=>'Shipped',
                                'OrderStatus.Status.4'=>'Canceled',//Get all unshipped, partially shipped, shipped, and canceled orders. This will update these types of orders if their status changes on Amazon
                                'LastUpdatedAfter'=> date("c", strtotime('-1 Month')) //FIXME update this to reflect our download interval later
                               );
        $this->create_signature();
        try 
            {       
            $results = $this->send_request(); //commented out for testing
            }
        catch (Exception $e)
            {
             echo $e->getMessage();
             return false;
            } 

        //$results = sql::value("SELECT api_response from dhs.dbo.api_response where id = 444");
        $xml = new SimpleXMLIterator($results); //make the iterator here so we can check for a nexttoken
        $orders = $this->parse_orders_result($xml->ListOrdersResult->Orders);
        if ($xml->ListOrdersResult->NextToken)
            {
            $next_token = (string)$xml->ListOrdersResult->NextToken;
            do 
                {
                $results = $this->get_orders_next_token($next_token);
                $xml = new SimpleXMLIterator($results);
                $returned = $this->parse_orders_result($xml->ListOrdersByNextTokenResult->Orders);
                $orders = array_merge($orders, $returned);
                if ($xml->ListOrdersByNextTokenResult->NextToken)
                    $next_token = (string) $xml->ListOrdersByNextTokenResult->NextToken;
                else
                    $next_token = null;
                }
            while($next_token);
            }
        //this stored procedure creates patient_ids for each order (if the patient doesn't already exist) and updates the orders table with with their patient_id
        sql::query('EXEC dhs.dbo.sp_create_patients');
        return $orders;
        }

PARSE_ORDERS_RESULT function:

$xml->registerXPathNamespace('amz', 'https://mws.amazonservices.com/Orders/2011-01-01');
            $orders = array();

            foreach ($xml->Order as $order)
            {
                $last = null;
                $name = explode(' ', preg_replace('/[^A-z ]/', '', (string) $order->BuyerName)); //remove all special characters
                if (!isset($name[1]) || !$name[1])
                {
                    $attn_last = explode(' ', preg_replace('/[^A-z ]/', '',$order->ShippingAddress->Name)); //remove all special characters
                    $pos = count($attn_last) - 1;
                    $last = $attn_last[$pos];
                }
                $phone = preg_replace('/[^0-9]/', '', (string) $order->ShippingAddress->Phone); //remove everything that is not an int
                $phone = "(" . substr($phone, 0,3) . ")" . substr($phone,3, 3) . "-" . substr($phone, 6); //format phone number

                //order and ship status are both int values tied to information in the db
                $ship_method = array_search(strtoupper((string) $order->ShipmentServiceLevelCategory),
                        sql::two_column_array("SELECT id, description FROM dhs.dbo.shipping_method WHERE site IS NULL or site = 'AMAZON' "));
                $order_status = array_search(strtoupper((string) $order->OrderStatus),
                        sql::two_column_array("SELECT id, description FROM dhs.dbo.order_status WHERE site IS NULL or site = 'AMAZON' "));
                //you have to cast found elements as string or they are returned as SimpleXMLIterator objects and you can't get the value
                $arr = array(
                        'order_code'=> (string) $order->AmazonOrderId,
                        'shipping_method'=>$ship_method,
                        'amount'=> (string) $order->OrderTotal->Amount,
                        'purchased_on' => date('Y-m-d H:i:s', strtotime($order->PurchaseDate)),
                        'market_id' => (string) $order->MarketplaceId[0],
                        'status'=> $order_status,
                        'site'=> 'AMAZON',
                        'last_update' => date('Y-m-d H:i:s'),
                        'employee_id'=>login::$id,
                        'addr1'=> (string) $order->ShippingAddress->AddressLine1,
                        'addr2'=>(string) $order->ShippingAddress->AddressLine2,
                        'city'=>(string) $order->ShippingAddress->City,
                        'state'=>(string) $order->ShippingAddress->StateOrRegion,
                        'zip'=>(string) $order->ShippingAddress->PostalCode,
                        'attention'=>(string) $order->ShippingAddress->Name,
                        'email'=>(string) $order->BuyerEmail,
                        'phone'=>$phone,
                        'first'=>array_shift($name), //remove the 0 element, use array shift so we can glue the rest of the array together for last name
                        'last'=>(implode(' ', $name)) ? implode(' ', $name): $last,
                        'fullname'=>(string) $order->BuyerName,
                        'unshipped_items'=> (string) $order->NumberOfItemsUnshipped
                );
                foreach ($arr as $k => $v)
                    $arr[$k] = strtoupper($v);

                $id = sql::merge('dhs', 'dbo', 'orders', array('order_code', 'site'), $arr, 'id');
                $arr['order_id'] = $id;
                sql::merge('dhs', 'dbo', 'order_shipping', array('order_id'), $arr);

                $orders[] = $arr;
            }
            return $orders;
        }

UPDATE

Unfortunately, I am not able to install XDebugger (its a company server and I don't have permission). However, I can provide a little more insight:

  • $xml->Orders will be a maximum of 100 orders (so only 100 iterations), with approximately 1000 characters per order
  • Although we are doing multiple database requests per iteration (which I will remove), there are only at max 150 users hitting the server at any one time (likely fewer than 70)
  • Server Environment: IIS 6, PHP 5.3, MSSQL Server 2008 R2, 48GB RAM, 16 2.7ghz processor cores (hyperthreaded to replicate 32 processors)
4

1 に答える 1

0

この問題は、データベースに対して実行していたクエリの数に間違いなく関係していました。多次元配列を受け入れる新しいマージステートメントを作成し、foreachループから不要なクエリを削除し、実行時間を24秒に短縮しました(do-whileループで5回の反復)。以下は、改訂された解析順序関数です。

private function parse_orders_result(SimpleXMLIterator $xml)
        {
            $shipping_methods = sql::two_column_array("SELECT id, description FROM dhs.dbo.shipping_method WHERE site IS NULL or site = 'AMAZON' ");
            $order_statuses = sql::two_column_array("SELECT id, description FROM dhs.dbo.order_status WHERE site IS NULL or site = 'AMAZON' ");
            $xml->registerXPathNamespace('amz', 'https://mws.amazonservices.com/Orders/2011-01-01');
            $orders = array();

            foreach ($xml->Order as $order)
            {
                $last = null;
                $name = explode(' ', preg_replace('/[^A-z ]/', '', (string) $order->BuyerName)); //remove all special characters
                if (!isset($name[1]) || !$name[1])
                {
                    $attn_last = explode(' ', preg_replace('/[^A-z ]/', '',$order->ShippingAddress->Name)); //remove all special characters
                    $pos = count($attn_last) - 1;
                    $last = $attn_last[$pos];
                }
                $phone = preg_replace('/[^0-9]/', '', (string) $order->ShippingAddress->Phone); //remove everything that is not an int
                $phone = "(" . substr($phone, 0,3) . ")" . substr($phone,3, 3) . "-" . substr($phone, 6); //format phone number

                //order and ship status are both int values tied to information in the db
                $ship_method = array_search(strtoupper((string) $order->ShipmentServiceLevelCategory), $shipping_methods);
                $order_status = array_search(strtoupper((string) $order->OrderStatus), $order_statuses);
                //you have to cast found elements as string or they are returned as SimpleXMLIterator objects and you can't get the value
                $arr = array(
                        'order_code'=> (string) $order->AmazonOrderId,
                        'shipping_method'=>$ship_method,
                        'amount'=> (string) $order->OrderTotal->Amount,
                        'purchased_on' => date('Y-m-d H:i:s', strtotime($order->PurchaseDate)),
                        'market_id' => (string) $order->MarketplaceId[0],
                        'status'=> $order_status,
                        'site'=> 'AMAZON',
                        'last_update' => date('Y-m-d H:i:s'),
                        'employee_id'=>login::$id,
                        'addr1'=> (string) $order->ShippingAddress->AddressLine1,
                        'addr2'=>(string) $order->ShippingAddress->AddressLine2,
                        'city'=>(string) $order->ShippingAddress->City,
                        'state'=>(string) $order->ShippingAddress->StateOrRegion,
                        'zip'=>(string) $order->ShippingAddress->PostalCode,
                        'attention'=>(string) $order->ShippingAddress->Name,
                        'email'=>(string) $order->BuyerEmail,
                        'phone'=>$phone,
                        'first'=>array_shift($name), //remove the 0 element, use array shift so we can glue the rest of the array together for last name
                        'last'=>(implode(' ', $name)) ? implode(' ', $name): $last,
                        'fullname'=>(string) $order->BuyerName,
                        'unshipped_items'=> (string) $order->NumberOfItemsUnshipped
                );
                foreach ($arr as $k => $v)
                    $arr[$k] = strtoupper($v);

                $orders[] = $arr;
            }
            sql::bulk_merge('dhs', 'dbo', 'orders', array('order_code', 'site'), $orders, 'id');
            sql::bulk_merge('dhs', 'dbo', 'order_shipping', array('order_code', 'site'), $orders);
            return $orders;
        }
于 2012-12-21T19:06:34.320 に答える