]> git.mxchange.org Git - friendica-addons.git/blob - rendertime/rendertime.php
Merge pull request #1351 from MrPetovan/bug/12665-monolog
[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\App;
11 use Friendica\Core\Hook;
12 use Friendica\Core\Renderer;
13 use Friendica\DI;
14
15 function rendertime_install() {
16         Hook::register('page_end', 'addon/rendertime/rendertime.php', 'rendertime_page_end');
17         DI::config()->set('system', 'profiler', true);
18 }
19
20 function rendertime_uninstall()
21 {
22         DI::config()->delete('system', 'profiler');
23 }
24
25 function rendertime_init_1()
26 {
27 }
28
29 function rendertime_addon_admin(string &$o)
30 {
31         $t = Renderer::getMarkupTemplate('admin.tpl', 'addon/rendertime/');
32
33         $o = Renderer::replaceMacros($t, [
34                 '$submit' => DI::l10n()->t('Save Settings'),
35                 '$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.')],
36                 '$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.')],
37         ]);
38 }
39
40 function rendertime_addon_admin_post()
41 {
42         DI::config()->set('rendertime', 'callstack', $_POST['callstack'] ?? false);
43         DI::config()->set('rendertime', 'minimal_time', $_POST['minimal_time'] ?? 0);
44 }
45
46 /**
47  * @param string $o
48  */
49 function rendertime_page_end(string &$o)
50 {
51         $profiler = DI::profiler();
52
53         $duration = microtime(true) - $profiler->get('start');
54
55         $ignored_modules = [
56                 \Friendica\Module\Media\Photo\Browser::class,
57                 \Friendica\Module\Media\Attachment\Browser::class,
58         ];
59         $ignored = in_array(DI::router()->getModuleClass(), $ignored_modules);
60
61         if (DI::userSession()->isSiteAdmin() && (($_GET['mode'] ?? '') != 'minimal') && !DI::mode()->isMobile() && !DI::mode()->isMobile() && !$ignored) {
62
63                 $o = $o . '<div class="renderinfo">' . DI::l10n()->t("Database: %s/%s, Network: %s, Rendering: %s, Session: %s, I/O: %s, Other: %s, Total: %s",
64                                 round($profiler->get('database') - $profiler->get('database_write'), 3),
65                                 round($profiler->get('database_write'), 3),
66                                 round($profiler->get('network'), 2),
67                                 round($profiler->get('rendering'), 2),
68                                 round($profiler->get('session'), 2),
69                                 round($profiler->get('file'), 2),
70                                 round($duration - $profiler->get('database')
71                                         - $profiler->get('network') - $profiler->get('rendering')
72                                         - $profiler->get('session') - $profiler->get('file'), 2),
73                                 round($duration, 2)
74                         //round($profiler->get('markstart'), 3)
75                         //round($profiler->get('plugin'), 3)
76                         ) . '</div>';
77
78                         $total = microtime(true) - $profiler->get('start');
79                         $rest = $total - ($profiler->get('ready') - $profiler->get('start')) - $profiler->get('init') - $profiler->get('content');
80                         $o = $o . '<div class="renderinfo">' . DI::l10n()->t("Class-Init: %s, Boot: %s, Init: %s, Content: %s, Other: %s, Total: %s",
81                                 round($profiler->get('classinit') - $profiler->get('start'), 3),
82                                 round($profiler->get('ready') - $profiler->get('classinit'), 3),
83                                 round($profiler->get('init'), 3),
84                                 round($profiler->get('content'), 3),
85                                 round($rest, 3),
86                                 round($total, 3)
87                                 ) . '</div>';
88
89                 if ($profiler->isRendertime()) {
90                         $o .= '<pre>';
91                         $o .= $profiler->getRendertimeString(floatval(DI::config()->get('rendertime', 'minimal_time', 0)));
92                         $o .= '</pre>';
93                 }
94         }
95 }