ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilBenchmark.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  +-----------------------------------------------------------------------------+
5  | ILIAS open source |
6  +-----------------------------------------------------------------------------+
7  | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
8  | |
9  | This program is free software; you can redistribute it and/or |
10  | modify it under the terms of the GNU General Public License |
11  | as published by the Free Software Foundation; either version 2 |
12  | of the License, or (at your option) any later version. |
13  | |
14  | This program is distributed in the hope that it will be useful, |
15  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
16  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17  | GNU General Public License for more details. |
18  | |
19  | You should have received a copy of the GNU General Public License |
20  | along with this program; if not, write to the Free Software |
21  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
22  +-----------------------------------------------------------------------------+
23 */
24 
25 
34 {
35 
39  function ilBenchmark()
40  {
41  }
42 
43 
47  function microtimeDiff($t1, $t2)
48  {
49  $t1 = explode(" ",$t1);
50  $t2 = explode(" ",$t2);
51  $diff = $t2[0] - $t1[0] + $t2[1] - $t1[1];
52 
53  return $diff;
54  }
55 
56 
57 
61  function clearData()
62  {
63  global $ilDB;
64 
65  $q = "DELETE FROM benchmark";
66  $ilDB->query($q);
67  }
68 
69 
77  function start($a_module, $a_bench)
78  {
79  $this->bench[$a_module.":".$a_bench][] = microtime();
80  }
81 
82 
88  function stop($a_module, $a_bench)
89  {
90  $this->bench[$a_module.":".$a_bench][count($this->bench[$a_module.":".$a_bench]) - 1]
91  = $this->microtimeDiff($this->bench[$a_module.":".$a_bench][count($this->bench[$a_module.":".$a_bench]) - 1], microtime());
92  }
93 
94 
98  function save()
99  {
100  global $ilDB;
101 
102  if ($this->isEnabled() &&
103  ($this->getMaximumRecords() > $this->getCurrentRecordNumber()))
104  {
105  foreach($this->bench as $key => $bench)
106  {
107  $bench_arr = explode(":", $key);
108  $bench_module = $bench_arr[0];
109  $benchmark = $bench_arr[1];
110  foreach($bench as $time)
111  {
112  $q = "INSERT INTO benchmark (cdate, duration, module, benchmark) VALUES ".
113  "(now(), ".$ilDB->quote($time).", ".$ilDB->quote($bench_module).", ".$ilDB->quote($benchmark).")";
114  $ilDB->query($q);
115  }
116  }
117  $this->bench = array();
118  }
119  }
120 
121 
122  /*
123  SELECT module, benchmark, COUNT(*) AS cnt, AVG(duration) AS avg_dur FROM benchmark
124  GROUP BY module, benchmark ORDER BY module, benchmark
125  */
126 
130  function getEvaluation($a_module)
131  {
132  global $ilDB;
133 
134  $q = "SELECT COUNT(*) AS cnt, AVG(duration) AS avg_dur, benchmark,".
135  " MIN(duration) AS min_dur, MAX(duration) AS max_dur".
136  " FROM benchmark".
137  " WHERE module = ".$ilDB->quote($a_module)." ".
138  " GROUP BY benchmark".
139  " ORDER BY benchmark";
140  $bench_set = $ilDB->query($q);
141  $eva = array();
142  while($bench_rec = $bench_set->fetchRow(DB_FETCHMODE_ASSOC))
143  {
144  $eva[] = array("benchmark" => $bench_rec["benchmark"],
145  "cnt" => $bench_rec["cnt"], "duration" => $bench_rec["avg_dur"],
146  "min" => $bench_rec["min_dur"], "max" => $bench_rec["max_dur"]);
147  }
148  return $eva;
149  }
150 
151 
156  {
157  global $ilDB;
158 
159  $q = "SELECT COUNT(*) AS cnt FROM benchmark";
160  $cnt_set = $ilDB->query($q);
161  $cnt_rec = $cnt_set->fetchRow(DB_FETCHMODE_ASSOC);
162 
163  return $cnt_rec["cnt"];
164  }
165 
166 
170  function getMaximumRecords()
171  {
172  global $ilias;
173 
174  return $ilias->getSetting("bench_max_records");
175  }
176 
177 
181  function setMaximumRecords($a_max)
182  {
183  global $ilias;
184 
185  $ilias->setSetting("bench_max_records", (int) $a_max);
186  }
187 
188 
192  function isEnabled()
193  {
194  global $ilias;
195 
196  return (boolean) $ilias->getSetting("enable_bench");
197  }
198 
199 
203  function enable($a_enable)
204  {
205  global $ilias;
206 
207  if ($a_enable)
208  {
209  $ilias->setSetting("enable_bench", 1);
210  }
211  else
212  {
213  $ilias->setSetting("enable_bench", 0);
214  }
215  }
216 
217 
222  {
223  global $ilDB;
224 
225  $q = "SELECT DISTINCT module FROM benchmark";
226  $mod_set = $ilDB->query($q);
227 
228  $modules = array();
229  while ($mod_rec = $mod_set->fetchRow(DB_FETCHMODE_ASSOC))
230  {
231  $modules[$mod_rec["module"]] = $mod_rec["module"];
232  }
233 
234  return $modules;
235  }
236 
237  // BEGIN WebDAV: Get measured time.
243  function getMeasuredTime($a_module, $a_bench)
244  {
245  return $this->bench[$a_module.":".$a_bench][count($this->bench[$a_module.":".$a_bench]) - 1];
246  }
247  // END WebDAV: Get measured time.
248 
249 }
250 
251 ?>