denoland / deno_blog

Minimal boilerplate blogging.
MIT License
477 stars 100 forks source link

Slow Loads #87

Closed willpuckett closed 1 year ago

willpuckett commented 2 years ago

Blog is hosted on deno deploy at https://smote.io and is taking about 7 seconds to load on initial load. Once it's been loaded, it moves normally. There's no warnings or errors coming out of the logs... Is this normal?

lino-levan commented 1 year ago

Definitely not normal, what are you hosting this on?

lino-levan commented 1 year ago

Ah, I am blind you said deploy. Just a few comments:

willpuckett commented 1 year ago

Yes blog is on Deno Deploy at https://smote.io. The repo is available at https://github.dev/willpuckett/smote.

I modified the deno_blog source code to produce some timing information by changing the loadContent() function in blog.tsx to the following:

async function loadContent(blogDirectory: string, isDev: boolean) {
  // Read posts from the current directory and store them in memory.
  const postsDirectory = join(blogDirectory, "posts");

  let post_load_time = 0;
  // TODO(@satyarohith): not efficient for large number of posts.
  for await (
    const entry of walk(postsDirectory)
  ) {
    const t0 = performance.now();
    if (entry.isFile && entry.path.endsWith(".md")) {
      await loadPost(postsDirectory, entry.path);
    }
    const t1 = performance.now();
    const entry_performance = t1 - t0;
    console.log(`Load ${entry.name} took ${entry_performance} milliseconds.`);
    post_load_time += entry_performance;

  }

  console.log(`Total post load time: ${post_load_time}`);

  if (isDev) {
    watchForChanges(postsDirectory).catch(() => {});
  }
}

It runs very quickly on my local machine, but the timing on Deno Deploy takes much longer. Here's a sample:

Screenshot 2023-01-31 at 1 11 38 PM

No caching (that I'm aware of)....

lino-levan commented 1 year ago

Last week I was able to reproduce the slowdowns but I can't anymore. Can you still reproduce this?

willpuckett commented 1 year ago

Yes it’s definitely still happening.

From the beginning, it seems to only happen when an isolate is spinning up. Then, it’s normal/snappy for a while, and then when what I imagine is an isolate becomes inactive, the long start up time recurs.

I don’t know if it’s a network latency issue or if it’s simply slow parsing the markdown.

On Jan 31, 2023, at 2:41 PM, Lino Le Van @.***> wrote:

Last week I was able to reproduce the slowdowns but I can't anymore. Can you still reproduce this?

— Reply to this email directly, view it on GitHub https://github.com/denoland/deno_blog/issues/87#issuecomment-1411165439, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADC355IL2OJ2U4RU2ZL5BELWVGIJJANCNFSM6AAAAAAQYCBLAM. You are receiving this because you authored the thread.

willpuckett commented 1 year ago

Just updated to the latest pushes in the repo and it’s taking 7 seconds from isolate spin up to serve.

 On Feb 3, 2023, at 2:32 PM, gmail @.***> wrote:

Yes it’s definitely still happening.

From the beginning, it seems to only happen when an isolate is spinning up. Then, it’s normal/snappy for a while, and then when what I imagine is an isolate becomes inactive, the long start up time recurs.

I don’t know if it’s a network latency issue or if it’s simply slow parsing the markdown.

On Jan 31, 2023, at 2:41 PM, Lino Le Van @.***> wrote:

Last week I was able to reproduce the slowdowns but I can't anymore. Can you still reproduce this?

— Reply to this email directly, view it on GitHub https://github.com/denoland/deno_blog/issues/87#issuecomment-1411165439, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADC355IL2OJ2U4RU2ZL5BELWVGIJJANCNFSM6AAAAAAQYCBLAM. You are receiving this because you authored the thread.

ayame113 commented 1 year ago

loadPost is currently running serially. https://github.com/denoland/deno_blog/blob/0a43328bc1d890ed21ea8938bac049d78001250a/blog.tsx#L194-L203

How about changing this to run in parallel? (As follows)

const loadingPromises: Promise<void>[] = [];

for await (const entry of walk(postsDirectory)) {
  if (entry.isFile && entry.path.endsWith(".md")) {
    loadingPromises.push(loadPost(postsDirectory, entry.path));
  }
}

await Promise.all(loadingPromises);

You seem to have 42 posts, so hopefully the startup time should be 1/42nd, about 0.15 seconds.

willpuckett commented 1 year ago

World of genius. Load time goes from about 9 seconds to under two. This should be the accepted answer.

On Feb 4, 2023, at 11:39 AM, ayame113 @.***> wrote:

loadPost is currently running serially. https://github.com/denoland/deno_blog/blob/0a43328bc1d890ed21ea8938bac049d78001250a/blog.tsx#L194-L203

How about changing this to run in parallel? (As follows)

const loadingPromises: Promise[] = [];

for await (const entry of walk(postsDirectory)) { if (entry.isFile && entry.path.endsWith(".md")) { loadingPromises.push(loadPost(postsDirectory, entry.path)); } }

await Promise.all(loadingPromises); — Reply to this email directly, view it on GitHub https://github.com/denoland/deno_blog/issues/87#issuecomment-1416833957, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADC355PZZN6YRYTIA2NYJTDWV2V7RANCNFSM6AAAAAAQYCBLAM. You are receiving this because you authored the thread.