Tobias Bosch 6f303121c6 refactor(perf): introduce benchpress2
Major changes:
- make API more reusable
- format output nicely
- only force gc if needed

Regarding forcing gc:
Forcing gc can change script execution time.
We now don't force gc at first and ignore results where gc happens during script execution.
When we ignored too many results, we switch to forcing gc.

Closes #339
2015-01-05 17:49:50 -08:00

56 lines
1.7 KiB
JavaScript

var webdriver = require('protractor/node_modules/selenium-webdriver');
module.exports = {
gc: gc,
timelineRecords: timelineRecords,
timelineTimestamp: timelineTimestamp
};
function timelineTimestamp(timestampId) {
browser.executeScript('console.timeStamp("'+timestampId+'")');
}
function timelineRecords() {
return perfLogs().then(function(logs) {
var logs = logs && logs['Timeline.eventRecorded'] || [];
return logs.map(function(message) {
return message.record;
});
});
}
function perfLogs() {
return plainLogs('performance').then(function(entries) {
var entriesByMethod = {};
entries.forEach(function(entry) {
var message = JSON.parse(entry.message).message;
var entries = entriesByMethod[message.method];
if (!entries) {
entries = entriesByMethod[message.method] = [];
}
entries.push(message.params);
});
return entriesByMethod;
});
}
// Needed as selenium-webdriver does not forward
// performance logs in the correct way
function plainLogs(type) {
return browser.driver.schedule(
new webdriver.Command(webdriver.CommandName.GET_LOG).
setParameter('type', type),
'WebDriver.manage().logs().get(' + type + ')');
}
function gc() {
// TODO(tbosch): this only works on chrome, and we actually should
// extend chromedriver to use the Debugger.CollectGarbage call of the
// remote debugger protocol.
// See http://src.chromium.org/viewvc/blink/trunk/Source/devtools/protocol.json
// For iOS Safari we need an extension to appium that uses
// the webkit remote debug protocol. See
// https://github.com/WebKit/webkit/blob/master/Source/WebInspectorUI/Versions/Inspector-iOS-8.0.json
return browser.executeScript('window.gc()');
}