ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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  var $bench = array();
15 
19  function microtimeDiff($t1, $t2)
20  {
21  $t1 = explode(" ",$t1);
22  $t2 = explode(" ",$t2);
23  $diff = $t2[0] - $t1[0] + $t2[1] - $t1[1];
24 
25  return $diff;
26  }
27 
28 
29 
33  function clearData()
34  {
35  global $ilDB;
36 
37  $q = "DELETE FROM benchmark";
38  $ilDB->manipulate($q);
39  }
40 
41 
49  function start($a_module, $a_bench)
50  {
51 return;
52  if ($this->isEnabled())
53  {
54  $this->bench[$a_module.":".$a_bench][] = microtime();
55  }
56  }
57 
58 
64  function stop($a_module, $a_bench)
65  {
66 return;
67  if ($this->isEnabled())
68  {
69  $this->bench[$a_module.":".$a_bench][count($this->bench[$a_module.":".$a_bench]) - 1]
70  = $this->microtimeDiff($this->bench[$a_module.":".$a_bench][count($this->bench[$a_module.":".$a_bench]) - 1], microtime());
71  }
72  }
73 
74 
78  function save()
79  {
80  global $ilDB, $ilUser;
81 
82  if ($this->isDbBenchEnabled() && is_object($ilUser) &&
83  $this->db_enabled_user == $ilUser->getLogin())
84  {
85  if (is_array($this->db_bench) && is_object($ilDB))
86  {
87  $this->db_bench_stop_rec = true;
88 
89  $ilDB->manipulate("DELETE FROM benchmark");
90  foreach ($this->db_bench as $b)
91  {
92  $id = $ilDB->nextId('benchmark');
93  $ilDB->insert("benchmark", array(
94  "id" => array("integer", $id),
95  "duration" => array("float", $this->microtimeDiff($b["start"], $b["stop"])),
96  "sql_stmt" => array("clob", $b["sql"])
97  ));
98  }
99  }
100  $this->enableDbBench(false);
101  }
102 
103  // log slow requests
104  //define("LOG_SLOW_REQUESTS", (float) "0.1");
105  if (defined("SLOW_REQUEST_TIME") && SLOW_REQUEST_TIME > 0)
106  {
107  $t1 = explode(" ", $GLOBALS['ilGlobalStartTime']);
108  $t2 = explode(" ", microtime());
109  $diff = $t2[0] - $t1[0] + $t2[1] - $t1[1];
110  if ($diff > SLOW_REQUEST_TIME)
111  {
112  global $ilIliasIniFile;
113 
114  $diff = round($diff, 4);
115 
116  include_once("./Services/Logging/classes/class.ilLog.php");
117  $slow_request_log = new ilLog(
118  $ilIliasIniFile->readVariable("log","slow_request_log_path"),
119  $ilIliasIniFile->readVariable("log","slow_request_log_file"),
120  CLIENT_ID);
121  $slow_request_log->write("SLOW REQUEST (".$diff."), Client:".CLIENT_ID.", GET: ".
122  str_replace("\n", " ", print_r($_GET, true)).", POST: ".
123  ilUtil::shortenText(str_replace("\n", " ", print_r($_POST, true)), 800, true));
124  }
125  }
126 
127  }
128 
129 
130  /*
131  SELECT module, benchmark, COUNT(*) AS cnt, AVG(duration) AS avg_dur FROM benchmark
132  GROUP BY module, benchmark ORDER BY module, benchmark
133  */
134 
138  function getEvaluation($a_module)
139  {
140  global $ilDB;
141 
142  $q = "SELECT COUNT(*) AS cnt, AVG(duration) AS avg_dur, benchmark,".
143  " MIN(duration) AS min_dur, MAX(duration) AS max_dur".
144  " FROM benchmark".
145  " WHERE module = ".$ilDB->quote($a_module, "text")." ".
146  " GROUP BY benchmark".
147  " ORDER BY benchmark";
148  $bench_set = $ilDB->query($q);
149  $eva = array();
150  while($bench_rec = $ilDB->fetchAssoc($bench_set))
151  {
152  $eva[] = array("benchmark" => $bench_rec["benchmark"],
153  "cnt" => $bench_rec["cnt"], "duration" => $bench_rec["avg_dur"],
154  "min" => $bench_rec["min_dur"], "max" => $bench_rec["max_dur"]);
155  }
156  return $eva;
157  }
158 
159 
164  {
165  global $ilDB;
166 
167  $q = "SELECT COUNT(*) AS cnt FROM benchmark";
168  $cnt_set = $ilDB->query($q);
169  $cnt_rec = $ilDB->fetchAssoc($cnt_set);
170 
171  return $cnt_rec["cnt"];
172  }
173 
174 
178  function getMaximumRecords()
179  {
180  global $ilias;
181 
182  return $ilias->getSetting("bench_max_records");
183  }
184 
185 
189  function setMaximumRecords($a_max)
190  {
191  global $ilias;
192 
193  $ilias->setSetting("bench_max_records", (int) $a_max);
194  }
195 
196 
200  function isEnabled()
201  {
202  global $ilSetting;
203 
204  if (!is_object($ilSetting))
205  {
206  return true;
207  }
208 
209  return (boolean) $ilSetting->get("enable_bench");
210  }
211 
212 
216  function enable($a_enable)
217  {
218  global $ilias;
219 
220  if ($a_enable)
221  {
222  $ilias->setSetting("enable_bench", 1);
223  }
224  else
225  {
226  $ilias->setSetting("enable_bench", 0);
227  }
228  }
229 
230 
235  {
236  global $ilDB;
237 
238  $q = "SELECT DISTINCT module FROM benchmark";
239  $mod_set = $ilDB->query($q);
240 
241  $modules = array();
242  while ($mod_rec = $ilDB->fetchAssoc($mod_set))
243  {
244  $modules[$mod_rec["module"]] = $mod_rec["module"];
245  }
246 
247  return $modules;
248  }
249 
250  // BEGIN WebDAV: Get measured time.
256  function getMeasuredTime($a_module, $a_bench)
257  {
258  if (isset($this->bench[$a_module.":".$a_bench]))
259  {
260  return $this->bench[$a_module.":".$a_bench][count($this->bench[$a_module.":".$a_bench]) - 1];
261  }
262  return false;
263  }
264  // END WebDAV: Get measured time.
265 
266  //
267  //
268  // NEW DB BENCHMARK IMPLEMENTATION
269  //
270  //
271 
275  function isDbBenchEnabled()
276  {
277  global $ilSetting;
278 
279  if (isset($this->db_enabled))
280  {
281  return $this->db_enabled;
282  }
283 
284  if (!is_object($ilSetting))
285  {
286  return false;
287  }
288 
289  $this->db_enabled = $ilSetting->get("enable_db_bench");
290  $this->db_enabled_user = $ilSetting->get("db_bench_user");
291  return $this->db_enabled;
292  }
293 
300  function enableDbBench($a_enable, $a_user = 0)
301  {
302  global $ilias;
303 
304  if ($a_enable)
305  {
306  $ilias->setSetting("enable_db_bench", 1);
307  if ($a_user !== 0)
308  {
309  $ilias->setSetting("db_bench_user", $a_user);
310  }
311  }
312  else
313  {
314  $ilias->setSetting("enable_db_bench", 0);
315  if ($a_user !== 0)
316  {
317  $ilias->setSetting("db_bench_user", $a_user);
318  }
319  }
320  }
321 
329  function startDbBench($a_sql)
330  {
331  global $ilUser;
332 
333  if ($this->isDbBenchEnabled() && is_object($ilUser) &&
334  $this->db_enabled_user == $ilUser->getLogin() &&
335  !$this->db_bench_stop_rec)
336  {
337  $this->start = microtime();
338  $this->sql = $a_sql;
339  }
340  }
341 
342 
348  function stopDbBench()
349  {
350  global $ilUser;
351 
352  if ($this->isDbBenchEnabled() && is_object($ilUser) &&
353  $this->db_enabled_user == $ilUser->getLogin() &&
354  !$this->db_bench_stop_rec)
355  {
356  $this->db_bench[] =
357  array("start" => $this->start, "stop" => microtime(),
358  "sql" => $this->sql);
359  }
360  }
361 
368  function getDbBenchRecords()
369  {
370  global $ilDB;
371 
372  $set = $ilDB->query("SELECT * FROM benchmark");
373  $b = array();
374  while ($rec = $ilDB->fetchAssoc($set))
375  {
376  $b[] = array("sql" => $rec["sql_stmt"],
377  "time" => $rec["duration"]);
378  }
379  return $b;
380  }
381 
382 }
383 
384 ?>
microtimeDiff($t1, $t2)
getMeasuredModules()
get all current measured modules
enable($a_enable)
enable benchmarking
$_GET["client_id"]
stop($a_module, $a_bench)
stop measurement
$GLOBALS['loaded']
Global hash that tracks already loaded includes.
static shortenText($a_str, $a_len, $a_dots=false, $a_next_blank=false, $a_keep_extension=false)
shorten a string to given length.
logging
Definition: class.ilLog.php:18
isDbBenchEnabled()
Check wether benchmarking is enabled or not.
startDbBench($a_sql)
start measurement
getCurrentRecordNumber()
get current number of benchmark records
isEnabled()
check wether benchmarking is enabled or not
write($a_msg, $a_log_level=NULL)
logging
getEvaluation($a_module)
get performance evaluation data
$ilUser
Definition: imgupload.php:18
clearData()
delete all measurement data
getMaximumRecords()
get maximum number of benchmark records
getDbBenchRecords()
Get db benchmark records.
Create styles array
The data for the language used.
stopDbBench()
stop measurement
setMaximumRecords($a_max)
set maximum number of benchmark records
global $ilSetting
Definition: privfeed.php:17
global $ilDB
performance measurement class
$ilIliasIniFile
start($a_module, $a_bench)
start measurement
defined( 'APPLICATION_ENV')||define( 'APPLICATION_ENV'
Definition: bootstrap.php:27
$_POST["username"]
save()
save all measurements
getMeasuredTime($a_module, $a_bench)
Get measurement.
enableDbBench($a_enable, $a_user=0)
Enable DB benchmarking.