In this tutorial we will see how to use Turbulence to performance test a wordpress site. In order to speed installation we will use the official wordpress docker image and deploy it locally, but these steps are applicable to applications deployed on servers; just change the target url!
First, make sure docker is installed and working, and then run:
docker run -d -e MYSQL_ROOT_PASSWORD=unsafepassword --name mysql library/mysql
docker run -d --name wordpress --link mysql -p 8080:80 library/wordpress
Check that Wordpress has started successfully by opening a browser to http://localhost:8000/. Follow the installation wizard until the wordpress instance is configured and the site is available.
Once the Wordpress site is available, we can begin writing our performance test. Create a new, empty directory and create a file with the following content:
turbulence
.startUserSteps()
.get('http://localhost:8080/')
.expectStatus(200)
.assertResponse((XPath) => {
let nodes = XPath("//h1[contains(@class,'site-title')]/a/text()");
return nodes.length === 1 && nodes.pop().data === 'my-example-wordpress-site';
})
.randomPause(2000, 10000)
.concurrentUsers(10)
.duration(60000)
.endUserSteps()
.run()
Then run the test with this command:
trb /path/to/your/test/script
Once the test has finished, a Report.html
file should be created in the current directory. Turbulence will report the performance metrics gathered during the test and any errors or assertion failures encountered.
In our test script inside of the assertResponse
function we returned a true or false value to determine if the test should pass. This can be inconvenient when testing a large number of values, and many existing assertion frameworks for js may provide a more aesthetic and expressive way of testing. We can use third-party libraries easily in Turbulence. Start by installing the Chai library globally:
npm install -g chai
Turbulence modifies the $NODE_PATH
to allow dependencies to be required from global and local directories. For more details on how this works, check the official node documentation.
Now in our test script we can add the Chai assertions.
let chai = require('chai');
let expect = chai.expect;
turbulence
.startUserSteps()
.get('http://localhost:8080/')
.expectStatus(200)
.assertResponse((XPath) => {
let nodes = XPath("//h1[contains(@class,'site-title')]/a/text()");
expect(nodes).to.have.lengthOf(1);
expect(nodes.pop().data).to.equal('my-example-wordpress-site');
})
.randomPause(2000, 10000)
.concurrentUsers(10)
.duration(60000)
.endUserSteps()
.run()
Because assertion steps can be failed by either returning false explicitly or via throwing an exception, most unit testing frameworks can be integrated easily via npm.