From e3e093c574e9e49268b3cedcbe407ddd11ae9602 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Peyrott?= Date: Tue, 5 Jan 2016 16:48:19 -0300 Subject: [PATCH 1/2] Add memory metrics using Chrome's window.performance.memory extension. --- .gitignore | 1 + docs/MemoryMetrics.json | 62 ++++++++++++++++++++++++++++++++++ lib/metrics/MemoryMetrics.js | 65 ++++++++++++++++++++++++++++++++++++ lib/metrics/index.js | 5 +-- lib/probes/MemoryProbe.js | 37 ++++++++++++++++++++ 5 files changed, 168 insertions(+), 2 deletions(-) create mode 100644 docs/MemoryMetrics.json create mode 100644 lib/metrics/MemoryMetrics.js create mode 100644 lib/probes/MemoryProbe.js diff --git a/.gitignore b/.gitignore index afb904b..373f340 100644 --- a/.gitignore +++ b/.gitignore @@ -48,3 +48,4 @@ Thumbnails *.sublime-workspace *.sublime-project +.tern-project diff --git a/docs/MemoryMetrics.json b/docs/MemoryMetrics.json new file mode 100644 index 0000000..989bf62 --- /dev/null +++ b/docs/MemoryMetrics.json @@ -0,0 +1,62 @@ +{ + "totalJSHeapSize_max": { + "type": "max", + "tags": ["Script"], + "unit": "bytes", + "source": "MemoryMetrics", + "importance": 50, + "summary": "Maximum size (peak) of JavaScript heap including unused space", + "details": "", + "browsers": ["chrome"] + }, + "totalJSHeapSize_min": { + "type": "min", + "tags": ["Script"], + "unit": "bytes", + "source": "MemoryMetrics", + "importance": 20, + "summary": "Minimum size of JavaScript heap including unused space", + "details": "", + "browsers": ["chrome"] + }, + "totalJSHeapSize_avg": { + "type": "average", + "tags": ["Script"], + "unit": "bytes", + "source": "MemoryMetrics", + "importance": 70, + "summary": "Average size of JavaScript heap including unused space", + "details": "", + "browsers": ["chrome"] + }, + "usedJSHeapSize_max": { + "type": "max", + "tags": ["Script"], + "unit": "bytes", + "source": "MemoryMetrics", + "importance": 50, + "summary": "Maximum size (peak) of JavaScript heap excluding unused space", + "details": "", + "browsers": ["chrome"] + }, + "usedJSHeapSize_min": { + "type": "min", + "tags": ["Script"], + "unit": "bytes", + "source": "MemoryMetrics", + "importance": 20, + "summary": "Minimum size of JavaScript heap excluding unused space", + "details": "", + "browsers": ["chrome"] + }, + "usedJSHeapSize_avg": { + "type": "average", + "tags": ["Script"], + "unit": "bytes", + "source": "MemoryMetrics", + "importance": 70, + "summary": "Average size of JavaScript heap excluding unused space", + "details": "", + "browsers": ["chrome"] + } +} diff --git a/lib/metrics/MemoryMetrics.js b/lib/metrics/MemoryMetrics.js new file mode 100644 index 0000000..7592995 --- /dev/null +++ b/lib/metrics/MemoryMetrics.js @@ -0,0 +1,65 @@ +var Q = require('q'), + debug = require('debug')('bp:metrics:MemoryMetrics'), + BaseMetrics = require('./BaseMetrics'); + +function MemoryMetrics(probes) { + BaseMetrics.apply(this, [{ + probes: probes + }]); +} +require('util').inherits(MemoryMetrics, BaseMetrics); + +MemoryMetrics.prototype.id = 'MemoryMetrics'; +MemoryMetrics.prototype.probes = ['MemoryProbe']; + +MemoryMetrics.prototype.setup = function() { + this.results = []; +} + +MemoryMetrics.prototype.start = function() { +} + +MemoryMetrics.prototype.onData = function(data) { + this.results.push(data); +} + +MemoryMetrics.prototype.onError = function() { + debug('onError Method called'); +} + +MemoryMetrics.prototype.getResults = function() { + var totalmin = Number.MAX_VALUE, totalmax = 0, totalavg = 0, + usedmin = Number.MAX_VALUE, usedmax = 0, usedavg = 0; + + this.results.forEach(function(v) { + if(v.totalJSHeapSize < totalmin) { + totalmin = v.totalJSHeapSize; + } + if(v.totalJSHeapSize > totalmax) { + totalmax = v.totalJSHeapSize; + } + totalavg += v.totalJSHeapSize; + + if(v.usedJSHeapSize < usedmin) { + usedmin = v.usedJSHeapSize; + } + if(v.usedJSHeapSize > usedmax) { + usedmax = v.usedJSHeapSize; + } + usedavg += v.usedJSHeapSize; + }); + + usedavg /= this.results.length; + totalavg /= this.results.length; + + return { + totalJSHeapSize_max: totalmax, + totalJSHeapSize_min: totalmin, + totalJSHeapSize_avg: totalavg, + usedJSHeapSize_max: usedmax, + usedJSHeapSize_min: usedmin, + usedJSHeapSize_avg: usedavg, + } +} + +module.exports = MemoryMetrics; diff --git a/lib/metrics/index.js b/lib/metrics/index.js index 97bd513..8829929 100644 --- a/lib/metrics/index.js +++ b/lib/metrics/index.js @@ -85,5 +85,6 @@ module.exports.builtIns = [ 'ChromeTracingMetrics', 'RafRenderingStats', 'NetworkTimings', - 'NetworkResources' -]; \ No newline at end of file + 'NetworkResources', + 'MemoryMetrics' +]; diff --git a/lib/probes/MemoryProbe.js b/lib/probes/MemoryProbe.js new file mode 100644 index 0000000..d9c1891 --- /dev/null +++ b/lib/probes/MemoryProbe.js @@ -0,0 +1,37 @@ +var Q = require('q'), + util = require('util'), + events = require('events'), + helpers = require('../helpers'), + debug = require('debug')('bp:probes:MemoryProbe'); + +function MemoryProbe(id) { + if (id) { + this.id = id; + } + events.EventEmitter.call(this); +} + +util.inherits(MemoryProbe, events.EventEmitter); + +MemoryProbe.prototype.id = 'MemoryProbe'; + +MemoryProbe.prototype.setup = function(cfg) { + this.timerHandle = null; +}; + +MemoryProbe.prototype.start = function(browser) { + var me = this; + this.timerHandle = setInterval(function() { + browser.eval('window.performance.memory').then(function(res) { + if(res) { + me.emit('data', res); + } + }); + }, 50); +}; + +MemoryProbe.prototype.teardown = function(cfg, browser) { + clearInterval(this.timerHandle); +}; + +module.exports = MemoryProbe; From 728a9f24342fa19f7fb96692a811eda47e96e842 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Peyrott?= Date: Wed, 6 Jan 2016 12:18:23 -0300 Subject: [PATCH 2/2] MemoryProbe timer set to 1 sec. --- lib/probes/MemoryProbe.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/probes/MemoryProbe.js b/lib/probes/MemoryProbe.js index d9c1891..44693df 100644 --- a/lib/probes/MemoryProbe.js +++ b/lib/probes/MemoryProbe.js @@ -27,7 +27,7 @@ MemoryProbe.prototype.start = function(browser) { me.emit('data', res); } }); - }, 50); + }, 1000); }; MemoryProbe.prototype.teardown = function(cfg, browser) {