Zend_Db_Profiler can be enabled to allow profiling of queries.
Profiles include the queries processed by the adapter as well as elapsed time to run the queries, allowing inspection of the queries that have been performed without needing to add extra debugging code to classes.
Advanced usage also allows the developer to filter which queries are profiled.
Enable the profiler by either passing a directive to the adapter constructor, or by asking the adapter to enable it later.
Zend_Db_Profiler可以啟用查詢分析功能。分析內容包括適配器處理的查詢和查詢的運行消耗時間。通過它,無需添加額外的調試代碼,就可以檢查已執行的查詢。
$params = array( 'host' => '127.0.0.1', 'username' => 'webuser', 'password' => 'xxxxxxxx', 'dbname' => 'test' 'profiler' => true // turn on profiler // set to false to disable (disabled by default)); $db = Zend_Db::factory('PDO_MYSQL', $params); // turn off profiler:$db->getProfiler()->setEnabled(false); // turn on profiler:$db->getProfiler()->setEnabled(true);
The value of the 'profiler' option is flexible. It is interpreted differently depending on its type. Most often, you should use a simple boolean value, but other types enable you to customize the profiler behavior.
A boolean argument sets the profiler to enabled if it is a TRUE value, or disabled if FALSE. The profiler class is the adapter's default profiler class,Zend_Db_Profiler.
啟用分析功能,配置方法是非常靈活的。大多數情況下,你只需使用一個簡單的布爾值,但允許使用其他自定義類型來啟用分析功能。
如果參數設置為true表示啟用分析功能。如果要禁用只需設置為false即可。Zend_Db_Profiler類是適配器的默認分析器類。
An instance of a profiler object makes the adapter use that object. The object type must beZend_Db_Profiler or a subclass thereof. Enabling the profiler is done separately.
適配器使用分析器對象時。分析器對象的類型必須是 Zend_Db_Profiler 或者其子類。
The argument can be an associative array containing any or all of the keys 'enabled', 'instance', and 'class'.
The 'enabled' and 'instance' keys correspond to the boolean and instance types documented above.
The 'class' key is used to name a class to use for a custom profiler. The class must beZend_Db_Profiler or a subclass.
The class is instantiated with no constructor arguments.
The 'class' option is ignored when the 'instance' option is supplied.
配置參數可以是一個關聯數組。其鍵名為'enabled', 'instance', 和'class'.
'enabled' 和 'instance' 鍵的值是一個boolean值和profiler對象的實例
'class' 指定自定義的分析器的類名。類必須是Zend_Db_Profiler或Zend_Db_Profiler的子類子類。實例化類時,無須傳遞構造參數。
使用'instance'時, 'class' 選項會被忽略。
Finally, the argument can be an object of type Zend_Config containing properties, which are treated as the array keys described above.
For example, a file "config.ini" might contain the following data:
上面的配置參數也可以使用Zend_Config實現。數組的key可以作為配置選項的名稱。例如,一個“config.ini”文件可能包含以下數據:
This configuration can be applied by the following PHP code:
使用這些配置的方法:
The 'instance' property may be used as in the following:
'instance' 配置選項可以按照如下方法使用:
At any point, grab the profiler using the adapter's getProfiler() method:
在任何時候, 要使用分析器。都要通過適配器的 getProfiler()方法調用
This returns a Zend_Db_Profiler object instance. With that instance, the developer can examine your queries using a variety of methods:
得到一個Zend_Db_Profiler 對象實例后,開發人員可以使用如下方法進行查詢語句的分析工作:
getTotalNumQueries() returns the total number of queries that have been profiled. 返回查詢語句的個數
getTotalElapsedSecs() returns the total number of seconds elapsed for all profiled queries. 返回所有查詢語句消耗的時間
getQueryProfiles() returns an array of all query profiles. 返回所有查詢語句
getLastQueryProfile() returns the last (most recent) query profile, regardless of whether or not the query has finished (if it hasn't, the end time will beNULL) 無論查詢是否完成,都會返回最后一個(最近一個)查詢信息,(如果沒有的話,返回NULL)
clear() clears any past query profiles from the stack. 從堆棧中清除以往任何查詢信息。
The return value of getLastQueryProfile() and the individual elements ofgetQueryProfiles() are Zend_Db_Profiler_Query objects, which provide the ability to inspect the individual queries themselves:
getLastQueryProfile() ,getQueryProfiles() 的返回值 是 Zend_Db_Profiler_Query 對象。還提供檢查個別的查詢的方法:
getQuery() returns the SQL text of the query. The SQL text of a prepared statement with parameters is the text at the time the query was prepared, so it contains parameter placeholders, not the values used when the statement is executed.
返回SQL語句。 SQL語句是預處理statement 的語句,所以它包含參數占位符,而不是具體執行的語句。
getQueryParams() returns an array of parameter values used when executing a prepared query. This includes both bound parameters and arguments to the statement'sexecute() method. The keys of the array are the positional (1-based) or named (string) parameter indices.
返回一個數組,包含預處理查詢執行時使用的參數值。 包括綁定參數和statement的execute() 方法的參數。數組的key是位置(索引從1開始)或者名稱(字符串)參數索引
getElapsedSecs() returns the number of seconds the query ran. 返回查詢運行的秒數。
The information Zend_Db_Profiler provides is useful for profiling bottlenecks in applications, and for debugging queries that have been run.
For instance, to see the exact query that was last run:
Zend_Db_Profiler提供信息對分析應用程序中的瓶頸非常有用的,也可以調試運行的查詢。例如,獲取最后一次運行的查詢:
Perhaps a page is generating slowly;
use the profiler to determine first the total number of seconds of all queries, and then step through the queries to find the one that ran longest:
如果一個頁面加載運行緩慢。可以使用Profiler來確定所有查詢的消耗時間總數,然后逐步找到一個運行時間最長的查詢:
In addition to query inspection, the profiler also allows the developer to filter which queries get profiled.
The following methods operate on a Zend_Db_Profiler instance:
除了可以對查詢的檢查分析,分析器也使開發人員能夠分析過濾查詢。Zend_Db_Profiler提供了相關的操作方法。
setFilterElapsedSecs() allows the developer to set a minimum query time before a query is profiled.
To remove the filter, pass the method a NULL value.
setFilterElapsedSecs()允許開發人員可以設置一個最小的查詢時間,來過濾查詢。刪除過濾器,只需要設置為NULL即可。
setFilterQueryType() allows the developer to set which types of queries should be profiled; to profile multiple types, logical OR them. Query types are defined as the followingZend_Db_Profiler constants:
通過使用setFilterQueryType() 方法,開發人員可以根據類型來過濾查詢。分析器Zend_Db_Profiler類中定義了多種查詢類型常量。如下:
Zend_Db_Profiler::CONNECT: connection operations, or selecting a database.鏈接操作,或者選擇數據庫
Zend_Db_Profiler::QUERY: general database queries that do not match other types. 沒有具體匹配類型的查詢
Zend_Db_Profiler::INSERT: any query that adds new data to the database, generallySQL INSERT.INSERT 插入語句
Zend_Db_Profiler::UPDATE: any query that updates existing data, usuallySQL UPDATE. UPDATE更新修改語句
Zend_Db_Profiler::DELETE: any query that deletes existing data, usuallySQL DELETE. DELETE刪除語句
Zend_Db_Profiler::SELECT: any query that retrieves existing data, usuallySQL SELECT. SELECT語句
Zend_Db_Profiler::TRANSACTION: any transactional operation, such as start transaction, commit, or rollback. 事務處理語句例如commit和rollback
As with setFilterElapsedSecs(), you can remove any existing filters by passingNULL as the sole argument.
使用setFilterElapsedSecs(),通過設置參數為NULL,你可以刪除任何現有的過濾規則。
Using setFilterQueryType() can cut down on the profiles generated.
However, sometimes it can be more useful to keep all profiles, but view only those you need at a given moment.
Another feature of getQueryProfiles() is that it can do this filtering on-the-fly, by passing a query type (or logical combination of query types) as its first argument; seethis section for a list of the query type constants.
使用 setFilterQueryType() 可以過濾查詢。但是,有時需要多種過濾類型同時起作用。 getQueryProfiles()的第一個參數不僅可以是一個查詢類型常量,也可以是對查詢類型進行邏輯運算組合。如下:
A Specialized Profiler is an object that inherits from Zend_Db_Profiler. Specialized Profilers treat profiling information in specific ways.
可以通過繼承Zend_Db_Profiler實現一個特定的分析器。特定的分析器可以通過特殊的方式返回分析信息
Zend_Db_Profiler_Firebug sends profiling infomation to the? Firebug ? Console. All data is sent via the Zend_Wildfire_Channel_HttpHeaders component which usesHTTP headers to ensure the page content is not disturbed.
Debugging AJAX requests that require cleanJSON and XML responses is possible with this approach.
通過Zend_Db_Profiler_Firebug 將性能分析信息輸出到Firebug 的Console控制臺。通過Zend_Wildfire_Channel_HttpHeaders的組件設置HTTP頭發送所有的數據,以確保頁面內容不被干擾。通過這種方法,當用戶調試AJAX請求時,可以保證響應JSON和XML不會摻雜多余的信息。
Requirements:要求
Firefox Browser ideally version 3 but version 2 is also supported. 較新的火狐瀏覽器
Firebug Firefox Extension which you can download from ? https://addons.mozilla.org/en-US/firefox/addon/1843. 安裝Firebug
FirePHP Firefox Extension which you can download from ? https://addons.mozilla.org/en-US/firefox/addon/6149.安裝FirePHP
Example #1 DB Profiling with Zend_Controller_Front 通過Zend_Controller_Front做DB性能分析
Example #2 DB Profiling without Zend_Controller_Front 不采用Zend_Controller_Front做DB性能分析
-----------
對譯文與原文在含義上的差異而造成的誤解不承擔任何責任。僅供參考。