[TOC]
* * * * *
## 1 調試源代碼(thinkphp\library\think\Debug.php)
~~~
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) - 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() - 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)
{
if ($detail) {
$files = get_included_files();
$info = [];
foreach ($files as $key => $file) {
$info[] = $file . ' ( ' . number_format(filesize($file) / 1024, 2) . ' KB )';
}
return $info;
}
return count(get_included_files());
}
~~~
~~~
public static function dump($var, $echo = true, $label = null)
{
$label = (null === $label) ? '' : rtrim($label) . ':';
ob_start();
var_dump($var);
$output = ob_get_clean();
$output = preg_replace('/\]\=\>\n(\s+)/m', '] => ', $output);
if (IS_CLI) {
$output = PHP_EOL . $label . $output . PHP_EOL;
} else {
if (!extension_loaded('xdebug')) {
$output = htmlspecialchars($output, ENT_QUOTES);
}
$output = '<pre>' . $label . $output . '</pre>';
}
if ($echo) {
echo ($output);
return null;
} else {
return $output;
}
}
~~~
## 2 文件分析
1 靜態變量
`$info:` 開始標簽和結束標簽之間的時間間隔信息
`$mem:`開始標簽和結束標簽之間的內存消耗信息
2 `public static function remark($name, $value = ''){}`
記錄時間間隔和內存使用情況
3 `public static function getRangeTime($start, $end, $dec = 6){}`
統計某個區間的時間間隔
> $start:統計開始標簽
> $end:統計結束標簽
> $dec:小數位
4 `public static function getUseTime($dec = 6){}`
統計從開始(START_TIME)到當前的時間
> $dec:小數位
5 `public static function getThroughputRate(){}`
從開始到當前時間的吞吐率情況
6 `public static function getRangeMem($start, $end, $dec = 2){}`
統計某個區間的內存使用情況
> $start:統計開始標簽
> $end:統計結束標簽
> $dec:小數位
7 `public static function getUseMem($dec = 2)`
統計從開始到當前時間的內存使用情況
> $dec:小數位
8 `public static function getMemPeak($start, $end, $dec = 2){}`
統計某個區間的內存峰值情況
> $start:統計開始標簽
> $end:統計結束標簽
> $dec:小數位
9 `public static function getFile($detail = false){}`
獲取文件加載信息
> $detail 是否包含詳細信息
10 `public static function dump($var, $echo = true, $label = null){}`
瀏覽器友好變量輸出
> $var 輸出數據信息
> $echo 返回還是輸出
> $label 標簽控制
## 3 使用方法
> >1 快捷操作
helper.php輔助函數中的G()封裝了調試接口,分析見另:(helper.php)輔助函數
dump() 函數調用Debug::dump(),實現了瀏覽器友好變量輸出
>>2 全局變量
base.php文件定義了系統開始時間和內存使用情況
START_TIME 開始運行時間
START_MEM 開始內存消耗
## 4 總結
Debug.php文件在開發調試中用來統計性能。
包括時間消耗,內存消耗。
還有環境友好變量輸出
- 更新記錄
- 概述
- 文件索引
- 函數索引
- 章節格式
- 框架流程
- 前:章節說明
- 主:(index.php)入口
- 主:(start.php)框架引導
- 主:(App.php)應用啟動
- 主:(App.php)應用調度
- C:(Controller.php)應用控制器
- M:(Model.php)數據模型
- V:(View.php)視圖對象
- 附:(App.php)應用啟動
- 附:(base.php)全局變量
- 附:(common.php)模式配置
- 附:(convention.php)全局配置
- 附:(Loader.php)自動加載器
- 附:(Build.php)自動生成
- 附:(Hook.php)監聽回調
- 附:(Route.php)全局路由
- 附:(Response.php)數據輸出
- 附:(Log.php)日志記錄
- 附:(Exception.php)異常處理
- 框架工具
- 另:(helper.php)輔助函數
- 另:(Cache.php)數據緩存
- 另:(Cookie.php)cookie操作
- 另:(Console.php)控制臺
- 另:(Debug.php)開發調試
- 另:(Error.php)錯誤處理
- 另:(Url.php)Url操作文件
- 另:(Loader.php)加載器實例化
- 另:(Input.php)數據輸入
- 另:(Lang.php)語言包管理
- 另:(ORM.php)ORM基類
- 另:(Process.php)進程管理
- 另:(Session.php)session操作
- 另:(Template.php)模板解析
- 框架驅動
- D:(\config)配置解析
- D:(\controller)控制器擴展
- D:(\model)模型擴展
- D:(\db)數據庫驅動
- D:(\view)模板解析
- D:(\template)模板標簽庫
- D:(\session)session驅動
- D:(\cache)緩存驅動
- D:(\console)控制臺
- D:(\process)進程擴展
- T:(\traits)Trait目錄
- D:(\exception)異常實現
- D:(\log)日志驅動
- 使用范例
- 服務器與框架的安裝
- 控制器操作
- 數據模型操作
- 視圖渲染控制
- MVC開發初探
- 模塊開發
- 入口文件定義全局變量
- 運行模式開發
- 框架配置
- 自動生成應用
- 事件與插件注冊
- 路由規則注冊
- 輸出控制
- 多種應用組織
- 綜合應用
- tp框架整合后臺auto架構快速開發
- 基礎原理
- php默認全局變量
- php的魔術方法
- php命名空間
- php的自動加載
- php的composer
- php的反射
- php的trait機制
- php設計模式
- php的系統時區
- php的異常錯誤
- php的輸出控制
- php的正則表達式
- php的閉包函數
- php的會話控制
- php的接口
- php的PDO
- php的字符串操作
- php的curl
- 框架心得
- 心:整體結構
- 心:配置詳解
- 心:加載器詳解
- 心:輸入輸出詳解
- 心:url路由詳解
- 心:模板詳解
- 心:模型詳解
- 心:日志詳解
- 心:緩存詳解
- 心:控制臺詳解
- 框架更新
- 4.20(驗證類,助手函數)
- 4.27(新模型Model功能)
- 5.4(新數據庫驅動)
- 7.28(自動加載)