From 0a128636ed32fae53a1c9c002fffce61d3affa24 Mon Sep 17 00:00:00 2001 From: Rami Jasim Date: Fri, 24 Jan 2025 15:00:55 +0100 Subject: implement timer and log time --- lib/classes/MyRealmModel.php | 8 ++++- lib/classes/Timer.php | 82 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 lib/classes/Timer.php diff --git a/lib/classes/MyRealmModel.php b/lib/classes/MyRealmModel.php index f3ab44c..8ffc9d2 100644 --- a/lib/classes/MyRealmModel.php +++ b/lib/classes/MyRealmModel.php @@ -426,7 +426,9 @@ class MyRealmModel if ($group_field === 'mvv') { self::groupByMVVModule($sem_courses); } - + echo '
';
+        var_dump(Timer::getInstance()->get_time_deltas());
+        echo '
'; return $sem_courses; } @@ -440,6 +442,7 @@ class MyRealmModel */ public static function getAdditionalNavigations($object_id, &$my_obj_values, $sem_class, $user_id, $visit_data = []) { + $timer = Timer::getInstance(); $navigation = []; foreach (self::getDefaultModules() as $plugin_id => $plugin) { @@ -457,7 +460,10 @@ class MyRealmModel $navigation[$plugin_id] = self::checkVote($my_obj_values, $user_id, $object_id); } else if ($tool = $my_obj_values['tools']->findOneBy('plugin_id', $plugin_id)) { if (Seminar_Perm::get()->have_studip_perm($tool->getVisibilityPermission(), $object_id, $user_id)) { + $plugin_class = get_class($plugin); + $timer->start_timer($plugin_class, $object_id); $navigation[$plugin_id] = $plugin->getIconNavigation($object_id, $visit_data[$plugin_id]['visitdate'], $user_id); + $timer->stop_timer($plugin_class, $object_id); } else { $navigation[$plugin_id] = null; } diff --git a/lib/classes/Timer.php b/lib/classes/Timer.php new file mode 100644 index 0000000..8d5ec95 --- /dev/null +++ b/lib/classes/Timer.php @@ -0,0 +1,82 @@ +timers[$timer_name][$instance]['start'] = microtime(true); + } + + public function stop_timer(string $timer_name, $instance): void + { + if (isset($this->timers[$timer_name][$instance])) { + $this->timers[$timer_name][$instance]['end'] = microtime(true); + } else { + throw new InvalidArgumentException('Timer not found: ' . $timer_name); + } + } + + public function get_time_deltas(): array + { + $deltas = []; + foreach ($this->timers as $timer_name => $timer) { + foreach ($timer as $instance) { + $delta = $this->compute_delta($instance); + if (is_float($delta)) { + $deltas[$timer_name][] = $delta * 1000; + } + } + } + $res = []; + foreach ($deltas as $timer_name => $data) { + $res[$timer_name] = array_sum($data)/count($data); + } + return $res; + } + + private function compute_delta($data) + { + if (isset($data['start']) && isset($data['end'])) { + return $data['end'] - $data['start']; + } + return false; + } + + /** + * is not allowed to call from outside to prevent from creating multiple instances, + * to use the singleton, you have to obtain the instance from Singleton::getInstance() instead + */ + private function __construct() + { + } + + /** + * prevent the instance from being cloned (which would create a second instance of it) + */ + private function __clone() + { + } + + /** + * prevent from being unserialized (which would create a second instance of it) + */ + public function __wakeup() + { + throw new Exception("Cannot unserialize singleton"); + } +} -- cgit v1.0