]> git.mxchange.org Git - friendica-addons.git/blob - rendertime/rendertime.php
Apply suggestions from code review
[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 Vogel <http://pirati.ca/profile/heluecht>
7  *
8  */
9
10 use Friendica\Core\Hook;
11 use Friendica\Core\Renderer;
12 use Friendica\DI;
13
14 function rendertime_install() {
15         Hook::register('page_end', 'addon/rendertime/rendertime.php', 'rendertime_page_end');
16         DI::config()->set('system', 'profiler', true);
17 }
18
19 function rendertime_uninstall() {
20         DI::config()->delete('system', 'profiler');
21 }
22
23 function rendertime_init_1(&$a) {
24 }
25
26 function rendertime_addon_admin(&$a, &$o) {
27         $t = Renderer::getMarkupTemplate("admin.tpl", "addon/rendertime/");
28
29         $o = Renderer::replaceMacros($t, [
30                 '$submit' => DI::l10n()->t('Save Settings'),
31                 '$callstack' => ['callstack', DI::l10n()->t('Show callstack'), DI::config()->get('rendertime', 'callstack'), DI::l10n()->t('Show detailed performance measures in the callstack. When deactivated, only the summary will be displayed.')],
32                 '$minimal_time' => ['minimal_time', DI::l10n()->t('Minimal time'), DI::config()->get('rendertime', 'minimal_time'), DI::l10n()->t('Minimal time that an activity needs to be listed in the callstack.')],
33         ]);
34 }
35
36 function rendertime_addon_admin_post(&$a) {
37         DI::config()->set('rendertime', 'callstack', $_POST['callstack'] ?? false);
38         DI::config()->set('rendertime', 'minimal_time', $_POST['minimal_time'] ?? 0);
39 }
40
41 /**
42  * @param Friendica\App $a
43  * @param string $o
44  */
45 function rendertime_page_end(Friendica\App $a, &$o)
46 {
47
48         $profiler = DI::profiler();
49
50         $duration = microtime(true) - $profiler->get('start');
51
52         $ignored_modules = ["fbrowser"];
53         $ignored = in_array(DI::args()->getModuleName(), $ignored_modules);
54
55         if ($a->isSiteAdmin() && (($_GET['mode'] ?? '') != 'minimal') && !DI::mode()->isMobile() && !DI::mode()->isMobile() && !$ignored) {
56
57                 $o = $o . '<div class="renderinfo">' . DI::l10n()->t("Database: %s/%s, Network: %s, Rendering: %s, Session: %s, I/O: %s, Other: %s, Total: %s",
58                                 round($profiler->get('database') - $profiler->get('database_write'), 3),
59                                 round($profiler->get('database_write'), 3),
60                                 round($profiler->get('network'), 2),
61                                 round($profiler->get('rendering'), 2),
62                                 round($profiler->get('session'), 2),
63                                 round($profiler->get('file'), 2),
64                                 round($duration - $profiler->get('database')
65                                         - $profiler->get('network') - $profiler->get('rendering')
66                                         - $profiler->get('session') - $profiler->get('file'), 2),
67                                 round($duration, 2)
68                         //round($profiler->get('markstart'), 3)
69                         //round($profiler->get('plugin'), 3)
70                         ) . '</div>';
71
72                         $total = microtime(true) - $profiler->get('start');
73                         $rest = $total - ($profiler->get('ready') - $profiler->get('start')) - $profiler->get('init') - $profiler->get('content');
74                         $o = $o . '<div class="renderinfo">' . DI::l10n()->t("Class-Init: %s, Boot: %s, Init: %s, Content: %s, Other: %s, Total: %s",
75                                 round($profiler->get('classinit') - $profiler->get('start'), 3),
76                                 round($profiler->get('ready') - $profiler->get('classinit'), 3),
77                                 round($profiler->get('init'), 3),
78                                 round($profiler->get('content'), 3),
79                                 round($rest, 3),
80                                 round($total, 3)
81                                 ) . '</div>';
82
83                 if ($profiler->isRendertime()) {
84                         $o .= '<pre>';
85                         $o .= $profiler->getRendertimeString(DI::config()->get('rendertime', 'minimal_time', 0));
86                         $o .= '</pre>';
87                 }
88         }
89 }