• skymvc网站测试之mysql数据生成


    skymvc网站测试之mysql数据生成

    使用方法:

    删除数据 /index.php?m=test_mysql&a=autoDelete

    重置自增ID /index.php?m=test_mysql&a=reset

    插入数据: /index.php?m=test_mysql&a=autoInsert

    <?php
    /******
    *Author 雷日锦
    *本代码在skymvc中使用
    *skymvc mysql数据库测试
    *******/
    class test_mysqlControl extends skymvc{
        public $maxrow=30;//每次最多生成多少千行
        public $maxThreads=10;
        public function __construct(){
            parent::__construct();
        }
        
        public function onDefault(){
             
        }
        
        public function onReset(){
            if(!empty($_SESSION)){
                 foreach($_SESSION as $k=>$v){
                     unset($_SESSION[$k]);
                 }
             }
            $this->onIncrement();
        }
        
        public function getNum($table){
            //设置表所需要的记录数
            $cf=array(
                "article"=>300000,
                "test"=>200000,
                "test2"=>200000,
            );
            if(!isset($cf[$table])){
                return 200000;
            }else{
                return $cf[$table];
            }
        }
        
        public function onAutoDelete(){
            set_time_limit(0);
            ob_implicit_flush(true);
            $res=M("article")->query("show tables");
            $data=M("article")->fetch_array(PDO::FETCH_NUM);
            $this->loadClass("spider");
            if($data){
                $uk=0;
                foreach($data as $k=>$t){
                    $tables[]=$t[0];
                    
                    $urls[$uk][]="http://".$_SERVER['HTTP_HOST']."/index.php?m=test_mysql&a=delete&table=".str_replace(TABLE_PRE,"",$t[0]);
                    
                    if($k%$this->maxThreads==($this->maxThreads-1)){
                        $uk++;
                    }
                    
                }
                echo "删除开始<br>";
                echo '<div id="aid">0</div>
    <script>
        var i=0;
        var it=setInterval(function(){
            i++;
            document.querySelector("#aid").innerHTML="已经执行"+i+"秒了";
        },1000);
    </script>';
                    foreach($urls as $k=>$us){
                        echo "第".$k."部分<br>";
                        flush();
                            @ob_flush();
                        $this->spider->start($us,function($data){
                        
                            echo $data['url']." <br>".$data['content']."<br>";
                            flush();
                            @ob_flush();
                        },600);
                    }
            }
            echo "本次删结束<br>";
                flush();
                @ob_flush();
                 
            echo "<script>
        setTimeout(function(){
            window.location.reload();
        },1000);
    </script>";
        }
        
        public function onDelete(){
            $table=get('table','h');
            M($table)->query("delete from ".table($table)." where 1=1 limit 50000");
            echo "delete $table success";
        }
        /*更新自增id*/
        public function onIncrement(){
            $res=M("article")->query("show tables");
            $data=M("article")->fetch_array(PDO::FETCH_NUM);
            if($data){
                foreach($data as $k=>$t){
                    $table=str_replace(TABLE_PRE,"",$t[0]);
                    M($table)->query("ALTER TABLE `sky_".$table."` AUTO_INCREMENT=1;");
                }
            }
            echo "update increment";
        }
        
        public function onAutoInsert(){
            set_time_limit(0);
            ob_implicit_flush(true);
            $res=M("article")->query("show tables");
            $data=M("article")->fetch_array(PDO::FETCH_NUM);
            $tables=array();
            if($data){
                $uk=0;
                foreach($data as $k=>$t){
                    $tables[]=$t[0];
                    
                    $urls[$uk][]="http://".$_SERVER['HTTP_HOST']."/index.php?m=test_mysql&a=insert&table=".str_replace(TABLE_PRE,"",$t[0]);
                    if($k%$this->maxThreads==($this->maxThreads-1)){
                        $uk++;
                    }
                }
                $this->loadClass("spider");
                echo "开始<br>";
                echo '<div id="aid">0</div>
    <script>
        var i=0;
        var it=setInterval(function(){
            i++;
            document.querySelector("#aid").innerHTML="已经执行"+i+"秒了";
        },1000);
    </script>';
                 
                foreach($urls as $k=>$us){
                    echo "第".$k."部分<br>";
                    flush();
                @ob_flush();
                    $this->spider->start($us,function($data){
                        
                        echo $data['url']." <br>".$data['content']."<br>";
                        flush();
                        @ob_flush();
                    },600);
                }
                echo "结束<br>";
                flush();
                @ob_flush();
            }
              
            echo "<script>
        setTimeout(function(){
            window.location.reload();
        },1000);
    </script>";
        }
        public function onInsert($table=''){
            $inauto=true;
            if(!$table){  
                set_time_limit(0);
                $inauto=false;
            }
            
            $table=$table?$table:get('table','h');
            if(!$table){
                $table="article";
            }
            
            $fields=$this->getFIelds($table);
            
            //为什么只执行到266 百思不得其解
            $jnum=$this->getNum($table);
             for($j=0;$j<$this->maxrow;$j++){
                $rscount=M($table)->selectOne(array(
                    "fields"=>" count(1)"
                ));
                if($rscount>$jnum){
                    echo $table."测试数据已经够了<br>";
                     break;
                }
                 
                if(!inauto){
                    echo "正在插入第".$j."千条<br>";
                    flush();
                    @ob_flush();
                    ob_clean();
                }
                $data=array();
                
                 
                for($i=0;$i<1000;$i++){
                    $data[]=$this->dbPost($table);
                }
                 
                $sql=" insert into ".table($table)."(".implode(",",$fields).") values ";
                foreach($data as $k=>$v){
                    if($k>0){
                        $sql.=",";
                    }
                    $sql.="("._implode($v).")";
                }
                $sql.=";";
                
                M($table)->query($sql);
                unset($data);
                unset($sql);
            }
            
            echo "success"; 
        }
        public function getFIelds($table){
            $fields=M($table)->getFields();
            foreach($fields as $k=>$v){
                if($k==0) continue;
                $data[]=$v['Field'];
            }
            return $data;
        }
        public function dbPost($table,$msg=''){
            if(isset($_SESSION["field_".$table])){
                $fields=$_SESSION["field_".$table];
            }else{
                $fields=M($table)->getFields();
                $_SESSION["field_".$table]=$fields;
            }
            
            $msg=$msg?$msg:"skymvc是".date("Ymdhis")."最贴心的".date("Ymdhis")."php开发框架,快来使用吧!";
            $data=array();
            $gid=$this->getId($table);
            foreach($fields as $k=>$v){
                if($k==0) continue;
                if(preg_match("/tinyint/i",$v['Type'])){
                    $data[$v['Field']]=rand(0,3);
                }elseif(preg_match("/int/i",$v['Type'])){
                    if($v['Field']=='dateline'){
                        $data[$v['Field']]=time();
                    }else{
                        $data[$v['Field']]=$gid;
                    }
                }elseif(preg_match("/decimal/i",$v['Type'])){
                    $data[$v['Field']]=rand(1,100000);
                }elseif(preg_match("/datetime/i",$v['Type']) ){
                    $data[$v['Field']]=date("Y-m-d H:i:s");
                }elseif($v['Field']=='bstatus'){
                    $data[$v['Field']]=1;
                }else{
                    $data[$v['Field']]=$msg;
                }
                
            }
            return $data;
        }
        
        public function getId($table){
            if(!isset($_SESSION["autoid_$table"])){
                $_SESSION["autoid_$table"]=1;
            }else{
                $_SESSION["autoid_$table"]++;
            }
            return $_SESSION["autoid_$table"];
         
        }
        
        
        
    }
    
    ?>
  • 相关阅读:
    linux -- 基于zookeeper搭建yarn的HA高可用集群
    Linux -- 之HDFS实现自动切换HA(全新HDFS)
    Hadoop格式化 From hu-hadoop1/192.168.11.11 to hu-hadoop2:8485 failed on connection exception: java.net.
    Directory /home/hdfs/name is in an inconsistent state: storage directory does not exist or is not a
    react学习01
    单页面应用(spa)引入百度地图(Cannot read property 'dc' of undefined)
    npm 发布包
    Vue学习-01
    echarts3.0使用总结
    webpack学习--创建一个webpack打包流程
  • 原文地址:https://www.cnblogs.com/lrjxgl/p/5529708.html
Copyright © 2020-2023  润新知