devtools icon indicating copy to clipboard operation
devtools copied to clipboard

pref: events-reader handle large log file will take long time

Open CrabSAMA opened this issue 1 year ago • 2 comments

At first time I must to say, I did not know current is too early to talk about preformance about this library, because we rely on rolldown, which not stable and maybe have some break change. But IMO events-reader is the most important tool in the whole library, every feature is rely on the data readed by reader.

Therefore, I believe that there is indeed for improvement in this thing, and this is also something we need to be concerned about in the future, so I have decided to take this issue.

I am very happy to communicate with everyone about this issue. If you have any opinions, please let me know.

Problem

Currently I use the devtools to analyze my real production project, devtool have really helpful and help me found some problem when building, but in the real project, the log file is too large(more than 800m), I make a simple time count about events-reader:

async read() {
  // start
  console.log('read start:', performance.now())
  const { mtime, size } = await fs.promises.stat(this.filepath)
  if (mtime.getTime() <= this.lastTimestamp) {
    return
  }
  const stream = fs.createReadStream(this.filepath, {
    start: this.lastBytes,
  })
  this.lastTimestamp = mtime.getTime()
  this.lastBytes = size
  await parseJsonStreamWithConcatArrays<Event>(
    stream,
    (event) => {
      this.manager.handleEvent(event)
      return event
    },
  )
  // end
  console.log('read end:', performance.now())
}

and I get the result:

read start: 6914.6415                                                7:45:25 PM
ℹ                                                                   7:45:29 PM
[vue-tsc] Found 0 errors. Watching for file changes.
read end: 44218.320792                                               7:46:02 PM

When load a file it cost 37s, not too slow, but not fast either.

IMO the larger the project, the more likely it is to encounter performance issues, which is where devtool most useful. So reader's performance will face to challenge, maybe we need to find some better perf way to read file.

My attempt

When I communicate with gpt, I know currently the log file is a file format called NDJSON. NDJSON is very simple itself, just each line is a traditional JSON object.

Now we use stream-json to read json log file, stream-json is a very powerful library of parse json file, it can ensure the accuracy and robustness of parsing, but it does indeed take more time in parsing.

About handling NDJSON, I found a solution: use split2 to split by line and use JSON.parse to parse. I make a small attempt:

async read() {
  console.log('read start:', performance.now())
  const { mtime, size } = await fs.promises.stat(this.filepath)
  if (mtime.getTime() <= this.lastTimestamp) {
    return
  }
  const stream = fs.createReadStream(this.filepath, {
    start: this.lastBytes,
  })
  this.lastTimestamp = mtime.getTime()
  this.lastBytes = size
  // use split2 to split and parse
  await pipeline(
    stream,
    split2(),
    async (source: AsyncIterable<string>) => {
      for await (const line of source) {
        if (!line)
          continue
        this.manager.handleEvent(JSON.parse(line))
      }
    },
  )
  console.log('read end:', performance.now())
}

and I get the result:

read start: 8545.192125                                              8:04:25 PM
ℹ                                                                   8:04:29 PM
[vue-tsc] Found 0 errors. Watching for file changes.
read end: 19023.620083                                               8:04:35 PM

Same file it cost 10.4s, the time cost reduced 2/3 (37s -> 10.4s).

I know that this simple performance evaluation is not reliable, we also need more code to handle exceptional situations, but indeed, in multiple tests, the time consumption of this method has significantly decreased.


So I have some question:

  • Is now too early to discuss about the preformance?
  • Are we need a better preformance way to read file? Not necessarily split2, it's just my attempt. :-)

Welcome everyone to discuss this topic!

CrabSAMA avatar Aug 28 '25 12:08 CrabSAMA

The log reading is only a short-term solution, and we may use DB to replace it in the future (not yet confirmed). It's a good catch and attempt, and I don't mind us introducing split2 as a temporary solution to improve performance (if the changes are not large). PR welcome, and we can discuss it further in the PR.

webfansplz avatar Aug 28 '25 13:08 webfansplz

The log reading is only a short-term solution, and we may use DB to replace it in the future (not yet confirmed). It's a good catch and attempt, and I don't mind us introducing split2 as a temporary solution to improve performance (if the changes are not large). PR welcome, and we can discuss it further in the PR.

Great! I also think log reading is temp solution, and I very interest to see anyother better way to replace such as use DB.

BTW, I will try to take a PR soon, hope my work will helpful.

CrabSAMA avatar Aug 28 '25 13:08 CrabSAMA