PHP多进程制作数据

要测试atlas性能如何就制作几千万数据,肯定要用到并行写入,测试脚本如下

主进程

for($i=0; $i<100; $i++) {
    $command = sprintf('/usr/local/bin/php -f %s %d &', '/Users/apple/atlas/makeData.php', $i);
    $handler = popen($command, 'r');
        //注释掉以下2行各个子进程独立并行工作,加上则变成串行工作
    //fread($handler, 1000);
    //pclose($handler);
    sleep(1);
}

工作进程

connect('127.0.0.1', 6379);

//启动程序
$start = 0;
$tableNo = isset($argv[1]) ? $argv[1] : 0;
$max = mt_rand(20000, 50000);
while ($start<$max) {
    createUser($tableNo, $start);
    $start++;
}
exit(0);

//插入用户
function createUser($tableNo=0, $i){
    global $pdo, $redis;
    try {
        $max_userid = $redis->incr('max_userid');
        $sql = sprintf("INSERT INTO `%s` (`user_id`, `username`,`password`,`realname`) 
                VALUES ( '%d', '%s', '%s', '%s')", 
            'user' . str_pad($tableNo, 5, 0, STR_PAD_LEFT),
            $max_userid,
            md5(mt_rand()), 
            sha1(mt_rand()), 
            uniqid($i)
        );
        $pdo->executeUpdate($sql);
    }catch (Exception $ex){
        echo $ex->getMessage();
    }
}

你可能感兴趣的:(PHP多进程制作数据)