ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilBenchmark.php
Go to the documentation of this file.
1<?php
2
20
25{
26 public const DB_BENCH_USER = "db_bench_user";
27 public const ENABLE_DB_BENCH = "enable_db_bench";
28 private ?ilDBInterface $db = null;
29 private ?ilSetting $settings = null;
30 private ?ilObjUser $user = null;
31
32 private Container $dic;
33
34 private string $start = '';
35 private ?string $temporary_sql_storage = '';
36 private array $collected_db_benchmarks = [];
37
38 private bool $stop_db_recording = false;
39
40 private int $bench_max_records = 2000;
41
42 private ?bool $db_bechmark_enabled = null;
43 private ?int $db_bechmark_user_id = null;
44
48 public function __construct()
49 {
50 global $DIC;
51 $this->dic = $DIC;
52 $this->initSettins();//(int) ($this->retrieveSetting("bench_max_records") ?? 0);
53 }
54
55 private function initSettins(): void
56 {
57 if (!$this->settings instanceof ilSetting) {
58 $global_settings_available = $this->dic->isDependencyAvailable('settings');
59 if ($global_settings_available) {
60 $this->settings = $this->dic->settings();
61
62 $this->db_bechmark_enabled = (bool) ($this->retrieveSetting(self::ENABLE_DB_BENCH) ?? false);
63 $user_id = $this->retrieveSetting(self::DB_BENCH_USER);
64 $this->db_bechmark_user_id = $user_id !== null ? (int) $user_id : null;
65 }
66 }
67 }
68
69 private function retrieveSetting(string $keyword): ?string
70 {
71 return $this->settings !== null
72 ? $this->settings->get($keyword)
73 : null;
74 }
75
76 private function retrieveDB(): ?ilDBInterface
77 {
78 if (!$this->db instanceof ilDBInterface && $this->dic->isDependencyAvailable('database')) {
79 $this->db = $this->dic->database();
80 }
81 return $this->db;
82 }
83
84 private function isDBavailable(): bool
85 {
86 return !is_null($this->retrieveDB());
87 }
88
89 private function retrieveUser(): ?ilObjUser
90 {
91 if (!$this->user instanceof ilObjUser && $this->dic->isDependencyAvailable('user')) {
92 $this->user = $this->dic->user();
93 }
94 return $this->user;
95 }
96
97 private function isUserAvailable(): bool
98 {
99 return !is_null($this->retrieveUser());
100 }
101
102 private function microtimeDiff(string $t1, string $t2): string
103 {
104 $partials1 = explode(" ", $t1);
105 $partials2 = explode(" ", $t2);
106
107 return (string) ((float) $partials2[0] - (float) $partials1[0] + (float) $partials2[1] - (float) $partials1[1]);
108 }
109
113 public function clearData(): void
114 {
115 if ($this->isDBavailable()) {
116 $db = $this->retrieveDB();
117 if ($db !== null) {
118 $db->manipulate("DELETE FROM benchmark");
119 }
120 }
121 }
122
128 public function start(string $a_module, string $a_bench): void
129 {
130 }
131
137 public function stop(string $a_module, string $a_bench): void
138 {
139 }
140
144 public function save(): void
145 {
146 if (!$this->isDBavailable() || !$this->isUserAvailable()) {
147 return;
148 }
149 if ($this->isDbBenchEnabled()
150 && $this->db_bechmark_user_id === $this->user->getId()) {
151 if (is_array($this->collected_db_benchmarks)) {
152 $this->stop_db_recording = true;
153
154 $db = $this->retrieveDB();
155 if ($db !== null) {
156 $db->manipulate("DELETE FROM benchmark");
157 foreach ($this->collected_db_benchmarks as $b) {
158 $id = $db->nextId('benchmark');
159 $db->insert("benchmark", [
160 "id" => ["integer", $id],
161 "duration" => ["float", $this->microtimeDiff($b["start"], $b["stop"])],
162 "sql_stmt" => ["clob", $b["sql"]]
163 ]);
164 }
165 }
166 }
167 $this->disableDbBenchmark();
168 }
169 }
170
174 private function getCurrentRecordNumber(): int
175 {
176 if ($this->isDBavailable()) {
177 $db = $this->retrieveDB();
178 if ($db !== null) {
179 $cnt_set = $db->query("SELECT COUNT(*) AS cnt FROM benchmark");
180 $cnt_rec = $db->fetchAssoc($cnt_set);
181 return (int) $cnt_rec["cnt"];
182 }
183 }
184 return 0;
185 }
186
187 //
188 //
189 // NEW DB BENCHMARK IMPLEMENTATION
190 //
191 //
192
196 public function isDbBenchEnabled(): bool
197 {
198 return $this->db_bechmark_enabled === true && $this->isDBavailable();
199 }
200
201 public function enableDbBenchmarkForUserName(?string $a_user): void
202 {
203 if ($a_user === null) {
204 $this->disableDbBenchmark();
205 return;
206 }
207 $this->initSettins();
208 $this->db_bechmark_enabled = true;
209 $this->settings->set(self::ENABLE_DB_BENCH, '1');
210
212
213 $this->db_bechmark_user_id = $user_id;
214 $this->settings->set(self::DB_BENCH_USER, (string) $user_id);
215 }
216
217 public function disableDbBenchmark(): void
218 {
219 $this->db_bechmark_enabled = false;
220 $this->settings->set(self::ENABLE_DB_BENCH, '0');
221 $this->db_bechmark_user_id = null;
222 $this->settings->set(self::DB_BENCH_USER, '0');
223 }
224
228 public function startDbBench(string $a_sql): void
229 {
230 $this->initSettins();
231 if (
232 !$this->stop_db_recording
233 && $this->isDbBenchEnabled()
234 && $this->isUserAvailable()
235 && $this->db_bechmark_user_id === $this->user->getId()
236 ) {
237 $this->start = (string) microtime();
238 $this->temporary_sql_storage = $a_sql;
239 }
240 }
241
242 public function stopDbBench(): bool
243 {
244 if (
245 !$this->stop_db_recording
246 && $this->isDbBenchEnabled()
247 && $this->isUserAvailable()
248 && $this->db_bechmark_user_id === $this->user->getId()
249 ) {
250 $this->collected_db_benchmarks[] = ["start" => $this->start, "stop" => (string) microtime(), "sql" => $this->temporary_sql_storage];
251
252 return true;
253 }
254
255 return false;
256 }
257
258 public function getDbBenchRecords(): array
259 {
260 if ($this->isDBavailable()) {
261 $db = $this->retrieveDB();
262 if ($db !== null) {
263 $set = $db->query("SELECT * FROM benchmark");
264 $b = [];
265 while ($rec = $db->fetchAssoc($set)) {
266 $b[] = [
267 "sql" => $rec["sql_stmt"],
268 "time" => $rec["duration"]
269 ];
270 }
271 return $b;
272 }
273 }
274 return [];
275 }
276}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:36
Class ilBenchmark.
getCurrentRecordNumber()
get current number of benchmark records
microtimeDiff(string $t1, string $t2)
ilDBInterface $db
isDbBenchEnabled()
Check wether benchmarking is enabled or not.
__construct()
Constructor.
retrieveSetting(string $keyword)
start(string $a_module, string $a_bench)
start measurement
startDbBench(string $a_sql)
start measurement
string $temporary_sql_storage
ilSetting $settings
enableDbBenchmarkForUserName(?string $a_user)
array $collected_db_benchmarks
save()
save all measurements
stop(string $a_module, string $a_bench)
stop measurement
clearData()
delete all measurement data
User class.
static _lookupId(string|array $a_user_str)
ILIAS Setting Class.
Interface ilDBInterface.
insert(string $table_name, array $values)
nextId(string $table_name)
manipulate(string $query)
Run a (write) Query on the database.
query(string $query)
Run a (read-only) Query on the database.
fetchAssoc(ilDBStatement $statement)
global $DIC
Definition: shib_login.php:26