memory.js 856 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. 'use strict'
  2. const PROPERTIES = [ 'rss', 'heapTotal', 'heapUsed', 'external' ]
  3. let memory
  4. module.exports = {
  5. initialise,
  6. update,
  7. report
  8. }
  9. function initialise () {
  10. memory = PROPERTIES.reduce((result, name) => {
  11. result[name] = {
  12. sum: 0,
  13. hwm: 0
  14. }
  15. return result
  16. }, { count: 0 })
  17. }
  18. function update () {
  19. const currentMemory = process.memoryUsage()
  20. PROPERTIES.forEach(name => updateProperty(name, currentMemory))
  21. }
  22. function updateProperty (name, currentMemory) {
  23. const m = memory[name]
  24. const c = currentMemory[name]
  25. m.sum += c
  26. if (c > m.hwm) {
  27. m.hwm = c
  28. }
  29. }
  30. function report () {
  31. PROPERTIES.forEach(name => reportProperty(name))
  32. }
  33. function reportProperty (name) {
  34. const m = memory[name]
  35. // eslint-disable-next-line no-console
  36. console.log(`mean ${name}: ${m.sum / memory.count}; hwm: ${m.hwm}`)
  37. }