Add simple JS init performance trace (#31459)

Related to #23461, and help some cases like #31412

For developers, they could use browser's Performance tool to collect
performance data, while this PR is also quite handy to optimize the
`index.js`.

For end users, this PR is simple enough and could figure out the slow
function quickly.


![image](https://github.com/go-gitea/gitea/assets/2114189/a557b08e-6594-474b-81a3-03d5ac2bd68e)
This commit is contained in:
wxiaoguang 2024-06-22 17:20:20 +08:00 committed by GitHub
parent 1a811c0bd1
commit 1d76e9aabf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -94,110 +94,138 @@ import {
import {initGlobalDropzone} from './features/dropzone.js'; import {initGlobalDropzone} from './features/dropzone.js';
import {initGlobalEnterQuickSubmit, initGlobalFormDirtyLeaveConfirm} from './features/common-form.js'; import {initGlobalEnterQuickSubmit, initGlobalFormDirtyLeaveConfirm} from './features/common-form.js';
// Init Gitea's Fomantic settings
initGiteaFomantic(); initGiteaFomantic();
initDirAuto(); initDirAuto();
initSubmitEventPolyfill(); initSubmitEventPolyfill();
function callInitFunctions(functions) {
// Start performance trace by accessing a URL by "https://localhost/?_ui_performance_trace=1" or "https://localhost/?key=value&_ui_performance_trace=1"
// It is a quick check, no side effect so no need to do slow URL parsing.
const initStart = performance.now();
if (window.location.search.includes('_ui_performance_trace=1')) {
let results = [];
for (const func of functions) {
const start = performance.now();
func();
results.push({name: func.name, dur: performance.now() - start});
}
results = results.sort((a, b) => b.dur - a.dur);
for (let i = 0; i < 20 && i < results.length; i++) {
// eslint-disable-next-line no-console
console.log(`performance trace: ${results[i].name} ${results[i].dur.toFixed(3)}`);
}
} else {
for (const func of functions) {
func();
}
}
const initDur = performance.now() - initStart;
if (initDur > 500) {
console.error(`slow init functions took ${initDur.toFixed(3)}ms`);
}
}
onDomReady(() => { onDomReady(() => {
initGlobalDropdown(); callInitFunctions([
initGlobalTabularMenu(); initGlobalDropdown,
initGlobalShowModal(); initGlobalTabularMenu,
initGlobalFetchAction(); initGlobalShowModal,
initGlobalTooltips(); initGlobalFetchAction,
initGlobalButtonClickOnEnter(); initGlobalTooltips,
initGlobalButtons(); initGlobalButtonClickOnEnter,
initGlobalCopyToClipboardListener(); initGlobalButtons,
initGlobalDropzone(); initGlobalCopyToClipboardListener,
initGlobalEnterQuickSubmit(); initGlobalDropzone,
initGlobalFormDirtyLeaveConfirm(); initGlobalEnterQuickSubmit,
initGlobalDeleteButton(); initGlobalFormDirtyLeaveConfirm,
initGlobalDeleteButton,
initCommonOrganization(); initCommonOrganization,
initCommonIssueListQuickGoto(); initCommonIssueListQuickGoto,
initCompSearchUserBox(); initCompSearchUserBox,
initCompWebHookEditor(); initCompWebHookEditor,
initInstall(); initInstall,
initHeadNavbarContentToggle(); initHeadNavbarContentToggle,
initFootLanguageMenu(); initFootLanguageMenu,
initCommentContent(); initCommentContent,
initContextPopups(); initContextPopups,
initHeatmap(); initHeatmap,
initImageDiff(); initImageDiff,
initMarkupAnchors(); initMarkupAnchors,
initMarkupContent(); initMarkupContent,
initSshKeyFormParser(); initSshKeyFormParser,
initStopwatch(); initStopwatch,
initTableSort(); initTableSort,
initAutoFocusEnd(); initAutoFocusEnd,
initFindFileInRepo(); initFindFileInRepo,
initCopyContent(); initCopyContent,
initAdminCommon(); initAdminCommon,
initAdminEmails(); initAdminEmails,
initAdminUserListSearchForm(); initAdminUserListSearchForm,
initAdminConfigs(); initAdminConfigs,
initAdminSelfCheck(); initAdminSelfCheck,
initDashboardRepoList(); initDashboardRepoList,
initNotificationCount(); initNotificationCount,
initNotificationsTable(); initNotificationsTable,
initOrgTeamSearchRepoBox(); initOrgTeamSearchRepoBox,
initOrgTeamSettings(); initOrgTeamSettings,
initRepoActivityTopAuthorsChart(); initRepoActivityTopAuthorsChart,
initRepoArchiveLinks(); initRepoArchiveLinks,
initRepoBranchButton(); initRepoBranchButton,
initRepoCodeView(); initRepoCodeView,
initRepoCommentForm(); initRepoCommentForm,
initRepoEllipsisButton(); initRepoEllipsisButton,
initRepoDiffCommitBranchesAndTags(); initRepoDiffCommitBranchesAndTags,
initRepoEditor(); initRepoEditor,
initRepoGraphGit(); initRepoGraphGit,
initRepoIssueContentHistory(); initRepoIssueContentHistory,
initRepoIssueDue(); initRepoIssueDue,
initRepoIssueList(); initRepoIssueList,
initRepoIssueSidebarList(); initRepoIssueSidebarList,
initArchivedLabelHandler(); initArchivedLabelHandler,
initRepoIssueReferenceRepositorySearch(); initRepoIssueReferenceRepositorySearch,
initRepoIssueTimeTracking(); initRepoIssueTimeTracking,
initRepoIssueWipTitle(); initRepoIssueWipTitle,
initRepoMigration(); initRepoMigration,
initRepoMigrationStatusChecker(); initRepoMigrationStatusChecker,
initRepoProject(); initRepoProject,
initRepoPullRequestMergeInstruction(); initRepoPullRequestMergeInstruction,
initRepoPullRequestAllowMaintainerEdit(); initRepoPullRequestAllowMaintainerEdit,
initRepoPullRequestReview(); initRepoPullRequestReview,
initRepoRelease(); initRepoRelease,
initRepoReleaseNew(); initRepoReleaseNew,
initRepoSettingGitHook(); initRepoSettingGitHook,
initRepoSettingSearchTeamBox(); initRepoSettingSearchTeamBox,
initRepoSettingsCollaboration(); initRepoSettingsCollaboration,
initRepoTemplateSearch(); initRepoTemplateSearch,
initRepoTopicBar(); initRepoTopicBar,
initRepoWikiForm(); initRepoWikiForm,
initRepository(); initRepository,
initRepositoryActionView(); initRepositoryActionView,
initRepositorySearch(); initRepositorySearch,
initRepoContributors(); initRepoContributors,
initRepoCodeFrequency(); initRepoCodeFrequency,
initRepoRecentCommits(); initRepoRecentCommits,
initCommitStatuses(); initCommitStatuses,
initCaptcha(); initCaptcha,
initUserAuthOauth2(); initUserAuthOauth2,
initUserAuthWebAuthn(); initUserAuthWebAuthn,
initUserAuthWebAuthnRegister(); initUserAuthWebAuthnRegister,
initUserSettings(); initUserSettings,
initRepoDiffView(); initRepoDiffView,
initPdfViewer(); initPdfViewer,
initScopedAccessTokenCategories(); initScopedAccessTokenCategories,
initColorPickers(); initColorPickers,
]);
}); });