ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
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 ?bool $db_bechmark_enabled = null;
41 private ?int $db_bechmark_user_id = null;
42
46 public function __construct()
47 {
48 global $DIC;
49 $this->dic = $DIC instanceof Container ? $DIC : null;
50 $this->initSettings();
51 }
52
53 private function initSettings(): void
54 {
55 if ($this->dic === null) {
56 return;
57 }
58
59 if (!$this->settings instanceof ilSetting) {
60 $global_settings_available = $this->dic->isDependencyAvailable('settings');
61 if ($global_settings_available) {
62 $this->settings = $this->dic->settings();
63
64 $this->db_bechmark_enabled = (bool) ($this->retrieveSetting(self::ENABLE_DB_BENCH) ?? false);
65 $user_id = $this->retrieveSetting(self::DB_BENCH_USER);
66 $this->db_bechmark_user_id = $user_id !== null ? (int) $user_id : null;
67 }
68 }
69 }
70
71 private function retrieveSetting(string $keyword): ?string
72 {
73 return $this->settings !== null
74 ? $this->settings->get($keyword)
75 : null;
76 }
77
78 private function retrieveDB(): ?ilDBInterface
79 {
80 if (!$this->db instanceof ilDBInterface && $this->dic->isDependencyAvailable('database')) {
81 $this->db = $this->dic->database();
82 }
83 return $this->db;
84 }
85
86 private function isDBavailable(): bool
87 {
88 return !is_null($this->retrieveDB());
89 }
90
91 private function retrieveUser(): ?ilObjUser
92 {
93 if (!$this->user instanceof ilObjUser && $this->dic->isDependencyAvailable('user')) {
94 $this->user = $this->dic->user();
95 }
96 return $this->user;
97 }
98
99 private function isUserAvailable(): bool
100 {
101 return !is_null($this->retrieveUser());
102 }
103
104 private function microtimeDiff(string $t1, string $t2): string
105 {
106 $partials1 = explode(" ", $t1);
107 $partials2 = explode(" ", $t2);
108
109 return (string) ((float) $partials2[0] - (float) $partials1[0] + (float) $partials2[1] - (float) $partials1[1]);
110 }
111
115 public function clearData(): void
116 {
117 if ($this->isDBavailable()) {
118 $db = $this->retrieveDB();
119 if ($db !== null) {
120 $db->manipulate("DELETE FROM benchmark");
121 }
122 }
123 }
124
130 public function start(string $a_module, string $a_bench): void
131 {
132 }
133
139 public function stop(string $a_module, string $a_bench): void
140 {
141 }
142
146 public function save(): void
147 {
148 if (!$this->isDBavailable() || !$this->isUserAvailable()) {
149 return;
150 }
151 if ($this->isDbBenchEnabled()
152 && $this->db_bechmark_user_id === $this->user->getId()) {
153 if (is_array($this->collected_db_benchmarks)) {
154 $this->stop_db_recording = true;
155
156 $db = $this->retrieveDB();
157 if ($db !== null) {
158 $db->manipulate("DELETE FROM benchmark");
159 foreach ($this->collected_db_benchmarks as $b) {
160 $id = $db->nextId('benchmark');
161 $db->insert("benchmark", [
162 "id" => ["integer", $id],
163 "duration" => ["float", $this->microtimeDiff($b["start"], $b["stop"])],
164 "sql_stmt" => ["clob", $b["sql"]]
165 ]);
166 }
167 }
168 }
169 $this->disableDbBenchmark();
170 }
171 }
172
173 //
174 //
175 // NEW DB BENCHMARK IMPLEMENTATION
176 //
177 //
178
182 public function isDbBenchEnabled(): bool
183 {
184 return $this->db_bechmark_enabled === true && $this->isDBavailable();
185 }
186
187 public function enableDbBenchmarkForUserName(?string $a_user): void
188 {
189 if ($a_user === null) {
190 $this->disableDbBenchmark();
191 return;
192 }
193 $this->initSettings();
194 $this->db_bechmark_enabled = true;
195 $this->settings->set(self::ENABLE_DB_BENCH, '1');
196
198
199 $this->db_bechmark_user_id = $user_id;
200 $this->settings->set(self::DB_BENCH_USER, (string) $user_id);
201 }
202
203 public function disableDbBenchmark(): void
204 {
205 $this->db_bechmark_enabled = false;
206 $this->settings->set(self::ENABLE_DB_BENCH, '0');
207 $this->db_bechmark_user_id = null;
208 $this->settings->set(self::DB_BENCH_USER, '0');
209 }
210
214 public function startDbBench(string $a_sql): void
215 {
216 $this->initSettings();
217 if (
218 !$this->stop_db_recording
219 && $this->isDbBenchEnabled()
220 && $this->isUserAvailable()
221 && $this->db_bechmark_user_id === $this->user->getId()
222 ) {
223 $this->start = microtime();
224 $this->temporary_sql_storage = $a_sql;
225 }
226 }
227
228 public function stopDbBench(): bool
229 {
230 if (
231 !$this->stop_db_recording
232 && $this->isDbBenchEnabled()
233 && $this->isUserAvailable()
234 && $this->db_bechmark_user_id === $this->user->getId()
235 ) {
236 $this->collected_db_benchmarks[] = ["start" => $this->start, "stop" => microtime(), "sql" => $this->temporary_sql_storage];
237
238 return true;
239 }
240
241 return false;
242 }
243
244 public function getDbBenchRecords(): array
245 {
246 if ($this->isDBavailable()) {
247 $db = $this->retrieveDB();
248 if ($db !== null) {
249 $set = $db->query("SELECT * FROM benchmark");
250 $b = [];
251 while ($rec = $db->fetchAssoc($set)) {
252 $b[] = [
253 "sql" => $rec["sql_stmt"],
254 "time" => $rec["duration"]
255 ];
256 }
257 return $b;
258 }
259 }
260 return [];
261 }
262}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:36
Class ilBenchmark.
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