ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
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 ilBenchmark()
20  {
21  }
22 
23 
27  function microtimeDiff($t1, $t2)
28  {
29  $t1 = explode(" ",$t1);
30  $t2 = explode(" ",$t2);
31  $diff = $t2[0] - $t1[0] + $t2[1] - $t1[1];
32 
33  return $diff;
34  }
35 
36 
37 
41  function clearData()
42  {
43  global $ilDB;
44 
45  $q = "DELETE FROM benchmark";
46  $ilDB->manipulate($q);
47  }
48 
49 
57  function start($a_module, $a_bench)
58  {
59 return;
60  if ($this->isEnabled())
61  {
62  $this->bench[$a_module.":".$a_bench][] = microtime();
63  }
64  }
65 
66 
72  function stop($a_module, $a_bench)
73  {
74 return;
75  if ($this->isEnabled())
76  {
77  $this->bench[$a_module.":".$a_bench][count($this->bench[$a_module.":".$a_bench]) - 1]
78  = $this->microtimeDiff($this->bench[$a_module.":".$a_bench][count($this->bench[$a_module.":".$a_bench]) - 1], microtime());
79  }
80  }
81 
82 
86  function save()
87  {
88  global $ilDB, $ilUser;
89 
90  if ($this->isDbBenchEnabled() && is_object($ilUser) &&
91  $this->db_enabled_user == $ilUser->getLogin())
92  {
93  if (is_array($this->db_bench) && is_object($ilDB))
94  {
95  $this->db_bench_stop_rec = true;
96 
97  $ilDB->manipulate("DELETE FROM benchmark");
98  foreach ($this->db_bench as $b)
99  {
100  $ilDB->insert("benchmark", array(
101  "duration" => array("float", $this->microtimeDiff($b["start"], $b["stop"])),
102  "sql_stmt" => array("clob", $b["sql"])
103  ));
104  }
105  }
106  $this->enableDbBench(false);
107  }
108 
109  // log slow requests
110  //define("LOG_SLOW_REQUESTS", (float) "0.1");
111  if (defined("SLOW_REQUEST_TIME") && SLOW_REQUEST_TIME > 0)
112  {
113  $t1 = explode(" ", $GLOBALS['ilGlobalStartTime']);
114  $t2 = explode(" ", microtime());
115  $diff = $t2[0] - $t1[0] + $t2[1] - $t1[1];
116  if ($diff > SLOW_REQUEST_TIME)
117  {
118  global $ilIliasIniFile;
119 
120  $diff = round($diff, 4);
121 
122  include_once("./Services/Logging/classes/class.ilLog.php");
123  $slow_request_log = new ilLog(
124  $ilIliasIniFile->readVariable("log","slow_request_log_path"),
125  $ilIliasIniFile->readVariable("log","slow_request_log_file"),
126  CLIENT_ID);
127  $slow_request_log->write("SLOW REQUEST (".$diff."), Client:".CLIENT_ID.", GET: ".
128  str_replace("\n", " ", print_r($_GET, true)).", POST: ".
129  ilUtil::shortenText(str_replace("\n", " ", print_r($_POST, true)), 800, true));
130  }
131  }
132 
133  }
134 
135 
136  /*
137  SELECT module, benchmark, COUNT(*) AS cnt, AVG(duration) AS avg_dur FROM benchmark
138  GROUP BY module, benchmark ORDER BY module, benchmark
139  */
140 
144  function getEvaluation($a_module)
145  {
146  global $ilDB;
147 
148  $q = "SELECT COUNT(*) AS cnt, AVG(duration) AS avg_dur, benchmark,".
149  " MIN(duration) AS min_dur, MAX(duration) AS max_dur".
150  " FROM benchmark".
151  " WHERE module = ".$ilDB->quote($a_module, "text")." ".
152  " GROUP BY benchmark".
153  " ORDER BY benchmark";
154  $bench_set = $ilDB->query($q);
155  $eva = array();
156  while($bench_rec = $ilDB->fetchAssoc($bench_set))
157  {
158  $eva[] = array("benchmark" => $bench_rec["benchmark"],
159  "cnt" => $bench_rec["cnt"], "duration" => $bench_rec["avg_dur"],
160  "min" => $bench_rec["min_dur"], "max" => $bench_rec["max_dur"]);
161  }
162  return $eva;
163  }
164 
165 
170  {
171  global $ilDB;
172 
173  $q = "SELECT COUNT(*) AS cnt FROM benchmark";
174  $cnt_set = $ilDB->query($q);
175  $cnt_rec = $ilDB->fetchAssoc($cnt_set);
176 
177  return $cnt_rec["cnt"];
178  }
179 
180 
184  function getMaximumRecords()
185  {
186  global $ilias;
187 
188  return $ilias->getSetting("bench_max_records");
189  }
190 
191 
195  function setMaximumRecords($a_max)
196  {
197  global $ilias;
198 
199  $ilias->setSetting("bench_max_records", (int) $a_max);
200  }
201 
202 
206  function isEnabled()
207  {
208  global $ilSetting;
209 
210  if (!is_object($ilSetting))
211  {
212  return true;
213  }
214 
215  return (boolean) $ilSetting->get("enable_bench");
216  }
217 
218 
222  function enable($a_enable)
223  {
224  global $ilias;
225 
226  if ($a_enable)
227  {
228  $ilias->setSetting("enable_bench", 1);
229  }
230  else
231  {
232  $ilias->setSetting("enable_bench", 0);
233  }
234  }
235 
236 
241  {
242  global $ilDB;
243 
244  $q = "SELECT DISTINCT module FROM benchmark";
245  $mod_set = $ilDB->query($q);
246 
247  $modules = array();
248  while ($mod_rec = $ilDB->fetchAssoc($mod_set))
249  {
250  $modules[$mod_rec["module"]] = $mod_rec["module"];
251  }
252 
253  return $modules;
254  }
255 
256  // BEGIN WebDAV: Get measured time.
262  function getMeasuredTime($a_module, $a_bench)
263  {
264  if (isset($this->bench[$a_module.":".$a_bench]))
265  {
266  return $this->bench[$a_module.":".$a_bench][count($this->bench[$a_module.":".$a_bench]) - 1];
267  }
268  return false;
269  }
270  // END WebDAV: Get measured time.
271 
272  //
273  //
274  // NEW DB BENCHMARK IMPLEMENTATION
275  //
276  //
277 
281  function isDbBenchEnabled()
282  {
283  global $ilSetting;
284 
285  if (isset($this->db_enabled))
286  {
287  return $this->db_enabled;
288  }
289 
290  if (!is_object($ilSetting))
291  {
292  return false;
293  }
294 
295  $this->db_enabled = $ilSetting->get("enable_db_bench");
296  $this->db_enabled_user = $ilSetting->get("db_bench_user");
297  return $this->db_enabled;
298  }
299 
306  function enableDbBench($a_enable, $a_user = 0)
307  {
308  global $ilias;
309 
310  if ($a_enable)
311  {
312  $ilias->setSetting("enable_db_bench", 1);
313  if ($a_user !== 0)
314  {
315  $ilias->setSetting("db_bench_user", $a_user);
316  }
317  }
318  else
319  {
320  $ilias->setSetting("enable_db_bench", 0);
321  if ($a_user !== 0)
322  {
323  $ilias->setSetting("db_bench_user", $a_user);
324  }
325  }
326  }
327 
335  function startDbBench($a_sql)
336  {
337  global $ilUser;
338 
339  if ($this->isDbBenchEnabled() && is_object($ilUser) &&
340  $this->db_enabled_user == $ilUser->getLogin() &&
341  !$this->db_bench_stop_rec)
342  {
343  $this->start = microtime();
344  $this->sql = $a_sql;
345  }
346  }
347 
348 
354  function stopDbBench()
355  {
356  global $ilUser;
357 
358  if ($this->isDbBenchEnabled() && is_object($ilUser) &&
359  $this->db_enabled_user == $ilUser->getLogin() &&
360  !$this->db_bench_stop_rec)
361  {
362  $this->db_bench[] =
363  array("start" => $this->start, "stop" => microtime(),
364  "sql" => $this->sql);
365  }
366  }
367 
374  function getDbBenchRecords()
375  {
376  global $ilDB;
377 
378  $set = $ilDB->query("SELECT * FROM benchmark");
379  $b = array();
380  while ($rec = $ilDB->fetchAssoc($set))
381  {
382  $b[] = array("sql" => $rec["sql_stmt"],
383  "time" => $rec["duration"]);
384  }
385  return $b;
386  }
387 
388 }
389 
390 ?>