wissel.net

Usability - Productivity - Business - The web - Singapore & Twins

Numbers are numbers, you have to see it! - Selenium edition


When looking at performance data and comparisons, numbers are just that: " X is 23% faster than y" is a statement few people can actually visualize. You have to see it in action to get a feel for the real difference. Applies to vehicles and web sites in the same manner.
Instinctively one would opt for a load test to see loading speeds, but after checking options I found a functional test will do just fine. My tool of choice here is Selenium WebDriver. It can be easily integrated into JUnit test and with a little effort even automatically record the whole session. So here is my test plan:
  1. Get a list of 2-3 URLs from the command line
  2. Open a new clean browser session for the number of URLs fetched
  3. Position the browser windows next to each other, so each has the same size
  4. Wait for the user hitting enter to start (so (s)he can adjust window sizes or resequence them)
  5. Spin of one thread for each URL to load the page
  6. Wait again
  7. Tear down the setup
Sounds much more complicated than it actually is. The whole code is about one hundred lines and can be easily extended to do more things. Selenium provides an IDE that assists to some extend getting started. I like Selenium for a number of reasons:
  • Can be fully integrated in JUnit tests
  • No new language to learn (it has bindings for quite some)
  • Functional test can be done without a specific browser using a generic web driver
  • Provides visible browser drivers (for Firefox and others) that by default use a new clean profile (no cache, no cookies)
  • Rich community and tons of examples
  • Can be used in your own code or delegated to cloud based testing service
  • Can test JavaScript, Ajax, Drag & Drop and Mobile
I run the code from a command line window, 3 lines high, perched at the bottom of my screen, so it doesn't get into the way of the big browser windows. Here comes the code:
package com.notessensei.speedtest;

import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.Collection;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class SimpleSpeedTest implements Runnable {

	private final WebDriver	driver;
	private final String	destination;
	private final int		width;
	private final int		height;
	private final int		xPosition;

	public static void waitEnter(String message) {
		System.out.println(message);
		try {
			System.in.read();
		} catch (Exception e) {
		}
	}

	public SimpleSpeedTest(String destination, int width, int height, int xPosition) {
		this.driver = new FirefoxDriver();
		this.destination = destination;
		this.width = width;
		this.height = height;
		this.xPosition = xPosition;
		Point p = new Point(this.xPosition, 0);

		// Reposition sometimes doesn't work otherwise
		try {
			Thread.sleep(2000);
		} catch (InterruptedException ex) {
			Thread.currentThread().interrupt();
		}
		this.driver.manage().window().setSize(new Dimension(this.width, this.height));
		this.driver.manage().window().setPosition(p);
	}

	public void run() {
		WebDriver d = this.driver;
		d.get(this.destination);
	}

	public void quit() {
		this.driver.quit();
	}

	public static void main(String[] args) throws InterruptedException {
		int urlCount = args.length;
		if (urlCount < 2) {
			System.out.println("Useage: java com.notessensei.speedtest.SimpleSpeedTest url1 url2 [url3]");
			System.exit(1);
		}

		// Get the screen size, leave some wiggle space
		Toolkit toolkit = Toolkit.getDefaultToolkit();
		java.awt.Dimension scrnsize = toolkit.getScreenSize();

		int height = (int) Math.round(scrnsize.getHeight() * 0.8);
		int width = (int) Math.round(scrnsize.getWidth() * 0.90 / urlCount);
		int pos = (int) Math.round(scrnsize.getWidth() / urlCount);

		Collection
 
 
 
  
  
   testers = new ArrayList
  
  
  
   
    (); // Loading the objects for (int i = 0; i < urlCount; i++) { int xPosition = (int) (pos * i); System.out.format("Running %s %s %s %s\n", args[i], width, height, xPosition); testers.add(new SimpleSpeedTest(args[i], width, height, xPosition)); } // Ready to go SimpleSpeedTest.waitEnter("Browsers are ready, press enter to start the test"); ExecutorService executor = Executors.newFixedThreadPool(urlCount); System.out.println("Tests on their way .."); for (SimpleSpeedTest sst : testers) { executor.execute(sst); } // Tear down executor.shutdown(); executor.awaitTermination(1, TimeUnit.HOURS); SimpleSpeedTest.waitEnter("Tests done, press Enter to continue"); for (SimpleSpeedTest sst : testers) { sst.quit(); } System.out.println("Done"); } } 
  
  
 
 
 

As usual YMMV.

Posted by on 15 February 2014 | Comments (0) | categories: Software

Comments

  1. No comments yet, be the first to comment