Sunday, June 17, 2012

Setting Up The Grinder under Windows

For this first tutorial I'm going to use Windows rather than Linux, as it is likely that most Grinder users are more familiar with Windows than Linux. I've created a Zip file with a full installation of the latest version of The Grinder 3.9.1 (as of writing), in C:\Grinder. The set up follows The Grinder User Guide closely, using standard Windows .cmd files. It also includes a lightweight HTTP server called "Mongoose" that supports server-side PHP, which I plan to use to develop more interesting test scripts in the future.

You can download the ZIP file from here. The only thing the Zip file does not contain is the Sun Java JRE. For the purposes of the tutorial we're going to use C:\Grinder as The Grinder installation folder. I'm going to assume you have already installed the Sun JRE and that Java's home (JAVA_HOME)  is C:\Windows\System32. If this is not the case you will have to change the C:\Grinder\setGrinderEnv.cmd command file to point to your JAVA_HOME.

Once you have everything downloaded, unzipped, and installed. Double-click on the command file C:\Grinder\startHttpd.cmd to start the Mongoose HTTP server on port 8080. A DOS command window should appear similar to the following, indicating that the Mongoose HTTP server is running:


You should then be able to see the "Hello World" page by going to the Mongoose root here. In Firefox you should hopefully see something similar to the following:



Note that the HTTP server tries to listen on port 8080. You may have to tell Windows Firewall to allow it to access the port, and also note that this is a development HTTP server that shouldn't be left running as it is likely very insecure. Only run it when you are working through the tutorial and when your PC is not on a public network. Email me at flying kiwi guy @ gmail.com (no spaces) if you have any problems.

The next step is to run the TCPProxy. The TCPProxy is a program that records each click you do when using Firefox to browse a website. The recording of clicks (actually HTTP requests) will be translated into Jython code that is directly executable by The Grinder. Double-click on  C:\Grinder\setGrinderEnv.cmd and you should see a the TCPProxy start up in a second DOS command window:



You will also see the TCPProxy Console window, similar to the following:


Note that the TCPProxy is configured to listen on port 8001. You need to change your browser settings in order for the TCPProxy to intercept ("proxy") the client HTTP requests to the HTTP server. In Firefox, choose Tools -> Options -> Advanced:


Then click on the Network tab and then Settings... and make the following changes:


You should confirm that the "No Proxy For:" is blank. Our HTTP server is running on on localhost:8080, and the TCPProxy is running on localhost:8001 (127.0.0.1 is the IP address for localhost). We want to proxy any Firefox request through the TCPProxy at localhost:8001. Let's see if we can now still access our Hello World by again clicking here. Not very exciting, but you should again see the same page:


What is different this time is that the TCPProxy should have recorded your Firefox click, and maybe some clicks to this blog too. Let's check. Click the Stop button in the TCPProxy Console window. The TCPProxy should have created the file C:\Grinder\scripts\grinder_recorded.py like so:


Open up this file and you should see some automatically generated code similar to the following:

# The Grinder 3.9.1
# HTTP script recorded by TCPProxy at 17-Jun-2012 16:39:01

from net.grinder.script import Test
from net.grinder.script.Grinder import grinder
from net.grinder.plugin.http import HTTPPluginControl, HTTPRequest
from HTTPClient import NVPair
connectionDefaults = HTTPPluginControl.getConnectionDefaults()
httpUtilities = HTTPPluginControl.getHTTPUtilities()

# To use a proxy server, uncomment the next line and set the host and port.
# connectionDefaults.setProxyServer("localhost", 8001)

# These definitions at the top level of the file are evaluated once,
# when the worker process is started.

url0 = 'http://localhost:8080'

# Create an HTTPRequest for each request, then replace the
# reference to the HTTPRequest with an instrumented version.
# You can access the unadorned instance using request101.__target__.
request101 = HTTPRequest(url=url0)
request101 = Test(101, 'GET /').wrap(request101)


class TestRunner:
  """A TestRunner instance is created for each worker thread."""

  # A method for each recorded page.
  def page1(self):
    """GET / (request 101)."""
    result = request101.GET('/', None,
      ( NVPair('User-Agent', 'Mozilla/5.0 (Windows NT 6.1; rv:12.0) Gecko/20100101 Firefox/12.0'),
        NVPair('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'),
        NVPair('Accept-Language', 'en-us,en;q=0.5'),
        NVPair('Accept-Encoding', 'gzip, deflate'),
        NVPair('If-Modified-Since', 'Sun, 17 Jun 2012 15:25:01 GMT'),
        NVPair('If-None-Match', '\"4fddf6cd.2d\"'), ))

    return result

  def __call__(self):
    """Called for every run performed by the worker thread."""
    self.page1()      # GET / (request 101)

def instrumentMethod(test, method_name, c=TestRunner):
  """Instrument a method with the given Test."""
  unadorned = getattr(c, method_name)
  import new
  method = new.instancemethod(test.wrap(unadorned), None, c)
  setattr(c, method_name, method)

# Replace each method with an instrumented version.
# You can call the unadorned method using self.page1.__target__().
instrumentMethod(Test(100, 'Page 1'), 'page1')

In the next post I will cover how to now execute your recorded script within The Grinder and obtain some performance test results on how the Mongoose HTTP server performs when it is serving our simple HTML page.

Remember, don't forget to disable the proxy settings in Firefox, if you plan to use Firefox for normal browsing! You might want to check out a useful Firefox plugin that can be used to easily change your proxy settings.

No comments:

Post a Comment