Next-i18next: Add memory leak load testing

Created on 14 Aug 2019  路  8Comments  路  Source: isaachinman/next-i18next

As per #450, we need load tests to ensure heap size is stable over hundreds or thousands of requests.

Most helpful comment

What I did a while back to profile our application when we had memory leaks in the past was to use https://www.npmjs.com/package/heapdump library.

I took two approaches:

  1. Take a snapshot of memory when the server starts and one when there is a memory leak
// Start memory reporting block
  if (process.env.NODE_ENV === 'production') {
    heapdump.writeSnapshot(`initial-${Date.now()}.heapsnapshot`)

     memwatch.on('leak', function(info) {
      heapdump.writeSnapshot(`leak-${Date.now()}.heapsnapshot`)

       console.log('-- Memory leak')
      console.log(info)
    })
  }
  // End memory reporting block

Then I would get the snapshot from the server and load on chrome inspector to analyze it.
If I'm not wrong, I think this approach didn't give me anything valuable. So I took a different approach.

  1. I took a snapshot every 30 minutes, and with that, I had more data to work with:
  // Start memory reporting block
  if (process.env.NODE_ENV === 'production') {
    const initialHeapDumpName = `${Date.now()}.initial.heapsnapshot`
    console.log(`>>> Initial snapshot created with name ${initialHeapDumpName}`)
    heapdump.writeSnapshot(initialHeapDumpName)

    // Take a memory snapshot every 30 minutes
    setInterval(function writeHeapSnapshot() {
      const partialHeapDumpName = `${Date.now()}.interval.heapsnapshot`
      heapdump.writeSnapshot(
        `>>> Partial snapshot created with name ${partialHeapDumpName}`
      )
    }, 1000 * 60 * 30)
  }
  // End memory reporting block

But be aware that while the server is writing the snapshot it blocks the event loop and make your server slow until it finishes the process

All 8 comments

Hi! How can I help with this?

We need to find a way to:

  1. Run an npm script, passing in an --inspect flag, or similar, holding this process open for later assertion
  2. Immediately assert against the memory usage of the process
  3. Run artillery against a localhost port
  4. Assert again against the memory usage of the process
  5. Close the process

What I did a while back to profile our application when we had memory leaks in the past was to use https://www.npmjs.com/package/heapdump library.

I took two approaches:

  1. Take a snapshot of memory when the server starts and one when there is a memory leak
// Start memory reporting block
  if (process.env.NODE_ENV === 'production') {
    heapdump.writeSnapshot(`initial-${Date.now()}.heapsnapshot`)

     memwatch.on('leak', function(info) {
      heapdump.writeSnapshot(`leak-${Date.now()}.heapsnapshot`)

       console.log('-- Memory leak')
      console.log(info)
    })
  }
  // End memory reporting block

Then I would get the snapshot from the server and load on chrome inspector to analyze it.
If I'm not wrong, I think this approach didn't give me anything valuable. So I took a different approach.

  1. I took a snapshot every 30 minutes, and with that, I had more data to work with:
  // Start memory reporting block
  if (process.env.NODE_ENV === 'production') {
    const initialHeapDumpName = `${Date.now()}.initial.heapsnapshot`
    console.log(`>>> Initial snapshot created with name ${initialHeapDumpName}`)
    heapdump.writeSnapshot(initialHeapDumpName)

    // Take a memory snapshot every 30 minutes
    setInterval(function writeHeapSnapshot() {
      const partialHeapDumpName = `${Date.now()}.interval.heapsnapshot`
      heapdump.writeSnapshot(
        `>>> Partial snapshot created with name ${partialHeapDumpName}`
      )
    }, 1000 * 60 * 30)
  }
  // End memory reporting block

But be aware that while the server is writing the snapshot it blocks the event loop and make your server slow until it finishes the process

@lucasfeliciano Nice - interesting stuff. Depending on how we launch the process, it should be as simple as using process.memoryUsage().heapUsed, though, right?

I wonder if there is a util/process-runner that measures heap size over time and allows you to perform assertions. That'd be a really useful package if anyone wants to create and maintain it...

@isaachinman If I'm not wrong process.memoryUsage().heapUsed gives us the total amount of memory.

heapdump.writeSnapshot() gives us a snapshot file to load on chrome inspector.

image

So we can analyze in detail what objects are in the heap.

It is pretty useful!
Check it out: https://developers.google.com/web/tools/chrome-devtools/memory-problems/

@lucasfeliciano I'm fully aware of how to analyse via the inspector, but the point is that we need a programmatic way to do this so that it can be run as a blocking step in CI. Is that possible with heap snapshots?

We can set a listener using memwatch pacakge which I also used on the snippet above but forgot to mention.

memwatch.on('leak', (info) => {
  console.error('Memory leak detected:\n', info);
});

@lucasfeliciano Interesting! Does memwatch only inspect the process that launched it, or is it also aware of all child processes?

Was this page helpful?
0 / 5 - 0 ratings