summaryrefslogtreecommitdiff
path: root/test/sequential/test-net-bytes-per-incoming-chunk-overhead.js
blob: 8f766e8c7a4106f320b604f24a6a82b636ff3ba2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Flags: --expose-gc
'use strict';

const common = require('../common');
const assert = require('assert');
const net = require('net');

// Tests that, when receiving small chunks, we do not keep the full length
// of the original allocation for the libuv read call in memory.

let client;
let baseRSS;
const receivedChunks = [];
const N = 250000;

const server = net.createServer(common.mustCall((socket) => {
  baseRSS = process.memoryUsage().rss;

  socket.setNoDelay(true);
  socket.on('data', (chunk) => {
    receivedChunks.push(chunk);
    if (receivedChunks.length < N) {
      client.write('a');
    } else {
      client.end();
      server.close();
    }
  });
})).listen(0, common.mustCall(() => {
  client = net.connect(server.address().port);
  client.setNoDelay(true);
  client.write('hello!');
}));

process.on('exit', () => {
  global.gc();
  const bytesPerChunk =
    (process.memoryUsage().rss - baseRSS) / receivedChunks.length;
  // We should always have less than one page (usually ~ 4 kB) per chunk.
  assert(bytesPerChunk < 512, `measured ${bytesPerChunk} bytes per chunk`);
});