Home php教程 PHP源码 skymvc网站测试之mysql数据生成

skymvc网站测试之mysql数据生成

Jun 02, 2016 am 09:14 AM

跳至 [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"];
	 
	}
	
	
	
}

?>
Copy after login
Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)