Home >Backend Development >PHP Tutorial >slowlog analysis tool v 10

slowlog analysis tool v 10

WBOY
WBOYOriginal
2016-08-08 09:18:561136browse
<?php

/**
 *  slowlog分析工具 v 1.0 
 *
 */
if( $argc < 2 ){
	
	$help.= "Help:" .PHP_EOL;
	$help.= "        slowlog.php [option]" .PHP_EOL;
	$help.= "        -f  log file path.".PHP_EOL;
	$help.= "e.g : php slowlog.php -f slow.log".PHP_EOL;
	
	exit($help);	
}

$file = $argv[2];

$logs = loadFile( $file );

$stats = array();

echo "Slow log:" . PHP_EOL;
foreach($logs as $key => $logsArr){
	
	echo "---------------------------{$key}-------------------------------" .PHP_EOL;
	
	foreach($logsArr as $k => $v){
	    echo $k . "     [num : ".count($v)."]" .PHP_EOL;	
		 foreach ($v as $phpfile){
			 
			$n = explode(":",$phpfile);
			
			$s = explode(" ",$n[0]);
			 
			if($s[2] && $s[2] != 'unknown' && $s[2] != 'dump'){
		       // echo "======> " . $s[2] . "[ Line: {$n[1]}]". PHP_EOL;
                
				$md5_key = md5($s[2].$n[1]);
				
				if(array_key_exists($md5_key,$stats[$key])){
				
					$stats[$key][$md5_key] = array(
					        'main'=>$k,
							'file'=> $s[2],
							'line'=> $n[1],
							'code'=> $s[1],
							'num'=> $stats[$key][$md5_key]['num'] + 1
					);
				} else {
					
					$stats[$key][$md5_key] = array(
					        'main'=>$k,
							'file'=> $s[2],
							'line'=> $n[1],
							'code'=> $s[1],
							'num'=>1
					);
					
				}
				
			}
		}
	}
	
}

echo PHP_EOL;
echo PHP_EOL;
echo PHP_EOL;
echo "Create json file: slowlog.json" . PHP_EOL;
file_put_contents('slowlog.json',json_encode($stats));
echo "Success!" . PHP_EOL;
echo "http://tool.lu/json/";



function loadFile( $file ){

	$str = file_get_contents($file);

	$arr = explode("\n",$str);
	$g = 0;
	$data = array();
	foreach($arr as $val){
		if( $val ){
			$data[ $g - 1][] = $val; 		   		
		} else {
			
			$g++;
		}
	} 

	$logs = array();
	foreach( $data as $key => $log){
		foreach($log as $k => $l){
		   
			$time = $data[$key][0]; 
			$phpfile = $data[$key][1];
			$p = "#\[(.*?) (.*?)\]  \[(.*?)\] pid (.*?)#";
			preg_match($p,$data[$key][0],$arr);
			$time = $arr[1]; 

			if( $data[$key][0] != $l ){
				
				if( $phpfile != $l ){
					$logs[ $time ][$phpfile][] = $l;
				}
			}
		}	
	}
	
	return $logs;
}

Copyright Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.

The above introduces the slowlog analysis tool v 10, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

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