Home  >  Article  >  php教程  >  skymvc网站测试之mysql数据生成

skymvc网站测试之mysql数据生成

WBOY
WBOYOriginal
2016-06-02 09:14:142281browse
跳至 [1] [全屏预览]
<?php
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"=>500000,
			"test"=>300000,
			"test2"=>300000,
		);
		if(!isset($cf[$table])){
			return 300000;
		}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"];
	 
	}
	
	
	
}

?>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn