ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilBenchmark.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
4
13{
14
18 protected $db_bench;
22 protected $start;
26 protected $sql;
35
36
40 public function __construct()
41 {
42 }
43
44 public $bench = array();
45
49 public function microtimeDiff($t1, $t2)
50 {
51 $t1 = explode(" ", $t1);
52 $t2 = explode(" ", $t2);
53 $diff = $t2[0] - $t1[0] + $t2[1] - $t1[1];
54
55 return $diff;
56 }
57
58
59
63 public function clearData()
64 {
65 global $DIC;
66 $ilDB = $DIC->database();
67
68 $q = "DELETE FROM benchmark";
69 $ilDB->manipulate($q);
70 }
71
72
81 public function start($a_module, $a_bench)
82 {
83 }
84
85
92 public function stop($a_module, $a_bench)
93 {
94 }
95
96
100 public function save()
101 {
102 global $DIC;
103 $ilDB = $DIC->database();
104 $ilUser = $DIC->user();
105 if ($this->isDbBenchEnabled() && is_object($ilUser) &&
106 $this->db_enabled_user == $ilUser->getLogin()) {
107 if (is_array($this->db_bench) && is_object($ilDB)) {
108 $this->db_bench_stop_rec = true;
109
110 $ilDB->manipulate("DELETE FROM benchmark");
111 foreach ($this->db_bench as $b) {
112 $id = $ilDB->nextId('benchmark');
113 $ilDB->insert("benchmark", array(
114 "id" => array("integer", $id),
115 "duration" => array("float", $this->microtimeDiff($b["start"], $b["stop"])),
116 "sql_stmt" => array("clob", $b["sql"])
117 ));
118 }
119 }
120 $this->enableDbBench(false);
121 }
122
123 // log slow requests
124 //define("LOG_SLOW_REQUESTS", (float) "0.1");
125 if (defined("SLOW_REQUEST_TIME") && SLOW_REQUEST_TIME > 0) {
126 $t1 = explode(" ", $GLOBALS['ilGlobalStartTime']);
127 $t2 = explode(" ", microtime());
128 $diff = $t2[0] - $t1[0] + $t2[1] - $t1[1];
129 if ($diff > SLOW_REQUEST_TIME) {
130 $ilIliasIniFile = $DIC["ilIliasIniFile"];
131
132 $diff = round($diff, 4);
133
134 include_once("./Services/Logging/classes/class.ilLog.php");
135 $slow_request_log = new ilLog(
136 $ilIliasIniFile->readVariable("log", "slow_request_log_path"),
137 $ilIliasIniFile->readVariable("log", "slow_request_log_file"),
138 CLIENT_ID
139 );
140 $slow_request_log->write("SLOW REQUEST (" . $diff . "), Client:" . CLIENT_ID . ", GET: " .
141 str_replace("\n", " ", print_r($_GET, true)) . ", POST: " .
142 ilUtil::shortenText(str_replace("\n", " ", print_r($_POST, true)), 800, true));
143 }
144 }
145 }
146
147
148 /*
149 SELECT module, benchmark, COUNT(*) AS cnt, AVG(duration) AS avg_dur FROM benchmark
150 GROUP BY module, benchmark ORDER BY module, benchmark
151 */
152
156 public function getEvaluation($a_module)
157 {
158 $ilDB = $this->db;
159
160 $q = "SELECT COUNT(*) AS cnt, AVG(duration) AS avg_dur, benchmark," .
161 " MIN(duration) AS min_dur, MAX(duration) AS max_dur" .
162 " FROM benchmark" .
163 " WHERE module = " . $ilDB->quote($a_module, "text") . " " .
164 " GROUP BY benchmark" .
165 " ORDER BY benchmark";
166 $bench_set = $ilDB->query($q);
167 $eva = array();
168 while ($bench_rec = $ilDB->fetchAssoc($bench_set)) {
169 $eva[] = array("benchmark" => $bench_rec["benchmark"],
170 "cnt" => $bench_rec["cnt"], "duration" => $bench_rec["avg_dur"],
171 "min" => $bench_rec["min_dur"], "max" => $bench_rec["max_dur"]);
172 }
173 return $eva;
174 }
175
176
180 public function getCurrentRecordNumber()
181 {
182 global $DIC;
183 $ilDB = $DIC->database();
184
185 $q = "SELECT COUNT(*) AS cnt FROM benchmark";
186 $cnt_set = $ilDB->query($q);
187 $cnt_rec = $ilDB->fetchAssoc($cnt_set);
188
189 return $cnt_rec["cnt"];
190 }
191
192
196 public function getMaximumRecords()
197 {
198 global $DIC;
199 $ilSetting = $DIC->settings();
200
201 return $ilSetting->get("bench_max_records");
202 }
203
204
208 public function setMaximumRecords($a_max)
209 {
210 global $DIC;
211 $ilSetting = $DIC->settings();
212
213 return $ilSetting->get("bench_max_records", (int) $a_max);
214 }
215
216
220 public function isEnabled()
221 {
222 global $DIC;
223 $ilSetting = $DIC->settings();
224
225
226 if (!is_object($ilSetting)) {
227 return true;
228 }
229
230 return (boolean) $ilSetting->get("enable_bench");
231 }
232
233
237 public function enable($a_enable)
238 {
239 global $DIC;
240 $ilSetting = $DIC->settings();
241
242
243 if ($a_enable) {
244 $ilSetting->get("enable_bench", 1);
245 } else {
246 $ilSetting->get("enable_bench", 0);
247 }
248 }
249
250
254 public function getMeasuredModules()
255 {
256 global $DIC;
257 $ilDB = $DIC->database();
258
259
260 $q = "SELECT DISTINCT module FROM benchmark";
261 $mod_set = $ilDB->query($q);
262
263 $modules = array();
264 while ($mod_rec = $ilDB->fetchAssoc($mod_set)) {
265 $modules[$mod_rec["module"]] = $mod_rec["module"];
266 }
267
268 return $modules;
269 }
270
271 // BEGIN WebDAV: Get measured time.
277 public function getMeasuredTime($a_module, $a_bench)
278 {
279 if (isset($this->bench[$a_module . ":" . $a_bench])) {
280 return $this->bench[$a_module . ":" . $a_bench][count($this->bench[$a_module . ":" . $a_bench]) - 1];
281 }
282 return false;
283 }
284 // END WebDAV: Get measured time.
285
286 //
287 //
288 // NEW DB BENCHMARK IMPLEMENTATION
289 //
290 //
291
295 public function isDbBenchEnabled()
296 {
297 global $DIC;
298 $ilSetting = $DIC->settings();
299
300
301 if (isset($this->db_enabled)) {
302 return $this->db_enabled;
303 }
304
305 if (!is_object($ilSetting)) {
306 return false;
307 }
308
309 $this->db_enabled = $ilSetting->get("enable_db_bench");
310 $this->db_enabled_user = $ilSetting->get("db_bench_user");
311 return $this->db_enabled;
312 }
313
320 public function enableDbBench($a_enable, $a_user = 0)
321 {
322 global $DIC;
323 $ilSetting = $DIC->settings();
324
325
326 if ($a_enable) {
327 $ilSetting->set("enable_db_bench", 1);
328 if ($a_user !== 0) {
329 $ilSetting->set("db_bench_user", $a_user);
330 }
331 } else {
332 $ilSetting->set("enable_db_bench", 0);
333 if ($a_user !== 0) {
334 $ilSetting->set("db_bench_user", $a_user);
335 }
336 }
337 }
338
339
347 public function startDbBench($a_sql)
348 {
349 global $DIC;
350
351 try {
352 $ilUser = $DIC->user();
353 } catch (InvalidArgumentException $e) {
354 return false;
355 }
356
357 if ($this->isDbBenchEnabled() && is_object($ilUser)
358 && $this->db_enabled_user == $ilUser->getLogin()
359 && !$this->db_bench_stop_rec) {
360 $this->start = microtime();
361 $this->sql = $a_sql;
362 }
363 }
364
365
369 public function stopDbBench()
370 {
371 global $DIC;
372
373 try {
374 $ilUser = $DIC->user();
375 } catch (InvalidArgumentException $e) {
376 return false;
377 }
378
379 if ($this->isDbBenchEnabled() && is_object($ilUser)
380 && $this->db_enabled_user == $ilUser->getLogin()
381 && !$this->db_bench_stop_rec) {
382 $this->db_bench[] = array(
383 "start" => $this->start,
384 "stop" => microtime(),
385 "sql" => $this->sql,
386 );
387
388 return true;
389 }
390
391 return false;
392 }
393
400 public function getDbBenchRecords()
401 {
402 global $DIC;
403 $ilDB = $DIC->database();
404
405 $set = $ilDB->query("SELECT * FROM benchmark");
406 $b = array();
407 while ($rec = $ilDB->fetchAssoc($set)) {
408 $b[] = array("sql" => $rec["sql_stmt"],
409 "time" => $rec["duration"]);
410 }
411 return $b;
412 }
413}
$_GET["client_id"]
$_POST["username"]
An exception for terminatinating execution or to throw for unit testing.
performance measurement class
start($a_module, $a_bench)
start measurement
getMeasuredTime($a_module, $a_bench)
Get measurement.
getMeasuredModules()
get all current measured modules
enableDbBench($a_enable, $a_user=0)
Enable DB benchmarking.
setMaximumRecords($a_max)
set maximum number of benchmark records
stop($a_module, $a_bench)
stop measurement
getMaximumRecords()
get maximum number of benchmark records
getCurrentRecordNumber()
get current number of benchmark records
getDbBenchRecords()
Get db benchmark records.
microtimeDiff($t1, $t2)
isDbBenchEnabled()
Check wether benchmarking is enabled or not.
__construct()
Constructor.
enable($a_enable)
enable benchmarking
getEvaluation($a_module)
get performance evaluation data
startDbBench($a_sql)
start measurement
save()
save all measurements
isEnabled()
check wether benchmarking is enabled or not
clearData()
delete all measurement data
logging
Definition: class.ilLog.php:19
static shortenText( $a_str, $a_len, $a_dots=false, $a_next_blank=false, $a_keep_extension=false)
shorten a string to given length.
if(!array_key_exists('StateId', $_REQUEST)) $id
$GLOBALS['loaded']
Global hash that tracks already loaded includes.
defined( 'APPLICATION_ENV')||define( 'APPLICATION_ENV'
Definition: bootstrap.php:27
global $ilSetting
Definition: privfeed.php:17
global $DIC
Definition: saml.php:7
global $ilDB
$ilIliasIniFile
$ilUser
Definition: imgupload.php:18