0789f54618e4d59d5b2a81e1f74fa34a38589f3d
[friendica-addons.git] / rendertime / rendertime.php
1 <?php
2 /**
3  * Name: rendertime
4  * Description: Shows the time that was needed to render the current page
5  * Version: 0.1
6  * Author: Michael Vvogel <http://pirati.ca/profile/heluecht>
7  *
8  */
9 use Friendica\Core\Addon;
10 use Friendica\Core\Config;
11
12 function rendertime_install() {
13         Addon::registerHook('page_end', 'addon/rendertime/rendertime.php', 'rendertime_page_end');
14 }
15
16
17 function rendertime_uninstall() {
18         Addon::unregisterHook('init_1', 'addon/rendertime/rendertime.php', 'rendertime_init_1');
19         Addon::unregisterHook('page_end', 'addon/rendertime/rendertime.php', 'rendertime_page_end');
20 }
21
22 function rendertime_init_1(&$a) {
23 }
24
25 function rendertime_page_end(&$a, &$o) {
26
27         $duration = microtime(true)-$a->performance["start"];
28
29         $ignored_modules = ["fbrowser"];
30         $ignored = in_array($a->module, $ignored_modules);
31
32         if (is_site_admin() && ($_GET["mode"] != "minimal") && !$a->is_mobile && !$a->is_tablet && !$ignored) {
33                 $o = $o.'<div class="renderinfo">'.sprintf(t("Database: %s/%s, Network: %s, Rendering: %s, Session: %s, I/O: %s, Other: %s, Total: %s"),
34                                                 round($a->performance["database"] - $a->performance["database_write"], 3),
35                                                 round($a->performance["database_write"], 3),
36                                                 round($a->performance["network"], 2),
37                                                 round($a->performance["rendering"], 2),
38                                                 round($a->performance["parser"], 2),
39                                                 round($a->performance["file"], 2),
40                                                 round($duration - $a->performance["database"]
41                                                          - $a->performance["network"] - $a->performance["rendering"]
42                                                          - $a->performance["parser"] - $a->performance["file"], 2),
43                                                 round($duration, 2)
44                                                 //round($a->performance["markstart"], 3)
45                                                 //round($a->performance["plugin"], 3)
46                                                 )."</div>";
47
48                 if (Config::get("rendertime", "callstack")) {
49                         $o .= "<pre>";
50                         $o .= "\nDatabase Read:\n";
51                         foreach ($a->callstack["database"] AS $func => $time) {
52                                 $time = round($time, 3);
53                                 if ($time > 0)
54                                         $o .= $func.": ".$time."\n";
55                         }
56                         $o .= "\nDatabase Write:\n";
57                         foreach ($a->callstack["database_write"] AS $func => $time) {
58                                 $time = round($time, 3);
59                                 if ($time > 0)
60                                         $o .= $func.": ".$time."\n";
61                         }
62
63                         $o .= "\nNetwork:\n";
64                         foreach ($a->callstack["network"] AS $func => $time) {
65                                 $time = round($time, 3);
66                                 if ($time > 0)
67                                         $o .= $func.": ".$time."\n";
68                         }
69
70                         $o .= "</pre>";
71                 }
72         }
73 }