Writing Performance Tests in Grinder using a Framework

by vivin

LoginTask

LoginTask is a relatively simple task. All it does is log into the application. It looks like this:

LoginTask.py

# Converted from XML to Jython by xmlToJython.pl on 19:33:46, Thu Jun 25, 2009
# The Grinder 3.2
# HTTP script recorded by TCPProxy at 2009-06-25T14:17:56.771-07:00

from torqueo.test.framework.Task import Task
from HTTPClient import NVPair
from net.grinder.plugin.http import HTTPPluginControl, HTTPRequest
from net.grinder.script import Test
from net.grinder.script.Grinder import grinder

class LoginTask(Task):

      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 of the class are Class Variables (as opposed to
      # Instance Variables) and are shared between all instances of this class.

      connectionDefaults.defaultHeaders = \
          (
            NVPair('User-Agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.11) Gecko/2009060308 Ubuntu/9.04 (jaunty) Firefox/3.0.11'),
            NVPair('Accept-Encoding', 'gzip,deflate'),
            NVPair('Accept-Language', 'en-us,en;q=0.5'),
            NVPair('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.7'),
          )

      headers0 = \
          (
            NVPair('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'),
          )

      headers1 = \
          (
            NVPair('Accept', 'image/png,image/*;q=0.8,*/*;q=0.5'),
            NVPair('Referer', 'https://local.sitetotest.com:8443/'),
          )

      headers2 = \
          (
            NVPair('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'),
            NVPair('Referer', 'https://local.sitetotest.com:8443/'),
          )

      headers3 = \
          (
            NVPair('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'),
            NVPair('Referer', 'https://local.sitetotest.com:8443/Admin/home.jsp'),
          )

      headers4 = \
          (
            NVPair('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'),
            NVPair('Referer', 'https://local.sitetotest.com:8443/Admin/home.jsp'),
            NVPair('Cache-Control', 'no-cache'),
          )

      headers5 = \
          (
            NVPair('Accept', 'text/css,*/*;q=0.1'),
            NVPair('Referer', 'https://local.sitetotest.com:8443/Admin/home.jsp'),
          )

      def __init__(self):
          """Initialize properties of class"""
          Task.__init__(self)
          self.description = "Log into the app"
          self.urlDict = {}
          self.taskId = Task.numberOfTasks

      def initializeTask(self):
          """Initializes Instance Variables for this class. This method will be called by the Scenario object that this task belongs to."""
          if(not self.urlDict.has_key("url0")):
             raise Exception(self.__class__.__name__ + ".urlDict is missing values for one or more of the following keys: [url0]. Please define them in the constructor for the parent Scenario.")
          else:
             self.request101 = HTTPRequest(url=self.urlDict["url0"], headers=LoginTask.headers0)
             self.request101 = Test(int(str(self.taskId) + str(101)), "Log into the app: GET /").wrap(self.request101)

             self.request102 = HTTPRequest(url=self.urlDict["url0"], headers=LoginTask.headers1)
             self.request102 = Test(int(str(self.taskId) + str(102)), "Log into the app: GET torqueo-crm.gif").wrap(self.request102)

             self.request103 = HTTPRequest(url=self.urlDict["url0"], headers=LoginTask.headers2)
             self.request103 = Test(int(str(self.taskId) + str(103)), "Log into the app: GET defaultLogin.jsp").wrap(self.request103)

             self.request104 = HTTPRequest(url=self.urlDict["url0"], headers=LoginTask.headers2)
             self.request104 = Test(int(str(self.taskId) + str(104)), "Log into the app: GET index.jsp").wrap(self.request104)

             self.request201 = HTTPRequest(url=self.urlDict["url0"], headers=LoginTask.headers2)
             self.request201 = Test(int(str(self.taskId) + str(201)), "Log into the app: POST processLogin.jsp").wrap(self.request201)

             self.request202 = HTTPRequest(url=self.urlDict["url0"], headers=LoginTask.headers2)
             self.request202 = Test(int(str(self.taskId) + str(202)), "Log into the app: GET home.jsp").wrap(self.request202)

             self.request301 = HTTPRequest(url=self.urlDict["url0"], headers=LoginTask.headers3)
             self.request301 = Test(int(str(self.taskId) + str(301)), "Log into the app: GET popUpTask.jsp").wrap(self.request301)

             self.request401 = HTTPRequest(url=self.urlDict["url0"], headers=LoginTask.headers4)
             self.request401 = Test(int(str(self.taskId) + str(401)), "Log into the app: POST contextSensitiveHelpProxy").wrap(self.request401)

             self.request402 = HTTPRequest(url=self.urlDict["url0"], headers=LoginTask.headers5)
             self.request402 = Test(int(str(self.taskId) + str(402)), "Log into the app: GET contextSensitiveHelpProxy").wrap(self.request402)

             self.request501 = HTTPRequest(url=self.urlDict["url0"], headers=LoginTask.headers4)
             self.request501 = Test(int(str(self.taskId) + str(501)), "Log into the app: POST calendarBackend.jsp").wrap(self.request501)

             self.parameters = \
             {
                 "appLogin1":
                 {
                     "104":
                     {
                         "msg":"Whoa,+easy+there+tiger.+You're+gonna+need+to+login+before+you+can+view+this+page."
                     }
                 },
                 "appLogin2":
                 {
                     "201":
                     {
                         "password":"[email protected]",
                         "Login":"Login",
                         "username":"vivin"
                     }
                 },
                 "appLogin4":
                 {
                     "401":
                     {
                         "url":"/home.html"
                     },
                     "402":
                     {
                         "url":"/data/skins/techjunkie/css/TechJunkieStripped.css"
                     }
                 },
                 "appLogin5":
                 {
                     "501":
                     {
                         "calDate":"25",
                         "userId":"1",
                         "weekEndDate":"-1",
                         "weekEndMonth":"-1",
                         "weekStartDate":"-1",
                         "weekEndYear":"-1",
                         "weekStartYear":"-1",
                         "weekStartMonth":"-1",
                         "calMonth":"5",
                         "calType":"Day",
                         "calYear":"2009"
                     }
                 }
             }

             self.instrumentMethod(Test(int(str(self.taskId) + str(100)), 'Log into the app'), 'appLogin1')
             self.instrumentMethod(Test(int(str(self.taskId) + str(200)), 'Log into the app'), 'appLogin2')
             self.instrumentMethod(Test(int(str(self.taskId) + str(300)), 'Log into the app'), 'appLogin3')
             self.instrumentMethod(Test(int(str(self.taskId) + str(400)), 'Log into the app'), 'appLogin4')
             self.instrumentMethod(Test(int(str(self.taskId) + str(500)), 'Log into the app'), 'appLogin5')

      def appLogin1(self):
          """Log into the app GET index.jsp (requests 101-104)."""
          result = self.request101.GET('/')

          self.request102.GET('/slices/torqueo-crm.gif')
          # Expecting 302'Moved Temporarily'
          grinder.sleep(219)

          self.request103.GET('/login/defaultLogin.jsp')
          # Expecting 302'Moved Temporarily'
          self.token_msg = LoginTask.httpUtilities.valueFromLocationURI('msg') # Whoa,+easy+there+tiger.+You're+gonna+need+to+login+before+you+can+view+this+page.

          grinder.sleep(14)

          self.request104.GET('/index.jsp'+
              '?msg=' + self.parameters["appLogin1"]["104"]["msg"]
          )

          grinder.sleep(13)

          return result

      def appLogin2(self):
          """Log into the app GET home.jsp (requests 201-202)."""
          result = self.request201.POST('/login/processLogin.jsp',
              (
                NVPair('password', self.parameters["appLogin2"]["201"]["password"]),
                NVPair('Login', self.parameters["appLogin2"]["201"]["Login"]),
                NVPair('username', self.parameters["appLogin2"]["201"]["username"]),
              ),
              ( NVPair('Content-Type', 'application/x-www-form-urlencoded'), )
          )
          # Expecting 302'Moved Temporarily'
          self.request202.GET('/Admin/home.jsp')

          grinder.sleep(27)

          return result

      def appLogin3(self):
          """Log into the app GET popUpTask.jsp (request 301)."""
          result = self.request301.GET('/files/popUpTask.jsp' + '?0.5711548512452386')

          return result

      def appLogin4(self):
          """Log into the app GET contextSensitiveHelpProxy (requests 401-402)."""
          result = self.request401.POST('/contextSensitiveHelpProxy',
              (
                NVPair('url', self.parameters["appLogin4"]["401"]["url"]),
              ),
              ( NVPair('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'), )
          )

          self.request402.GET('/contextSensitiveHelpProxy'+
              '?url=' + self.parameters["appLogin4"]["402"]["url"]
          )

          grinder.sleep(224)

          return result

      def appLogin5(self):
          """Log into the app POST calendarBackend.jsp (request 501)."""
          result = self.request501.POST('/Calendar/calendarBackend.jsp',
              (
                NVPair('calDate', self.parameters["appLogin5"]["501"]["calDate"]),
                NVPair('userId', self.parameters["appLogin5"]["501"]["userId"]),
                NVPair('weekEndDate', self.parameters["appLogin5"]["501"]["weekEndDate"]),
                NVPair('weekEndMonth', self.parameters["appLogin5"]["501"]["weekEndMonth"]),
                NVPair('weekStartDate', self.parameters["appLogin5"]["501"]["weekStartDate"]),
                NVPair('weekEndYear', self.parameters["appLogin5"]["501"]["weekEndYear"]),
                NVPair('weekStartYear', self.parameters["appLogin5"]["501"]["weekStartYear"]),
                NVPair('weekStartMonth', self.parameters["appLogin5"]["501"]["weekStartMonth"]),
                NVPair('calMonth', self.parameters["appLogin5"]["501"]["calMonth"]),
                NVPair('calType', self.parameters["appLogin5"]["501"]["calType"]),
                NVPair('calYear', self.parameters["appLogin5"]["501"]["calYear"]),
              ),
              ( NVPair('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'), )
          )

          return result

      def run(self):
          """The run() method runs all the tests in this Task"""
          self.callParameterizingMethodFor('appLogin1')
          self.appLogin1()

          self.callParameterizingMethodFor('appLogin2')
          self.appLogin2()
          grinder.sleep(1584)

          self.callParameterizingMethodFor('appLogin3')
          self.appLogin3()
          grinder.sleep(364)

          self.callParameterizingMethodFor('appLogin4')
          self.appLogin4()
          grinder.sleep(54)

          self.callParameterizingMethodFor('appLogin5')
          self.appLogin5()
          grinder.sleep(155)