Codeigniter-benchmarking
提供:Dev Guides
CodeIgniter-ベンチマーク
ベンチマークポイントの設定
一連の行またはメモリ使用量の実行にかかる時間を測定する場合は、CodeIgniterのベンチマークポイントを使用して計算できます。 CodeIgniterには、この目的のための別の「 Benchmarking 」クラスがあります。
このクラスは自動的にロードされます。ロードする必要はありません。 コントローラ、ビュー、モデルクラスのどこでも使用できます。 必要なのは、開始点と終了点をマークし、これらのマークされた2つの点の間で* elapsed_time()*関数を実行することです。以下に示すように、そのコードの実行にかかった時間を取得できます。
<?php
$this->benchmark->mark('code_start');
//Some code happens here
$this->benchmark->mark('code_end');
echo $this->benchmark->elapsed_time('code_start', 'code_end');
?>
メモリ使用量を表示するには、次のコードに示すように、関数* memory_usage()*を使用します。
<?php
echo $this->benchmark->memory_usage();
?>
例
*Profiler_controller.php* というコントローラーを作成し、 *application/controller/Profiler_controller.php* に保存します
<?php
class Profiler_controller extends CI_Controller {
public function index() {
//enable profiler
$this->output->enable_profiler(TRUE);
$this->load->view('test');
}
public function disable() {
//disable profiler
$this->output->enable_profiler(FALSE);
$this->load->view('test');
}
}
?>
*test.php* という名前のビューファイルを作成し、 *application/views/test.php* に保存します。
<!DOCTYPE html>
<html lang = "en">
<head>
<meta charset = "utf-8">
<title>CodeIgniter View Example</title>
</head>
<body>
CodeIgniter View Example
</body>
</html>
*application/config/routes.php* のroutes.phpファイルを変更して、上記のコントローラーのルートを追加し、ファイルの最後に次の行を追加します。
$route['profiler'] = "Profiler_controller";
$route['profiler/disable'] = "Profiler_controller/disable"
その後、ブラウザのアドレスバーに次のURLを入力して例を実行できます。
http://yoursite.com/index.php/profiler
上記のURLはプロファイラーを有効にし、次のスクリーンショットに示すように出力を生成します。
プロファイリングを無効にするには、次のURLを実行します。
http://yoursite.com/index.php/profiler/disable