Php 我的脚本的某些部分
我有一个执行以下步骤的脚本Php 我的脚本的某些部分,php,performance,codeigniter,codeigniter-2,Php,Performance,Codeigniter,Codeigniter 2,我有一个执行以下步骤的脚本 用户登录、添加SMS消息并指定收件人。该信息被添加到“排队消息表” 一些进程使用API发送消息,消息被移动到“已发送消息表” 此时会出现一个传递报告,消息将从“已发送消息表”中删除,并将引用已发送消息的日志条目添加到“已发送消息日志表”中 当大量消息在“排队消息表”中排队时,步骤(2)和(3)需要很长时间 在将消息推送到API之前,将为每个收件人生成一个随机唯一id,以供以后检索报告时参考,该id用于表“已发送消息日志表” 下面是示例脚本 <?php class
<?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();
}
}
我猜可能是您正在使用的api。我不得不为速度非常慢的不同服务使用API。也许可以尝试使用基准类分析代码的不同部分:
这将是找到代码最慢部分的一种快速而简单的方法。我猜这是在某种循环上运行的?与每次插入一条未知数量的记录不同,请查看《用户指南》中活动记录的insert_batch()方法。您可以使用一个数据库调用插入所有记录。与将数据插入数据库的循环不同,循环所要做的只是构建一个将要插入的所有数据的数组。循环完成后,为刚构建的阵列运行insert_batch('f_sent_sms',$my_data)
正如@Matthew已经说过的那样,对其进行基准测试(之前和之后)是一个好主意。将消息传输到API是可以的,循环通过ID并将其记录到数据库是缓慢的部分,我想知道我是否能找到更好的方法来生成和记录ids如果您阅读上面的示例代码,请检查此方法log_sms(),这就是你说的吗?我从ID数组构建一个查询,并运行一次查询以插入数据