Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Auditors:

Test Plan:
  • Loading branch information
ayumi committed Aug 17, 2017
1 parent fc019d8 commit e4cda44
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
logs
*.log
npm-debug.log.*
cpu-profiles
*.cpuprofile

# Runtime data
pids
Expand Down
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ env:
- CXX=g++-4.8 NODE_ENV=test TEST_DIR=contents
- CXX=g++-4.8 NODE_ENV=test TEST_DIR=misc-components
- CXX=g++-4.8 NODE_ENV=test TEST_DIR=navbar-components
- CXX=g++-4.8 NODE_ENV=test TEST_DIR=performance
- CXX=g++-4.8 NODE_ENV=test TEST_DIR=tab-components
addons:
apt:
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@
"base64-js": "^1.2.0",
"chai": "^3.4.1",
"chai-as-promised": "^5.1.0",
"chrome-remote-interface": "^0.24.3",
"co-mocha": "^1.1.2",
"cross-env": "^3.1.4",
"css-loader": "^0.23.0",
Expand Down
8 changes: 4 additions & 4 deletions test/lib/brave.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ var exports = {
.waitForVisible(urlInput)
})

this.app.client.addCommand('waitForUrl', function (url) {
this.app.client.addCommand('waitForUrl', function (url, timeout = 5000, interval = 100) {
logVerbose('waitForUrl("' + url + '")')
return this.waitUntil(function () {
return this.tabByUrl(url).then((response) => {
Expand All @@ -354,7 +354,7 @@ var exports = {
logVerbose('tabByUrl("' + url + '") => false')
return false
})
}, 5000, null, 100)
}, timeout, null, interval)
})

this.app.client.addCommand('waitForSelectedText', function (text) {
Expand Down Expand Up @@ -628,7 +628,7 @@ var exports = {
}).then((response) => response.value)
})

this.app.client.addCommand('newTab', function (createProperties = {}) {
this.app.client.addCommand('newTab', function (createProperties = {}, activateIfOpen = false, isRestore = false) {
return this
.execute(function (createProperties) {
return devTools('appActions').createTabRequested(createProperties)
Expand Down Expand Up @@ -1084,7 +1084,7 @@ var exports = {
? 'node_modules/electron-prebuilt/dist/brave.exe'
: './node_modules/.bin/electron',
env,
args: ['./', '--enable-logging', '--v=1'],
args: ['./', '--enable-logging', '--v=1', '--inspect=9222'],
requireName: 'devTools'
})
return this.app.start()
Expand Down
55 changes: 55 additions & 0 deletions test/lib/profilerUtil.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

'use strict'

const CDP = require('chrome-remote-interface')
const fs = require('fs')

const LOG_FOLDER = './cpu-profiles'

/**
* Connect to a remote instance using Chrome Debugging Protocol and enable
* the Profiler. Afterwards you need to profiler.start
* See https://github.com/cyrus-and/chrome-remote-interface#cdpoptions-callback
*/
const initProfiler = function * (context) {
const cdp = yield CDP({port: 9222})
context.profiler = cdp.Profiler
yield context.profiler.enable()
yield context.profiler.setSamplingInterval({interval: 100})
}

/**
* initProfiler and start it.
* @param context {object} webdriver client
*/
const startProfiler = function * (context) {
yield initProfiler(context)
yield context.profiler.start()
}

/**
* @param context {object} webdriver client
* @param tag {string=} Optional file prefix
* @returns filename to which CPU profile was written
*/
const stopProfiler = function * (context, tag = '') {
const cdpProfilerResult = yield context.profiler.stop()
if (!fs.existsSync(LOG_FOLDER)) {
console.log(`Creating directory ${LOG_FOLDER}`)
fs.mkdirSync(LOG_FOLDER)
}
const filename = `${LOG_FOLDER}/${process.env.NODE_ENV}-${tag}-${new Date().toISOString()}.cpuprofile`
const string = JSON.stringify(cdpProfilerResult.profile, null, 2)
fs.writeFile(filename, string)
console.log(`Wrote CPU profile data to: ${filename}`)
return filename
}

module.exports = {
initProfiler,
startProfiler,
stopProfiler
}
57 changes: 57 additions & 0 deletions test/lib/userProfiles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const Immutable = require('immutable')

// TODO: Decouple test tab generation from topsites
const {topSites} = require('../../app/common/data/topSites')

// XXX: Up to ~2000 bookmarks
const addBookmarksN = function (total) {
return function * (client) {
const data = []
let n = 0
const pushBookmark = (etld, suffix = '') => {
if (n >= total) {
return false
}
data.push({
location: `https://www.${etld}/${suffix}`,
title: `${etld}, ${suffix}`,
parentFolderId: 0
})
n += 1
}
for (let etld of topSites) {
const success =
pushBookmark(etld) &&
pushBookmark(etld, 'tomato') &&
pushBookmark(etld, 'potato') &&
pushBookmark(etld, 'onion')
if (!success) { break }
}
const immutableData = Immutable.fromJS(data)
yield client.addBookmarks(immutableData)
}
}
const addBookmarks2000 = addBookmarksN(2000)

const addTabsN = function (total) {
return function * (client) {
const data = []
for (let i = 0; i < total; i++) {
const etld = topSites[i]
data.push({
active: false,
discarded: true,
url: `https://www.${etld}`
})
}
for (let datum of data) {
yield client.newTab(datum, false, true) // isRestore
}
}
}
const addTabs100 = addTabsN(100)

module.exports = {
addBookmarks2000,
addTabs100
}
75 changes: 75 additions & 0 deletions test/performance/startupTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/* global describe, it, beforeEach, afterEach */

const Brave = require('../lib/brave')
const profilerUtil = require('../lib/profilerUtil')
const {urlInput} = require('../lib/selectors')
const userProfiles = require('../lib/userProfiles')

describe('Performance startup', function () {
Brave.beforeAllServerSetup(this)

function * setup () {
yield Brave.startApp()
Brave.addCommands()
yield Brave.app.client
.waitForUrl(Brave.newTabUrl)
.waitForBrowserWindow()
}

function * restart (timeout = 1000) {
// XXX Wait for Brave to fully shutdown and free up inspect port 9222
yield Brave.stopApp(false, timeout)
yield Brave.startApp()
Brave.addCommands()
}

beforeEach(function * () {
this.url = Brave.server.url('page1.html')
yield setup()
})

afterEach(function * () {
yield Brave.stopApp()
})

function * runStory () {
yield Brave.app.client
.waitForUrl(Brave.newTabUrl, 10000, 250)
.waitForBrowserWindow()
.windowByUrl(Brave.browserWindowUrl)
.ipcSend('shortcut-focus-url')
.waitForVisible(urlInput)
.waitForElementFocus(urlInput)
for (let i = 0; i < this.url.length; i++) {
yield Brave.app.client
.keys(this.url[i])
.pause(30)
}
yield Brave.app.client
.keys(Brave.keys.ENTER)
.waitForUrl(this.url)
}

it('fresh', function * () {
yield restart()
yield profilerUtil.startProfiler(this)
yield runStory.call(this)
yield profilerUtil.stopProfiler(this, 'fresh')
})

it('2000 bookmarks', function * () {
yield userProfiles.addBookmarks2000(Brave.app.client)
yield restart()
yield profilerUtil.startProfiler(this)
yield runStory.call(this)
yield profilerUtil.stopProfiler(this, '2000-bookmarks')
})

it('100 tabs', function * () {
yield userProfiles.addTabs100(Brave.app.client)
yield restart(5000)
yield profilerUtil.startProfiler(this)
yield runStory.call(this)
yield profilerUtil.stopProfiler(this, '100-tabs')
})
})

0 comments on commit e4cda44

Please sign in to comment.