Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

改善首屏加载时间 #786

Closed
cesaryuan opened this issue Oct 11, 2023 · 2 comments
Closed

改善首屏加载时间 #786

cesaryuan opened this issue Oct 11, 2023 · 2 comments

Comments

@cesaryuan
Copy link
Contributor

如下图,chatgpt库加载耗时800ms,极大延长了首屏加载事件

image

@terwer
Copy link
Owner

terwer commented Oct 12, 2023

image
是chatgpt依赖的 ticktoken太大导致。

@cesaryuan
Copy link
Contributor Author

cesaryuan commented Oct 12, 2023

@terwer 我写了一个脚本来测试首次加载时间(脚本放在最后面了),发现了几个可以优化的点:

使用num run dev编译的代码,时间为50次取平均值

case 首屏加载时间 Disable cache后
原始 1406ms 1695ms
index.htmlmodulepreload改为preload 1089ms 1495ms
将chatgpt模块改成动态加载 1018ms 1150ms
preload+chatgpt动态加载 757ms 1000ms

可以看出两个修改点将首屏加载时间从1400ms降低到了757ms,速度提升了一倍

速度测试代码

在任意一个文档块运行即可

(async () => {
    let __costs = []
    for (let i of [...Array(50).keys()]) {
        siyuan.dialogs[0]?.destroy();
        // 打开插件的常规发布页面
        document.getElementById("plugin_siyuan-plugin-publisher_0").click();
        let menuitem = await waitForElement(document.querySelector('#commonMenu > .b3-menu__items'), "#commonMenu > div.b3-menu__items > button:nth-child(3)");
        let start = Date.now();  
        menuitem.click();
        // 等待插件页面加载完成
        let frame = await waitForElement(document.body, "body > div.b3-dialog--open > div > div.b3-dialog__container > div.b3-dialog__body > iframe")
        await new Promise((r) => {
          frame.contentWindow.addEventListener('DOMContentLoaded', () => r());
        })
        await waitForElement(frame.contentDocument, "#app-layout-default > div.default-main > div.platform-desc > p > div > div.el-alert__content")
        // 计算时间
        let cost = Date.now() - start;
        console.log('cost', i, cost)
        __costs.push(cost);
        siyuan.dialogs[0]?.destroy();
    }
    console.log("avg cost:", __costs.reduce((a, b) => a + b, 0) / __costs.length);
})();

function waitForElement(rootElement, cssSelector) {
  return new Promise((resolve) => {
    const element = rootElement.querySelector(cssSelector);
    if (element) {
      resolve(element);
    } else {
      const observer = new MutationObserver(() => {
        const targetElement = rootElement.querySelector(cssSelector);
        if (targetElement) {
          observer.disconnect();
          resolve(targetElement);
        } else {
          console.log('not found')
        }
      });

      const config = {
        childList: true,
        subtree: true,
        attributes: true
      };

      observer.observe(rootElement, config);
    }
  });
}

cesaryuan added a commit to cesaryuan/siyuan-plugin-publisher that referenced this issue Oct 12, 2023
@terwer terwer closed this as completed Oct 12, 2023
@terwer terwer pinned this issue Oct 12, 2023
@terwer terwer changed the title chatgpt极大减慢了首屏加载时间,有没有办法改善 改善首屏加载时间 Oct 12, 2023
@terwer terwer unpinned this issue Oct 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants