`
hanyh
  • 浏览: 228409 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

    博客分类:
  • php
阅读更多
共享内存计数的时候没有锁定,会造成少量的更新遗失,不过对整体来说,遗失的数据量没有大的影响。


  <?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;
         }
     }
}


?> 
1
0
分享到:
评论

相关推荐

    数据插入数据库5种方式工具源码2012825

    数据插入数据库5种方式工具源码 功能描述: 基本Insert Into 单线程Bulk Insert 多线程Bulk Insert 单线程SqlBulkCopy 多线程SqlBulkCopy 试验的5种方法,比较各种方法的导入速度 要把导入的文件放在C盘如:c:\\20...

    网啦搜索引擎系统v1.0

    本子系统通过BCP把所有网页批量存入数据库,根据实际测试,其执行效率是数据库插入操作的十倍,大大节省操作时间,并且大大降低数据库负荷。 • WEB搜索子系统:英文名WEB Search Engine,用于全文检索数据库中的...

    mysql数据库

    MySQL是一个多线程的,结构化查询语言(SQL)数据库服务器。SQL 在世界上是最流行的数据库语言。MySQL 的执行性能非常高,运行速度非常快,并非常容易使用。是一个非常棒的数据库。新的版本可以作为复杂情况以及大负荷...

    API搜索引擎(简单易上手适用于各种课程设计,附带几个测试用例) 展示:http://43.143.77.107:8090

    5、利用线程池进行多线程处理提高索引保存速度 6、利用 AOP 切面计算索引保存时间 7、搜索模块利用 HTML+CSS+JS 实现前端搜索功能 8、为索引构建 搜索树 操作方法:1、找到文档中db.sql运行数据库2、下载解压后在...

    大数据量Excel 数据导入系统的设计与实现 (2014年)

    该系统采用C#语言,在VS2012的开发环境中,利用多线程技术将大数据量Excel表格数据导入到SQL Server中,加快了信息导入的速度。并且在临时表与目标表之间的合并过程中,摒弃了传统的游标逐条插入方式,取而代之的是...

    BigData:SQLite单表4亿订单,大数据测试

    写入向前日志模式,避免多线程访问时锁定数据库,写入时不必使用排它锁影响其它线程读取,而是把事务操作写入到WAL文件中,延迟合并 加大缓存,Cache Size=5000,提升性能。操作系统通过文件映射MapFile把整个数据库...

    dal-benchmark:数据访问层基准

    不要认为这是数据库基准,因为没有网络往返,并且仅使用单个客户端线程与数据库进行通信。 ###楷模 ###测试假设 装载整个骨​​料 简单模型是单个表的数据源 标准模型表示父/子关系。 实施可以自由选择一个表或...

    Delphi5开发人员指南

    11.5 多线程与数据库 335 11.6 多线程与图形处理 340 11.7 总结 343 第12章 文件处理 344 12.1 处理文件的输入/输出 344 12.1.1 文本文件的处理 344 12.1.2 类型文件的处理 348 12.1.3 无类型文件的处理 356 12.2 ...

    ORACLE9i_优化设计与系统调整

    §1.1 Oracle数据库结构 23 §1.1.1 Oracle数据字典 23 §1.1.2 表空间与数据文件 24 §1.1.3 Oracle实例(Instance) 24 §1.2 Oracle文件 26 §1.2.1 数据文件 26 §1.2.2 控制文件 26 §1.2.3 重做日志文件 26 §...

    vc++ 开发实例源码包

    实现了自绘控件,云端控制主要在CnComm类多线程串口通讯库, camerads-DirectShow使用示例 演示了摄像头的使用 CatListBoxDemo ListBox控件与其它控件阙套使用方法 CCAMS系统是一种用于局域网下的CS模式的软件...

    基于J2EE框架的个人博客系统项目毕业设计论文(源码和论文)

    Java的产生与流行是当今Internet发展的客观要求,Java是一门各方面性能都很好的编程语言,它的基本特点是简单、面向对象、分布式、解释的、健壮的、安全的、结构中立的、可移植的、性能很优异的、多线程的、动态的,...

    MYSQL

    10.2.3 调节服务器参数 10.2.4 MySQL 怎样打开和关闭数据库表 10.2.5 在同一个数据库中创建大量数据库表的缺点 10.2.6 为什么有这么多打开的表? 10.2.7 MySQL 怎样使用内存 10.2.8 MySQL ...

    BFC论坛采集器2.0 Beta 3

    &lt;br&gt;多线程采集与发布,使得采集速度更进一步,而系统占用率降低。 &lt;br&gt;同时兼顾全自动采集的方便快捷与手动采集的自由选择。 &lt;br&gt; 更新历史: &lt;br&gt; BFCImporter2.0 Beta 3: (文件日期:...

    MySQL中文参考手册

    o 4.15 安装后期(post-installation)的设置与测试 + 4.15.1 运行mysql_install_db 的问题 + 4.15.2 启动 MySQL 服务器的问题 + 4.15.3 自动启动和停止 MySQL + 4.15.4 选项文件 o 4.16 升级和降级...

    超级有影响力霸气的Java面试题大全文档

    与cgi的区别在于servlet处于服务器进程中,它通过多线程方式运行其service方法,一个实例可以服务于多个请求,并且其实例一般不会销毁,而CGI对每个请求都产生新的进程,服务完成后就销毁,所以效率上低于servlet。...

    易语言程序免安装版下载

    修改高级表格支持库,解决插入行/插入列在未指定行号/列号的情况下插入位置不正确的BUG。 7. 修改文本语音转换支持库,增加“机读文本.重新创建并初始化()”方法。 8. 修改应用接口支持库,增强“取快捷方式目标...

    MySQL 5.1中文手冊

    在同一个数据库中创建多个表的缺陷 7.5. 优化MySQL服务器 7.5.1. 系统因素和启动参数的调节 7.5.2. 调节服务器参数 7.5.3. 控制查询优化器的性能 7.5.4. 编译和链接怎样影响MySQL的速度 7.5.5. MySQL如何使用内存 ...

Global site tag (gtag.js) - Google Analytics