123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- <?php
- namespace think;
- use think\exception\ClassNotFoundException;
- use think\response\Redirect;
- class Debug
- {
-
- protected static $info = [];
-
- protected static $mem = [];
-
- public static function remark($name, $value = '')
- {
- self::$info[$name] = is_float($value) ? $value : microtime(true);
- if ('time' != $value) {
- self::$mem['mem'][$name] = is_float($value) ? $value : memory_get_usage();
- self::$mem['peak'][$name] = memory_get_peak_usage();
- }
- }
-
- public static function getRangeTime($start, $end, $dec = 6)
- {
- if (!isset(self::$info[$end])) {
- self::$info[$end] = microtime(true);
- }
- return number_format((self::$info[$end] - self::$info[$start]), $dec);
- }
-
- public static function getUseTime($dec = 6)
- {
- return number_format((microtime(true) - THINK_START_TIME), $dec);
- }
-
- public static function getThroughputRate()
- {
- return number_format(1 / self::getUseTime(), 2) . 'req/s';
- }
-
- public static function getRangeMem($start, $end, $dec = 2)
- {
- if (!isset(self::$mem['mem'][$end])) {
- self::$mem['mem'][$end] = memory_get_usage();
- }
- $size = self::$mem['mem'][$end] - self::$mem['mem'][$start];
- $a = ['B', 'KB', 'MB', 'GB', 'TB'];
- $pos = 0;
- while ($size >= 1024) {
- $size /= 1024;
- $pos++;
- }
- return round($size, $dec) . " " . $a[$pos];
- }
-
- public static function getUseMem($dec = 2)
- {
- $size = memory_get_usage() - THINK_START_MEM;
- $a = ['B', 'KB', 'MB', 'GB', 'TB'];
- $pos = 0;
- while ($size >= 1024) {
- $size /= 1024;
- $pos++;
- }
- return round($size, $dec) . " " . $a[$pos];
- }
-
- public static function getMemPeak($start, $end, $dec = 2)
- {
- if (!isset(self::$mem['peak'][$end])) {
- self::$mem['peak'][$end] = memory_get_peak_usage();
- }
- $size = self::$mem['peak'][$end] - self::$mem['peak'][$start];
- $a = ['B', 'KB', 'MB', 'GB', 'TB'];
- $pos = 0;
- while ($size >= 1024) {
- $size /= 1024;
- $pos++;
- }
- return round($size, $dec) . " " . $a[$pos];
- }
-
- public static function getFile($detail = false)
- {
- $files = get_included_files();
- if ($detail) {
- $info = [];
- foreach ($files as $file) {
- $info[] = $file . ' ( ' . number_format(filesize($file) / 1024, 2) . ' KB )';
- }
- return $info;
- }
- return count($files);
- }
-
- public static function dump($var, $echo = true, $label = null, $flags = ENT_SUBSTITUTE)
- {
- $label = (null === $label) ? '' : rtrim($label) . ':';
- ob_start();
- var_dump($var);
- $output = preg_replace('/\]\=\>\n(\s+)/m', '] => ', ob_get_clean());
- if (IS_CLI) {
- $output = PHP_EOL . $label . $output . PHP_EOL;
- } else {
- if (!extension_loaded('xdebug')) {
- $output = htmlspecialchars($output, $flags);
- }
- $output = '<pre>' . $label . $output . '</pre>';
- }
- if ($echo) {
- echo($output);
- return;
- }
- return $output;
- }
-
- public static function inject(Response $response, &$content)
- {
- $config = Config::get('trace');
- $type = isset($config['type']) ? $config['type'] : 'Html';
- $class = false !== strpos($type, '\\') ? $type : '\\think\\debug\\' . ucwords($type);
- unset($config['type']);
- if (!class_exists($class)) {
- throw new ClassNotFoundException('class not exists:' . $class, $class);
- }
-
- $trace = new $class($config);
- if ($response instanceof Redirect) {
-
- } else {
- $output = $trace->output($response, Log::getLog());
- if (is_string($output)) {
-
- $pos = strripos($content, '</body>');
- if (false !== $pos) {
- $content = substr($content, 0, $pos) . $output . substr($content, $pos);
- } else {
- $content = $content . $output;
- }
- }
- }
- }
- }
|