Skip to content

Instantly share code, notes, and snippets.

@naranjja
Created July 19, 2018 17:48
Show Gist options
  • Select an option

  • Save naranjja/99b4bbf839b726a79952a804828ced53 to your computer and use it in GitHub Desktop.

Select an option

Save naranjja/99b4bbf839b726a79952a804828ced53 to your computer and use it in GitHub Desktop.
Stress test using Selenium JavaScript webdriver
const { exec } = require("child_process")
const { Builder } = require("selenium-webdriver")
/**
* Instance a stress test
* @param {String<URL>} url The URL to stress test
* @param {Integer} numWorkers The number of workers to instance in the test
* @param {String} browser The browser to use for the test (requires driver in path)
* @param {Boolean} stopImmediately Whether to stop the test immediately after finishing
* @example <caption>Stress test with 100 workers using Chrome and immediate stopping</caption>
* const stressTest = new StressTest("http://some/url/", 100, "chrome", true)
* stressTest.run()
* @example <caption>Stress test with 20 workers using Firefox</caption>
* const stressTest = new StressTest("http://some/url/", 20, "firefox", false)
* stressTest.run()
*/
class StressTest {
constructor (url, numWorkers, browser, stopImmediately=true) {
this.url = url
this.numWorkers = numWorkers
this.browser = browser ? browser : "chrome"
this.stopImmediately = stopImmediately
}
loadURL (i) {
return new Promise((resolve, reject) => {
let driver = new Builder().forBrowser(this.browser).build()
console.time(`Worker #${i + 1}`)
driver.get(this.url).then(() => {
resolve({ i, driver })
})
})
}
startWorkers () {
return new Promise((resolve, reject) => {
let c = 0
for (let i = 0; i < this.numWorkers; i++) {
this.loadURL(i)
.then((result) => {
c++
console.timeEnd(`Worker #${result.i + 1}`)
if (this.stopImmediately) result.driver.quit()
if (c === this.numWorkers) {
resolve(this.numWorkers)
}
})
}
})
}
killAll () {
exec("pkill chromedriver")
exec("pkill chromium-browse")
}
run () {
return new Promise((resolve, reject) => {
this.startWorkers().then((numWorkers) => {
console.log(`Finished stress test with ${this.numWorkers} worker${this.numWorkers > 1 ? "s" : ""}.`)
})
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment