多线程数据库插入速度测试

共享内存计数的时候没有锁定,会造成少量的更新遗失,不过对整体来说,遗失的数据量没有大的影响。



<?php
/**
*

*
* 每个进程都单独连接数据库
* */

//configruation parameters

$tbl_id = 0;
$count_per_process = 10;
$concurrents = 100;
$logpath = "/tmp/s.log";
$host = "10.218.26.75";
$user = "user";
$pwd = "pwd";
$db = "test";
$port = 3306;
$sep = 1000;
$uid_count = 100000;
$uid_path= 100000;

if ($argc != 13) {
echo "Usage:php multiprocess.php tbl_id concurrents count_per_process logpath host user pwd db port sep_counts uid_count uid_path\n";
echo "Usage:php multiprocess.php 表ID 并发数 每进程多少次 记录路径 主机 用户 密码 数据库 端口 多少条记录一次 用户数 用户UID保存路径\n";
exit(1);
} else {
$tbl_id= trim($argv[1]);
$concurrents = (int)trim($argv[2]);
$count_per_process= (int)trim($argv[3]);
$logpath = trim($argv[4]);
$host = trim($argv[5]);
$user = trim($argv[6]);
$pwd = trim($argv[7]);
$db = trim($argv[8]);
$port = trim($argv[9]);
$sep = trim($argv[10]);
$uid_count= (int)trim($argv[11]);
$uid_path = trim($argv[12]);
if($uid_count < $concurrents and ($uid_count % $concurrents !=0 )) {
die("UID数必须多过并发进程数并曲uid数必须是进程数的整数倍\n");
}
$uid_count = $uid_count / $concurrents;

}

$shm_key = ftok(__FILE__, 't');
$shm_id = shmop_open($shm_key, "c", 0644, 100);
shmop_write($shm_id, 0, 0);

$m_start= microtime(true);
$conf = array(
'count' => $count_per_process,
'host' => $host,
'user' => $user,
'pwd' => $pwd,
'db' => $db,
'port' => $port,
'logpath' => $logpath,
'shm_id' => $shm_id,
'tbl_id' => $tbl_id,
'sep' => $sep,
'uid_count' => $uid_count,
'uid_path' => $uid_path,
);

error_log("+++++++++++++++++++++begin++++++++++++++++\n",3,$logpath);
error_log("$m_start\n",3,$logpath);
for ($i = 1; $i <= $concurrents; ++$i) {
$pid = pcntl_fork();
if (!$pid) {
$worker = new Simulator(getmypid(),$conf);
$worker->execute();
exit($i);
}
}

//main process
while (pcntl_waitpid(0, $status) != -1) {
$status = pcntl_wexitstatus($status);
echo "Child $status completed\n";
}
$m_end = microtime(true);
$m_duration = $m_end - $m_start;
echo "total time consumes:".$m_duration."\n";



$shm_size = shmop_size($shm_id);
$counter_str= shmop_read($shm_id, 0, $shm_size);
echo "shm counter :$counter_str\n";
$e =(int)$count_per_process * (int)$concurrents;
echo "theory counter :$e \n";

if (!shmop_delete($shm_id)) {
echo "Couldn't mark shared memory block for deletion.";
}
shmop_close($shm_id);
error_log("$m_end\n",3,$logpath);
error_log("+++++++++++++++++++++end++++++++++++++++\n",3,$logpath);

class Simulator{
private $link;
private $pid;
private $sep; //每隔多少条记录时间
private $tbl_id;
private $uid_path;
private $uid_count;
private $uid_tmp_count;
private $uid_is_ok;
private $uid_data;
private $count;
private $logpath;
private $error_path = "error.log";
private $counter=0;
private $domains = array(".com",".cn",".info",".org",".net",".biz",".mil",".net",".jp",".tw");
private $domain_size;

function __construct($pid,$conf) {
$this->pid= $pid;
if($conf['tbl_id'] == 0) {
$this->tbl_id= '';
} else {
$this->tbl_id= $conf['tbl_id'];
}
$this->domain_size = count($this->domains)-1;
$this->count = $conf['count'];
$this->sep = (int)$conf['sep'];
$this->shm_id = $conf['shm_id'];
$this->logpath = $conf['logpath'];
$this->uid_path = $conf['uid_path'];
$this->uid_count = (int)$conf['uid_count'];
$this->uid_is_ok = false; //关键字
$this->uid_tmp_count = 0; //

$this->link = mysql_connect($conf['host'].":".$conf['port'],$conf['user'],$conf['pwd']);
if(!$this->link) {
var_dump($conf);
die("connected faild:".mysql_error());
}
$db_selected = mysql_select_db($conf['db'],$this->link);
if(!$db_selected) {
die("can't use db:".mysql_error());
}
}

function __destruct() {
if($this->link) {
mysql_close($this->link);
}
if($this->uid_is_ok) {
//echo "destory";
$path = $this->uid_path;
//$path = $this->uid_path."-$this->pid";
$fp = fopen($path,"a");
if($fp) {
$contents = "";
$i = 1;
foreach($this->uid_data as $uid) {
if($i % 2000 == 0) {
$contents .= $uid."\n";
fwrite($fp,$contents);
$contents = "";
}
$contents .= $uid."\n";
}
fwrite($fp,$contents);
fclose($fp);
}

}
}

private function inc() {
$counter_str= shmop_read($this->shm_id, 0, 100);
$c = (int) $counter_str + 1;
if($c % $this->sep == 0) {
error_log(microtime(true)." $c\n",3,$this->logpath);
}
shmop_write($this->shm_id, $c, 0);
}


public function execute() {
$start = microtime(true);
$this->pre = microtime(true);
// echo $this->count;
for($i = 0;$i < $this->count;$i++) {
$uid = $this->getUid();
$email = $this->getEmail();
$name = $this->getName();
$sql = "INSERT INTO contact{$this->tbl_id}(uid,name,email) VALUES ($uid,'$name','$email')";
$result = mysql_query($sql,$this->link);
$this->inc();
if(!$result) {
error_log(mysql_error()."\n",3,$this->error_path);
}
}
$end = microtime(true);
$duration = $end - $start;
//$line = "process ".$this->pid." ".$count." ".$duration."\n";
//echo "$line";
// error_log($line,3,$this->logpath);

}

private function getEmail() {
$email = $this->getName()."@".$this->getName().$this->domains[mt_rand(0,$this->domain_size)];
//echo $email."\n";
return $email;
}

private function getName() {
$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
$size = strlen($chars);
mt_srand((double)microtime()*1000000*getmypid());
$i = mt_rand(6,18);
$output = "";
while(strlen($output)<$i)
$output.=substr($chars,(mt_rand() % $size),1);
return $output;
}

private function getUid() {
if($this->uid_is_ok) {
return $this->uid_data[mt_rand(0,$this->uid_count-1)];
} else {
mt_srand((double)microtime()*1000000*getmypid());
$uid = mt_rand(1000000000,9999999999);
$this->uid_data[] = $uid;
$this->uid_tmp_count += 1;
if($this->uid_tmp_count == $this->uid_count) {
$this->uid_is_ok = true;
}
return $uid;
}
}
}


?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值