Related Entries

Quick Start: Git for personal use
SVN client over SSH to remote Unix server from Windows
Quick Start Grinder - Part V
Quick Start Grinder - Part IV
Quick Start Grinder - Part II

« New iPhone 3G
» Quick Start Grinder - Part IV

Quick Start Grinder - Part III

Grind on in parallel threads!

It is pretty easy to make our Google search multi-threaded. We will enhance the example a little bit to search Google in 3 threads; each searching for different words.

Parts I and II of the series.

Changes to the script

Very little changes are required on our TestRunner class. Example below initializes a list of three words we use to search on, in __init__ method. Now, we change the __call__ method to pick a word based on the thread number and search for that word.

class TestRunner:
  """A TestRunner instance is created for each worker thread."""
  def __init__(self):
    self.words_to_search = ["Grinder", "Federer", "Obama"]

  def __call__(self):
    """This method is called for every run performed by the worker thread."""
    log("agentID %s, processID %s, threadID %s" % (agentID, processID, grinder.threadNumber))
    #this is the first test - getting the search form
    #this is the second one - actually searching for data
    #depending on the thread, we will pickup the word to search for
    search_for = self.words_to_search[grinder.threadNumber%len(self.words_to_search)]

Once you’ve done this change, you will need to update your file too. Let us change the value of grinder.threads to 7. Now, as explained before, distribute files and run the tests again. Check the log files!

Grinder multithreaded run

Now, try to run increasing the grinder.runs property to 4. You might want to put a sleep(5000) as the last line of __call__ method. Now, the tests will be repeated 4 times, in 7 threads. Between each run, the script will just sit idle for 5 seconds.