Thursday, March 22, 2012

How to Test REST Web Service Using The Grinder

A simple and easy method of load testing a REST service using The Grinder open source load testing tool is as follows:
  • use the HttpRequest object
  • use the appropriate REST method on the HttpRequest object: 
    • Method=PUT
    • Method=GET
    • Method=DELETE


















The following script gives an example of this method in the case of REST PUT request using a JSON request message.  (XML request message would be similar).

Script

# The Grinder 3.7.1

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()

# headers
headers0= \
  [ ]

# Http URL
url0 = 'http://servername:8080'

# Create an HTTPRequest for each request, then replace the
request101 = HTTPRequest(url=url0, headers=headers0)
request101 = Test(101, 'PUT Test').wrap(request101)

request102 = Test(102, 'PUT Test 2').wrap(request101)

json1 = '''{
              \"key1\"        :  \"BD6632F8569D4D609BB8ED902E9F6585\",    
              \"key2\"        :  \"65EE35B00655100625F85498A6092F80\",    
              \"key3\"        :  \"984921672\",\r\n    
              \"id1\"         :  \"6\",\r\n    
              \"id2\"         :  \"1\",\r\n    
              \"descr\"       :  \"4724711\",\r\n    
        }'''
json2 = '''{   
              \"key4\"        :  \"6\",\r\n    
              \"key5\"        :  \"1\",\r\n    
              \"key6\"        :  \"0\",\r\n    
              \"key7\"        :  \"2\",\r\n    
              \"name1\"       :  \"Green Bay\",\r\n    
              \"name2\"    :  \"GRBay\",\r\n    
        }'''


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

  # A method for each recorded page.
  def page1(self):
    """PUT Test (request 101)."""
    result = request101.PUT('/Capture/Test', json1,   ( NVPair('Content-Type', 'application/json'), ))

    return result
# A method for each recorded page.
  def page2(self):
    """PUT Test (request 101)."""
    result = request102.PUT('/Capture/Test', json2,   ( NVPair('Content-Type', 'application/json'), ))

    return result

  def __call__(self):
    """Called for every run performed by the worker thread."""
    self.page1()      # PUT Test (request 101)
    self.page2()      # PUT Test (request 102)

Output

1 comment: