2011-04-18 44 views
0

的某些部分我有執行下列步驟瓶頸放慢我的劇本

  1. 用戶登錄腳本中,增加了一個短信,並指定收件人。該信息被添加到「排隊的消息表」中;
  2. 某些進程使用API​​發送消息並將消息移至「已發送消息表」;
  3. 發送傳送報告並從「已發送消息表」中刪除消息,並將引用已發送消息的日誌條目添加到「已發送消息日誌表」中。

當消息量大被排隊在「隊列消息表」,步驟(2)和(3)需要很長的時間,

消息之前被推到API,隨機爲每個收件人生成唯一的ID,以供稍後在檢索報告時參考,該ID用於「發送消息日誌表」表。

下面是示例腳本

<?php 
class Message { 

    /* 
    * random unique id for mobile number 
    */ 
    protected $mobile_ids = array(); 

    public function add_unique_id($id, $mobile) 
    { 
     $this->mobile_ids[] = array($id, $mobile); 
    } 

    public function get_unique_id() 
    { 
     return $this->mobile_ids; 
    } 

    // The method that generated the xml for API 
    public function makeXML($param,$multi_part=false) 
    { 
      $xmlString = 
      "<SMS> 
      <authentification> 
      <username>sss</username> 
      <password>sss</password> 
      </authentification> 
      <message> 
      <sender>sender</sender>"; 
      if($multi_part == "longSMS") $xmlString .= "<type>longSMS</type>"; 

      $xmlString .= "<text>{$param['text']}</text></message><recipients>"; 

      // Array of mobile numbers came from $param 
      $phone_numbers = $param['numbers']; 

      // Loop through the array and generate <gsm messageId='0001'>mobile</gsm> 
      foreach($phone_numbers as $mobile) { 

       // Generate id for mobile 
       $msg_id = $this->make_random_int(); 

       /** 
       * This is the slow part of the script, 
       * IDs are added to the array for logging into the database 
       * When message is sent, i looped through the id and created a log for this message 
       **/ 
       $this->add_unique_id($msg_id, $mobile); 


       $xmlString .= "<gsm messageId=\"{$msg_id}\">{$mobile}</gsm>"; 
      } 
      $xmlString .= "</recipients></SMS>"; 
      return $xmlString; 
     } 

     /** 
      * This is the method that created the log 
     * Log the sms 
     * You will need to call $msgid = $this->update_db('the sms') 
     * return value of $msgid is last_insert_id 
     */ 
     public function log_sms($msgid) { 
      // Log the currently sent message 
      $userData = array(); 
      $now = date('Y-m-d H:i:s'); 
      foreach ($this->mobile_ids as $data) { 
       $userData[] = "('{$msgid}', '{$data[0]}', '{$data[1]}', 'QUEUED', '0000-00-00', '0000-00-00', '{$now}')"; 
      } 

      $query = 'INSERT INTO sent_sms_log (txtId,msgID,mobile,status,sentdate_time,deliver_date_time,sysdate_time) VALUES' . implode(',', $userData); 
      $this->ci->db->query($query); 

      $this->mobile_ids = array(); // reset the array 
     }  
    // Make random int 
     protected function make_random_int() { 
      $this->ci->load->helper('string'); 
      $int = random_string('numeric', 12); 
      return $int; 
     } 

     /** 
     * Update database after sms sent 
     * @return int 
     */ 
     public function update_db($msg, $owner, $qid=0) { 
      $data = array('qid'=> $qid, 'sms' => $msg, 'date_time' => date('Y-m-d H:i:s'), 'owner' => $owner); 
      $this->ci->db->insert('f_sent_sms', $data); 
      return $this->ci->db->insert_id(); 
     } 
} 

回答

0

我猜測它有可能成爲你正在使用的API。我不得不與apis一起工作,因爲極端的速度很慢。也許嘗試剖析代碼的不同部分具有標杆類:

http://codeigniter.com/user_guide/libraries/benchmark.html

那將是發現代碼中最慢的部分快速而簡便的方法。

+0

將消息傳遞給API是可以的,通過ID循環並將其記錄到數據庫中是很慢的部分,我想知道是否可以找到更好的方法來生成並記錄ID – elf1984 2011-04-18 23:21:00

0

我猜這是在某種循環上運行嗎?而不是一次插入一個未知數量的記錄,請查看用戶指南中的Active Record的insert_batch()方法http://codeigniter.com/user_guide/database/active_record.html您可以使用一次數據庫調用來插入所有記錄。所有循環將不必將數據插入數據庫,而是構建一個將插入的所有數據的數組。循環完成後,爲剛剛構建的數組運行insert_batch('f_sent_sms',$ my_data)。

@Matthew已經說過,所有這一切都是一個好主意(之前和之後)。

+0

如果您閱讀示例代碼上面,檢查這個方法log_sms(),那是你在說什麼?我從ID數組構建一個查詢並運行一次查詢來插入數據 – elf1984 2011-04-19 21:00:46