mapbox / node-cpp-skel

Skeleton for bindings to C++ libraries for Node.js using node-addon-api
Creative Commons Zero v1.0 Universal
72 stars 10 forks source link

Add memory tracking to the benchmark scripts #95

Closed springmeyer closed 6 years ago

springmeyer commented 6 years ago

Memory usage can be as important of a target as latency when aiming for fast code. This is because an application that might run fast under low memory pressure could all of a sudden run really slow under high memory pressure.

So, it would be great to add memory tracking to the node-cpp-skel benchmarks, such that developers can opt-in easiely to seeing what the peak memory usage was during a benchmark. This should be opt-in because tracking memory might skew the code speed slightly, so developers should only enable when they absolutely need memory tracking.

TODO: define these terms in the C++ glossary: https://github.com/mapbox/cpp/issues/44

springmeyer commented 6 years ago

This could be done by:

  1. Accepting a --mem argument to the benchmark scripts and include bytes module

npm install bytes --save
  1. Creating a stats object like:
    var memstats = {
      max_rss:0,
      max_heap:0,
      max_heap_total:0
    };  
  1. If --mem is passed, then check every 100 iterations for memory peak:
          if (track_mem && (runs % 100) == 0) {
              var mem = process.memoryUsage();
              if (mem.rss > memstats.max_rss) memstats.max_rss = mem.rss;
              if (mem.heapTotal > memstats.max_heap_total) memstats.max_heap_total = mem.heapTotal;
              if (mem.heapUsed > memstats.max_heap) memstats.max_heap = mem.heapUsed;
          }
  1. Dump results at end of run
console.log('Benchmark peak mem: ',bytes(memstats.max_rss),bytes(memstats.max_heap),bytes(memstats.max_heap_total));
GretaCB commented 6 years ago

Finished per https://github.com/mapbox/node-cpp-skel/pull/102