ILIAS  Release_4_1_x_branch Revision 61804
 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 
110 return;
111  if ($this->isEnabled() &&
112  ($this->getMaximumRecords() > $this->getCurrentRecordNumber()))
113  {
114  foreach($this->bench as $key => $bench)
115  {
116  $bench_arr = explode(":", $key);
117  $bench_module = $bench_arr[0];
118  $benchmark = $bench_arr[1];
119  foreach($bench as $time)
120  {
121  $q = "INSERT INTO benchmark (cdate, duration, module, benchmark) VALUES ".
122  "(".
123  $ilDB->now().", ".
124  $ilDB->quote($time, "float").", ".
125  $ilDB->quote($bench_module, "text").", ".
126  $ilDB->quote($benchmark, "text").")";
127  $ilDB->manipulate($q);
128  }
129  }
130  $this->bench = array();
131  }
132  }
133 
134 
135  /*
136  SELECT module, benchmark, COUNT(*) AS cnt, AVG(duration) AS avg_dur FROM benchmark
137  GROUP BY module, benchmark ORDER BY module, benchmark
138  */
139 
143  function getEvaluation($a_module)
144  {
145  global $ilDB;
146 
147  $q = "SELECT COUNT(*) AS cnt, AVG(duration) AS avg_dur, benchmark,".
148  " MIN(duration) AS min_dur, MAX(duration) AS max_dur".
149  " FROM benchmark".
150  " WHERE module = ".$ilDB->quote($a_module, "text")." ".
151  " GROUP BY benchmark".
152  " ORDER BY benchmark";
153  $bench_set = $ilDB->query($q);
154  $eva = array();
155  while($bench_rec = $ilDB->fetchAssoc($bench_set))
156  {
157  $eva[] = array("benchmark" => $bench_rec["benchmark"],
158  "cnt" => $bench_rec["cnt"], "duration" => $bench_rec["avg_dur"],
159  "min" => $bench_rec["min_dur"], "max" => $bench_rec["max_dur"]);
160  }
161  return $eva;
162  }
163 
164 
169  {
170  global $ilDB;
171 
172  $q = "SELECT COUNT(*) AS cnt FROM benchmark";
173  $cnt_set = $ilDB->query($q);
174  $cnt_rec = $ilDB->fetchAssoc($cnt_set);
175 
176  return $cnt_rec["cnt"];
177  }
178 
179 
183  function getMaximumRecords()
184  {
185  global $ilias;
186 
187  return $ilias->getSetting("bench_max_records");
188  }
189 
190 
194  function setMaximumRecords($a_max)
195  {
196  global $ilias;
197 
198  $ilias->setSetting("bench_max_records", (int) $a_max);
199  }
200 
201 
205  function isEnabled()
206  {
207  global $ilSetting;
208 
209  if (!is_object($ilSetting))
210  {
211  return true;
212  }
213 
214  return (boolean) $ilSetting->get("enable_bench");
215  }
216 
217 
221  function enable($a_enable)
222  {
223  global $ilias;
224 
225  if ($a_enable)
226  {
227  $ilias->setSetting("enable_bench", 1);
228  }
229  else
230  {
231  $ilias->setSetting("enable_bench", 0);
232  }
233  }
234 
235 
240  {
241  global $ilDB;
242 
243  $q = "SELECT DISTINCT module FROM benchmark";
244  $mod_set = $ilDB->query($q);
245 
246  $modules = array();
247  while ($mod_rec = $ilDB->fetchAssoc($mod_set))
248  {
249  $modules[$mod_rec["module"]] = $mod_rec["module"];
250  }
251 
252  return $modules;
253  }
254 
255  // BEGIN WebDAV: Get measured time.
261  function getMeasuredTime($a_module, $a_bench)
262  {
263  if (isset($this->bench[$a_module.":".$a_bench]))
264  {
265  return $this->bench[$a_module.":".$a_bench][count($this->bench[$a_module.":".$a_bench]) - 1];
266  }
267  return false;
268  }
269  // END WebDAV: Get measured time.
270 
271  //
272  //
273  // NEW DB BENCHMARK IMPLEMENTATION
274  //
275  //
276 
280  function isDbBenchEnabled()
281  {
282  global $ilSetting;
283 
284  if (isset($this->db_enabled))
285  {
286  return $this->db_enabled;
287  }
288 
289  if (!is_object($ilSetting))
290  {
291  return false;
292  }
293 
294  $this->db_enabled = $ilSetting->get("enable_db_bench");
295  $this->db_enabled_user = $ilSetting->get("db_bench_user");
296  return $this->db_enabled;
297  }
298 
305  function enableDbBench($a_enable, $a_user = 0)
306  {
307  global $ilias;
308 
309  if ($a_enable)
310  {
311  $ilias->setSetting("enable_db_bench", 1);
312  if ($a_user !== 0)
313  {
314  $ilias->setSetting("db_bench_user", $a_user);
315  }
316  }
317  else
318  {
319  $ilias->setSetting("enable_db_bench", 0);
320  if ($a_user !== 0)
321  {
322  $ilias->setSetting("db_bench_user", $a_user);
323  }
324  }
325  }
326 
334  function startDbBench($a_sql)
335  {
336  global $ilUser;
337 
338  if ($this->isDbBenchEnabled() && is_object($ilUser) &&
339  $this->db_enabled_user == $ilUser->getLogin() &&
340  !$this->db_bench_stop_rec)
341  {
342  $this->start = microtime();
343  $this->sql = $a_sql;
344  }
345  }
346 
347 
353  function stopDbBench()
354  {
355  global $ilUser;
356 
357  if ($this->isDbBenchEnabled() && is_object($ilUser) &&
358  $this->db_enabled_user == $ilUser->getLogin() &&
359  !$this->db_bench_stop_rec)
360  {
361  $this->db_bench[] =
362  array("start" => $this->start, "stop" => microtime(),
363  "sql" => $this->sql);
364  }
365  }
366 
373  function getDbBenchRecords()
374  {
375  global $ilDB;
376 
377  $set = $ilDB->query("SELECT * FROM benchmark");
378  $b = array();
379  while ($rec = $ilDB->fetchAssoc($set))
380  {
381  $b[] = array("sql" => $rec["sql_stmt"],
382  "time" => $rec["duration"]);
383  }
384  return $b;
385  }
386 
387 }
388 
389 ?>