个人在虚拟机centos7,单核,1G内存/***模拟并发请求,10万次写入数据库*拆分成10个进程,每个进程10000个进程插入*/$total=10000;$num=10;$per=$total/$num;$sql='';$child='';echo'start'.microtime(true).PHP_EOL;for($i=1;$i<=$num;$i++){$pid=pcntl_fork();if($pid==-1){die('分叉错误');}if($pid>0){//$id=pcntl_wait($status,WNOHANG);$孩子[]=$pid;}elseif($pid==0){$link=mysqli_connect('localhost','root','root','yii2advanced');$开始=($i-1)*$per+1;$end=$start+$per;for($j=$start;$j<$end;$j++){$time=microtime(true);$sql='插入pcntl_test(rank,time)值('.$j.','.$time.')';mysqli_query($link,$sql);}mysqli_close($link);$id=getmypid();echo'孩子'.$id.'完成'.microtime(true).PHP_EOL;退出(0);}}while(count($child)){foreach($childas$k=>$pid){$res=pcntl_waitpid($pid,$status,WNOHANG);if(-1==$res||$res>0){unset($child[$k]);}}}echo'end'.microtime(true).PHP_EOL;当$total=10000,$num=10;执行结果如下:start1491903371.5548child19860finished1491903417.2113child19857finished1491903417.6909child19864finished1491903417.7793child19855finished1491903417.8695child19859finished1491903417.9162child19861finished1491903418.0089child19856finished1491903418.0532child19863finished1491903418.0842child19862finished1491903418.1474child19858finished1491903418.4341end1491903418.4424总时间为46.88759994506836秒当$total=10000,$num=100时,执行结果如下:start1491904334.1735child20085finished1491904337.0712child20086finished1491904337.144……child20262finished1491904341.5602child20264finished1491904341.5803end1491904341.5869总时间为7.413399934768677当$total=10000,$num=1000时,执行结果如下:start1491904562.0166child20282finished1491904562.1191child20277finished1491904562.1268child20279finished1491904562.1352...child21586finished1491904576.6954child21582finished1491904576.7024child21584finished1491904576.7226end1491904576.7297总时间为14.71310019493103,相比100个子进程,耗时更长了进程切换太多,影响了了效率Shouldbeoneofthereasons.当$total=100000,$num=100时,十万条记录,100个进程插入start1491905670.2652child21647finished1491905725.4382child21651finished1491905725.4595child21642finished1491905725.5402....child21810finished1491905729.7709child21812finished1491905729.8498child21811finished1491905729.9612end1491905729.9679Thetotaltimeis59.70270013809204Ittakes18secondsforasingleprocesstoinsert10,000records,whichislesstime-consumingthan10processesforinserting10,000records.However,inserting100,000recordsinasingleprocesstakes187.40066790581hours,whichisrelativelyslow.threeminutes...However,whenIfork1,000processestoinsert100,000records,ittakesabout36secondsifitsucceeds,andanerrormayoccur.Mysqli_connectionreturnsfalse.Isthenumberofconnectionslimited?Fork10,000childprocessesandinsert1milliondata.Atthistime,therearemanycasesofconnectionerrors.Intheend,ittook360seconds,and945,300recordswereinsertedintothedatatable,withasuccessrateof94.53%.因为是查看数据库的相关配置信息mysql>showglobalstatuslike'%connect%';+----------------------------------------------+--------------------+|变量名|值|+--------------------------------------------+--------------------+|中止连接|0||连接错误接受|0||Connection_errors_internal|0||Connection_errors_max_connections|连接错误第628话Connection_errors_peer_address|连接错误0||连接错误选择|0||连接错误_tcpwrap|0||连接|16519||锁定连接|0||最大使用连接数|501||最大使用连接时间|2017-04-1215:19:54||Performance_schema_session_connect_attrs_lost|0||SSL_client_connects|0||Ssl_connect_renegotiates|0||Ssl_finished_connects|0||线程连接|4|+--------------------------------------------+--------------------+mysql>showglobalvariableslike'%connect%';+--------------------------------------------+--------------------+|变量名|值|+--------------------------------------------+--------------------+|字符集连接|utf8mb4||排序规则连接|utf8mb4_general_ci||连接超时||disconnect_on_expired_pa??ssword|开||初始化连接|||最大连接错误|100||最大连接|500||最大用户连接|0||performance_schema_session_connect_attrs_size|------------------------------+-----------------+修改myqsql配置文件,将/etc/my.cnf中的max_connections改为10000,然后重启mysql。实际MySQL服务器允许的最大连接数为16384;结果不一样,虚拟机好像挂了。当并发量大的时候,问题就出在连接mysql上。你可以尝试用连接池来解决这个问题。
