├── .gitignore ├── constants.js ├── index.js ├── package.json ├── keep-alive.js ├── runner.js ├── config.yml ├── README.md └── lib └── fullTest.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .idea 3 | -------------------------------------------------------------------------------- /constants.js: -------------------------------------------------------------------------------- 1 | var fs = require('fs'), 2 | path = require('path'), 3 | yaml = require('js-yaml'); 4 | 5 | exports.regions = yaml.safeLoad(fs.readFileSync(path.join(process.cwd(), './config.yml'))).regions; 6 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var constants = require('./constants'), 2 | fullTest = require('./lib/fullTest'), 3 | runner = require('./runner'); 4 | 5 | runner.runFullTests().then(runner.enableKeepalive).then(runner.runFullTests); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hub-utility", 3 | "version": "1.0.0", 4 | "description": "Hub utilities", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "Ankur Goel", 10 | "license": "ISC", 11 | "dependencies": { 12 | "bufferutil": "^1.2.1", 13 | "command-line-args": "^3.0.1", 14 | "js-yaml": "^3.6.1", 15 | "q": "^1.4.1", 16 | "selenium-webdriver": "^2.53.3", 17 | "underscore": "^1.8.3", 18 | "utf-8-validate": "^1.2.1" 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /keep-alive.js: -------------------------------------------------------------------------------- 1 | var http = require('http'), 2 | https = require('https'); 3 | 4 | var agent = new http.Agent({ 5 | keepAlive: true, 6 | keepAliveMsecs: 30*1000 7 | }); 8 | 9 | var secureAgent = new https.Agent({ 10 | keepAlive: true, 11 | keepAliveMsecs: 30*1000 12 | }); 13 | 14 | var httpRequest = http.request; 15 | var httpsRequest = http.request; 16 | 17 | http.request = function(options, callback){ 18 | if(options.protocol == "https:"){ 19 | options["agent"] = secureAgent; 20 | return httpsRequest(options, callback); 21 | } 22 | else { 23 | options["agent"] = agent; 24 | return httpRequest(options, callback); 25 | } 26 | }; -------------------------------------------------------------------------------- /runner.js: -------------------------------------------------------------------------------- 1 | var q = require("q"), 2 | fullTest = require('./lib/fullTest'); 3 | 4 | 5 | function runFullTests() { 6 | var runAllTests = fullTest.selectedHubs.reduce(function(promise, nextUrl) { 7 | fullTest.checkArguments(); 8 | return promise.then(() => { 9 | console.log("Running test with", nextUrl + ". Please wait..."); 10 | return fullTest.newTest(nextUrl); 11 | }); 12 | }, q()); 13 | return runAllTests.then(fullTest.generateReport); 14 | } 15 | 16 | function enableKeepAlive() { 17 | console.log("\n\nUsing keep alive:\n"); 18 | require('./keep-alive'); 19 | } 20 | 21 | exports.runFullTests = runFullTests; 22 | exports.enableKeepalive = enableKeepAlive; -------------------------------------------------------------------------------- /config.yml: -------------------------------------------------------------------------------- 1 | regions: 2 | global: 3 | - http://hub.browserstack.com 4 | - https://hub.browserstack.com 5 | - http://hub.browserstack.com:4444 6 | - http://hub-cloud.browserstack.com 7 | - https://hub-cloud.browserstack.com 8 | us_east: 9 | - http://hub-us.browserstack.com 10 | - https://hub-us.browserstack.com 11 | - http://hub-us.browserstack.com:4444 12 | - http://hub-cloud-us.browserstack.com 13 | - https://hub-cloud-us.browserstack.com 14 | us_west: 15 | - http://hub-usw.browserstack.com 16 | - https://hub-usw.browserstack.com 17 | - http://hub-usw.browserstack.com:4444 18 | - http://hub-cloud-usw.browserstack.com 19 | - https://hub-cloud-usw.browserstack.com 20 | eu_west: 21 | - http://hub-eu.browserstack.com 22 | - https://hub-eu.browserstack.com 23 | - http://hub-eu.browserstack.com:4444 24 | - http://hub-cloud-eu.browserstack.com 25 | - https://hub-cloud-eu.browserstack.com 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Selenium Hub Utilities 2 | > Set of utilities to measure performance of selenium tests. 3 | 4 | ### Select best hub for [BrowserStack Automate](https://browserstack.com/automate) 5 | Install the [latest release](https://github.com/AnkurGel/selenium-hub-utilities/releases). 6 | #### For Mac 7 | ``` 8 | BROWSERSTACK_USERNAME=your_user_name BROWSERSTACK_ACCESS_KEY=your_access_key ./selenium-hub-utilities-mac 9 | ``` 10 | #### For Linux 11 | ``` 12 | BROWSERSTACK_USERNAME=your_user_name BROWSERSTACK_ACCESS_KEY=your_access_key ./selenium-hub-utilities-linux-x86 13 | ``` 14 | #### For Windows 15 | ``` 16 | set BROWSERSTACK_USERNAME=your_user_name && set BROWSERSTACK_ACCESS_KEY=your_access_key && .\selenium-hub-utilities-win32.exe 17 | ``` 18 | #### From git project 19 | ``` 20 | BROWSERSTACK_USERNAME=your_user_name BROWSERSTACK_ACCESS_KEY=your_access_key node index.js 21 | ``` 22 | ### Results 23 | 24 | > Running test with http://hub-us.browserstack.com. Please wait... 25 | > Time taken by http://hub-us.browserstack.com - 5948 ms 26 | > Running test with https://hub-us.browserstack.com. Please wait... 27 | > Time taken by https://hub-us.browserstack.com - 14325 ms 28 | > Running test with http://hub-us.browserstack.com:4444. Please wait... 29 | > Time taken by http://hub-us.browserstack.com:4444 - 13509 ms 30 | > Running test with http://hub-cloud-us.browserstack.com. Please wait... 31 | > Time taken by http://hub-cloud-us.browserstack.com - 17680 ms 32 | > Running test with https://hub-cloud-us.browserstack.com. Please wait... 33 | > Time taken by https://hub-cloud-us.browserstack.com - 40771 ms 34 | 35 | > -------------------------------------------------------------- 36 | > ----------------------------REPORT---------------------------- 37 | > -- http://hub-us.browserstack.com 5948 ms -- 38 | > -- http://hub-us.browserstack.com:4444 13509 ms -- 39 | > -- https://hub-us.browserstack.com 14325 ms -- 40 | > -- http://hub-cloud-us.browserstack.com 17680 ms -- 41 | > -- https://hub-cloud-us.browserstack.com 40771 ms -- 42 | 43 | > -------------------------------------------------------------- 44 | > > You can select the best hub endpoint according to report. 45 | 46 | 47 | #### Options 48 | 49 | * Add `REGION=region_name` to select a particular region. The available regions are: 50 | * us_east 51 | * us_west 52 | * eu_west 53 | 54 | -------------------------------------------------------------------------------- /lib/fullTest.js: -------------------------------------------------------------------------------- 1 | var webdriver = require('selenium-webdriver'), 2 | q = require('q'), 3 | underscore = require('underscore'), 4 | constants = require('../constants'); 5 | 6 | var keyCount = +process.env['KEYCOUNT'] || 100; 7 | var selectedHubs = constants.regions[process.env['REGION'] || 'global']; 8 | 9 | function checkArguments() { 10 | if(!process.env['BROWSERSTACK_USERNAME'] || !process.env['BROWSERSTACK_ACCESS_KEY']){ 11 | if(process.platform && process.platform == 'win32') { 12 | console.log("Correct usage: set BROWSERSTACK_USERNAME=your_user_name && set BROWSERSTACK_ACCESS_KEY=your_access_key &&", process.argv[0]); 13 | } else { 14 | var command = process.argv[0] == 'node' ? process.argv.join(' ') : process.argv[0]; 15 | console.log("Correct usage: BROWSERSTACK_USERNAME=your_user_name BROWSERSTACK_ACCESS_KEY=your_access_key", command); 16 | } 17 | process.exit(); 18 | } 19 | } 20 | 21 | var results = {}; 22 | 23 | var capabilities = { 24 | 'browserName' : 'chrome', 25 | 'os': 'OS X', 26 | 'os_version': 'Yosemite', 27 | 'browserstack.user': process.env['BROWSERSTACK_USERNAME'], 28 | 'browserstack.key': process.env['BROWSERSTACK_ACCESS_KEY'], 29 | 'build': 'Selenium Utilities - Speed test ' + (process.env['REGION'] || '') 30 | }; 31 | 32 | // TODO: handle rejection of a promise 33 | function newTest(driverUrl) { 34 | capabilities['name'] = driverUrl; 35 | var defer = q.defer(); 36 | var driver = new webdriver.Builder(). 37 | usingServer(driverUrl + '/wd/hub'). 38 | withCapabilities(capabilities). 39 | build(); 40 | 41 | driver.get('https://www.google.com/ncr'); 42 | var element = driver.findElement(webdriver.By.name('q')); 43 | var timeNow = new Date(); 44 | element.then(function() { 45 | timeNow = new Date(); 46 | }); 47 | 48 | 49 | var selPromises = underscore.times(keyCount, () => { return q(element.sendKeys('a')); }); 50 | q.allSettled(selPromises).then(function() { 51 | var timeTaken = new Date() - timeNow; 52 | console.log("Time taken by", driverUrl, "-", timeTaken, 'ms'); 53 | results[driverUrl] = timeTaken; 54 | driver.quit().then(defer.resolve, defer.resolve); 55 | }); 56 | 57 | 58 | return defer.promise; 59 | } 60 | 61 | function generateReport() { 62 | console.log("--------------------------------------------------------------"); 63 | console.log("----------------------------REPORT----------------------------"); 64 | underscore.sortBy(underscore.pairs(results), (hubTime) => { 65 | return hubTime[1]; 66 | }).forEach((hub) => { 67 | console.log("--", hub[0], hub[1], "ms", "--"); 68 | }); 69 | console.log("--------------------------------------------------------------"); 70 | } 71 | 72 | exports.selectedHubs = selectedHubs; 73 | exports.checkArguments = checkArguments; 74 | exports.newTest = newTest; 75 | exports.generateReport = generateReport; 76 | --------------------------------------------------------------------------------