多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php框架 > ZendFramework > Zend Framework教程-Zend_Db-數據庫操作7-Zend_Db_Profiler翻譯

Zend Framework教程-Zend_Db-數據庫操作7-Zend_Db_Profiler翻譯

來源:程序員人生   發布時間:2014-01-29 04:15:58 閱讀次數:4617次

Zend_Db_Profiler

Introduction簡介

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類是適配器的默認分析器類。

  1. $params['profiler'] =true;
  2. $db = Zend_Db::factory('PDO_MYSQL',$params);

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 或者其子類。

  1. $profiler = MyProject_Db_Profiler();
  2. $profiler->setEnabled(true);
  3. $params['profiler'] =$profiler;
  4. $db = Zend_Db::factory('PDO_MYSQL',$params);

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'  選項會被忽略。


  1. $params['profiler'] =array(
  2.     'enabled' => true,
  3.     'class'   => 'MyProject_Db_Profiler'
  4. );
  5. $db = Zend_Db::factory('PDO_MYSQL',$params);


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”文件可能包含以下數據:

  1. [main]
  2. db.profiler.class   = "MyProject_Db_Profiler"
  3. db.profiler.enabled = true


This configuration can be applied by the following PHP code:

使用這些配置的方法:

  1. $config = new Zend_Config_Ini('config.ini','main');
  2. $params['profiler'] =$config->db->profiler;
  3. $db = Zend_Db::factory('PDO_MYSQL',$params);


The 'instance' property may be used as in the following:

 'instance' 配置選項可以按照如下方法使用:

  1. $profiler = new MyProject_Db_Profiler();
  2. $profiler->setEnabled(true);
  3. $configData = array(
  4.     'instance' => $profiler
  5.     );
  6. $config = new Zend_Config($configData);
  7. $params['profiler'] =$config;
  8. $db = Zend_Db::factory('PDO_MYSQL',$params);

Using the Profiler

At any point, grab the profiler using the adapter's getProfiler() method:

在任何時候, 要使用分析器。都要通過適配器的 getProfiler()方法調用

  1. $profiler = $db->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提供信息對分析應用程序中的瓶頸非常有用的,也可以調試運行的查詢。例如,獲取最后一次運行的查詢:

  1. $query = $profiler->getLastQueryProfile();
  2.  
  3. echo$query->getQuery();

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來確定所有查詢的消耗時間總數,然后逐步找到一個運行時間最長的查詢:

  1. $totalTime    = $profiler->getTotalElapsedSecs();
  2. $queryCount   = $profiler->getTotalNumQueries();
  3. $longestTime  = 0;
  4. $longestQuery = null;
  5.  
  6. foreach ($profiler->getQueryProfiles()as $query){
  7.     if ($query->getElapsedSecs() >$longestTime){
  8.         $longestTime  = $query->getElapsedSecs();
  9.         $longestQuery = $query->getQuery();
  10.     }
  11. }
  12.  
  13. echo'Executed ' . $queryCount .' queries in ' . $totalTime .
  14.      ' seconds' . "";
  15. echo'Average query length: ' . $totalTime / $queryCount .
  16.      ' seconds' . "";
  17. echo'Queries per second: ' . $queryCount / $totalTime . "";
  18. echo'Longest query length: ' . $longestTime . "";
  19. echo"Longest query: " .$longestQuery . "";

Advanced Profiler Usage高級使用方法

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提供了相關的操作方法。

Filter by query elapsed time 通過查詢時間進行過濾

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即可。

  1. // Only profile queries that take at least 5 seconds:
  2. $profiler->setFilterElapsedSecs(5);
  3.  
  4. // Profile all queries regardless of length:
  5. $profiler->setFilterElapsedSecs(null);

Filter by query type 通過查詢類型過濾

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,你可以刪除任何現有的過濾規則。

  1. // profile only SELECT queries
  2. $profiler->setFilterQueryType(Zend_Db_Profiler::SELECT);
  3.  
  4. // profile SELECT, INSERT, and UPDATE queries
  5. $profiler->setFilterQueryType(Zend_Db_Profiler::SELECT |
  6.                               Zend_Db_Profiler::INSERT |
  7.                               Zend_Db_Profiler::UPDATE);
  8.  
  9. // profile DELETE queries
  10. $profiler->setFilterQueryType(Zend_Db_Profiler::DELETE);
  11.  
  12. // Remove all filters
  13. $profiler->setFilterQueryType(null);

Retrieve profiles by query type 設置分析查詢的類型

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()的第一個參數不僅可以是一個查詢類型常量,也可以是對查詢類型進行邏輯運算組合。如下:

  1. // Retrieve only SELECT query profiles
  2. $profiles = $profiler->getQueryProfiles(Zend_Db_Profiler::SELECT);
  3.  
  4. // Retrieve only SELECT, INSERT, and UPDATE query profiles
  5. $profiles = $profiler->getQueryProfiles(Zend_Db_Profiler::SELECT |
  6.                                         Zend_Db_Profiler::INSERT |
  7.                                         Zend_Db_Profiler::UPDATE);
  8.  
  9. // Retrieve DELETE query profiles
  10. $profiles = $profiler->getQueryProfiles(Zend_Db_Profiler::DELETE);

Specialized Profilers  特定的分析器

A Specialized Profiler is an object that inherits from Zend_Db_Profiler. Specialized Profilers treat profiling information in specific ways.

可以通過繼承Zend_Db_Profiler實現一個特定的分析器。特定的分析器可以通過特殊的方式返回分析信息

Profiling with Firebug通過Firebug進行性能分析

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性能分析

  1. // In your bootstrap file
  2.  
  3. $profiler = new Zend_Db_Profiler_Firebug('All DB Queries');
  4. $profiler->setEnabled(true);
  5.  
  6. // Attach the profiler to your db adapter
  7. $db->setProfiler($profiler);
  8.  
  9. // Dispatch your front controller
  10.  
  11. // All DB queries in your model, view and controller
  12. // files will now be profiled and sent to Firebug

Example #2 DB Profiling without Zend_Controller_Front 不采用Zend_Controller_Front做DB性能分析

  1. $profiler = new Zend_Db_Profiler_Firebug('All DB Queries');
  2. $profiler->setEnabled(true);
  3.  
  4. // Attach the profiler to your db adapter
  5. $db->setProfiler($profiler);
  6.  
  7. $request  = new Zend_Controller_Request_Http();
  8. $response = new Zend_Controller_Response_Http();
  9. $channel  = Zend_Wildfire_Channel_HttpHeaders::getInstance();
  10. $channel->setRequest($request);
  11. $channel->setResponse($response);
  12.  
  13. // Start output buffering
  14. ob_start();
  15.  
  16. // Now you can run your DB queries to be profiled
  17.  
  18. // Flush profiling data to browser
  19. $channel->flush();
  20. $response->sendHeaders();

具體的使用方法,在FirePHP 的官方網站已經講的很詳細了。可以參考完成相關配置和使用。


-----------

對譯文與原文在含義上的差異而造成的誤解不承擔任何責任。僅供參考。 



生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 99在线视频精品费观看视 | 欧美成人综合 | 福利视频一区二区 | 亚洲成人影院在线 | 亚洲精品日韩在线一区 | 日本h片无遮挡在线观看 | 爽一爽色视频 | 欧美一级日韩在线观看 | 亚洲全网成人资源在线观看 | 国产精品视频白浆免费视频 | 欧美高清xxxx性 | 波多野结衣资源在线观看 | 国产香蕉一区二区在线观看 | 欧美三级视频在线播放 | 欧美日韩一级片在线观看 | 亚洲欧美另类在线视频 | 91精品国产综合久久久久 | 欧美日韩亚洲高清不卡一区二区三区 | 日韩欧美天堂 | 天堂最新| 免费v片| 多人伦精品一区二区三区视频 | 久草综合在线 | 亚洲日本黄色 | 色视频一区二区三区 | 欧美日韩国产综合在线 | 亚洲视频福利 | 欧美黑人巨大videos免费 | 欧美色图亚洲综合 | 免费观看69xxx视频在线 | 亚洲成a人v欧美综合天 | 久久91综合国产91久久精品 | 波多野结衣久久精品免费播放 | 欧美一级aa天码毛片 | 在线观看欧洲成人免费视频 | 日本a毛片在线播放 | 亚洲全网成人资源在线观看 | 噜噜噜噜影院 | 亚洲tv精品一区二区三区 | 偷窥自拍校园春色 | 日本xxx护士21|