Split grinder scripts into modules

View: New views
8 Messages — Rating Filter:   Alert me  

Split grinder scripts into modules

by Satheesh Babu Vattekkat :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Using Grinder 3.2 - very nice. I prefer writing my own scripts than using the recorder, so over the last two days I got couple of nicely working flows automated. Both these flows have lot of common functions. What I would like to do is to move the common functions out to a separate python file - say myutils.py. Then, import myutils.py into my main script and use functions as myutils.utilitiy(p1, p2). 

However, when I do that, I ran into couple of issues:
1) My directory is like src/applicationcode/myutils.py, src/applicationcode/grinder_flow_1.py. It says cannot find path src/applicationcode/myutils.py. That I solved by putting empty __init__.py into src and applicationcode directories (ugly, but works for now!)

2) Second problem is in grinder_flow_1.py, when I import myutils.py, it fails saying NameError TestRunner. I don't want to put a TestRunner class in myutils.py. How do I get around this?

Thanks
Babu

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
grinder-use mailing list
grinder-use@...
https://lists.sourceforge.net/lists/listinfo/grinder-use

Re: Split grinder scripts into modules

by Philip Aston-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Satheesh Babu Vattekkat wrote:

> Using Grinder 3.2 - very nice. I prefer writing my own scripts than
> using the recorder, so over the last two days I got couple of nicely
> working flows automated. Both these flows have lot of common
> functions. What I would like to do is to move the common functions out
> to a separate python file - say myutils.py. Then, import myutils.py
> into my main script and use functions as myutils.utilitiy(p1, p2).
>
> However, when I do that, I ran into couple of issues:
> 1) My directory is like src/applicationcode/myutils.py,
> src/applicationcode/grinder_flow_1.py. It says cannot find path
> src/applicationcode/myutils.py. That I solved by putting empty
> __init__.py into src and applicationcode directories (ugly, but works
> for now!)

That's correct - its a python requirement. (It occasionally annoys me too).

>
> 2) Second problem is in grinder_flow_1.py, when I import myutils.py,
> it fails saying NameError TestRunner. I don't want to put a TestRunner
> class in myutils.py. How do I get around this?

Please post the stack trace, and the bit of code that is failing.

- Phil

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
grinder-use mailing list
grinder-use@...
https://lists.sourceforge.net/lists/listinfo/grinder-use

Re: Split grinder scripts into modules

by Satheesh Babu Vattekkat :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Philip,

Thanks. The source and logs are below.

Test script snippet:
=================================
#
# Just do login as two  users
# vim: ts=2:et:sw=2:ai:sm:nolist:foldmethod=indent:
#
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

import src.ui.yfcutils as yfcutils
import src.ui.memmgmt as memmgmt

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

  def __init__(self):
    pass
   
  def __call__(self):
    yfcutils.log("agentID %s, processID %s, threadID %s" % (agentID, processID, grinder.threadNumber))
    if (len(memmgmt.userlist) > grinder.threadNumber):
      memmgmt.doGetLoginPage()
      ...

yfcutils snippet:
=================================
#
#
# vim: ts=2:et:sw=2:ai:sm:nolist:foldmethod=indent:
#

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
import re


log = grinder.logger.output
error = grinder.logger.error

def valueFromTextInput(inputname, html):
  """Gets the first value that matches the input name. Assumes that HTML tag is made so that type, name, value attributes are specified in that order"""
  import re
  rx = re.compile(r'''<input\s+type=['"]text['"]\s+.*?\s+name=["']%s['"]\s+.*?\s+value=['"](.*?)['"].*?/>''' % inputname, re.MULTILINE|re.IGNORECASE)
  m = rx.findall(html)
  for a in m:
    return a
  return ''
...

Log file output:
============================================
8/21/09 12:24:05 PM (process laptop-0): The Grinder version 3.2
8/21/09 12:24:05 PM (process laptop-0): Sun Microsystems Inc. Java HotSpot(TM) Client VM 1.5.0_11-b03 on Windows XP x86 5.1
8/21/09 12:24:05 PM (process laptop-0): time zone is IST (+0530)
8/21/09 12:24:07 PM (process laptop-0): worker process 0 of agent number 0
8/21/09 12:24:07 PM (process laptop-0): executing "src\ui\test_memmgmt.py" using Jython 2.2.1
8/21/09 12:24:10 PM (process laptop-0): registered plug-in net.grinder.plugin.http.HTTPPlugin
8/21/09 12:24:12 PM (process laptop-0): ERROR ("Error running worker..."), see error log for details

Error file output:
===============================================
8/21/09 12:24:12 PM (process laptop-0): Error running worker process (NameError: TestRunner
File "S:\loadtest\.\laptop-file-store\current\src\ui\yfcutils.py", line 32, in ?
File "S:\loadtest\.\laptop-file-store\current\src\ui\test_memmgmt.py", line 10, in ?)
NameError: TestRunner
File "S:\loadtest\.\laptop-file-store\current\src\ui\yfcutils.py", line 32, in ?
File "S:\loadtest\.\laptop-file-store\current\src\ui\test_memmgmt.py", line 10, in ?


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
grinder-use mailing list
grinder-use@...
https://lists.sourceforge.net/lists/listinfo/grinder-use

Re: Split grinder scripts into modules

by Philip Aston-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Can you supply the whole of ycutils.py, or at least the bits around line 32?

- Phil

Satheesh Babu Vattekkat wrote:

> Philip,
>
> Thanks. The source and logs are below.
>
> Test script snippet:
> =================================
> #
> # Just do login as two  users
> # vim: ts=2:et:sw=2:ai:sm:nolist:foldmethod=indent:
> #
> 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
>
> import src.ui.yfcutils as yfcutils
> import src.ui.memmgmt as memmgmt
>
> class TestRunner:
>   """A TestRunner instance is created for each worker thread."""
>
>   def __init__(self):
>     pass
>    
>   def __call__(self):
>     yfcutils.log("agentID %s, processID %s, threadID %s" % (agentID,
> processID, grinder.threadNumber))
>     if (len(memmgmt.userlist) > grinder.threadNumber):
>       memmgmt.doGetLoginPage()
>       ...
>
> yfcutils snippet:
> =================================
> #
> #
> # vim: ts=2:et:sw=2:ai:sm:nolist:foldmethod=indent:
> #
>
> 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
> import re
>
>
> log = grinder.logger.output
> error = grinder.logger.error
>
> def valueFromTextInput(inputname, html):
>   """Gets the first value that matches the input name. Assumes that
> HTML tag is made so that type, name, value attributes are specified in
> that order"""
>   import re
>   rx =
> re.compile(r'''<input\s+type=['"]text['"]\s+.*?\s+name=["']%s['"]\s+.*?\s+value=['"](.*?)['"].*?/>'''
> % inputname, re.MULTILINE|re.IGNORECASE)
>   m = rx.findall(html)
>   for a in m:
>     return a
>   return ''
> ...
>
> Log file output:
> ============================================
> 8/21/09 12:24:05 PM (process laptop-0): The Grinder version 3.2
> 8/21/09 12:24:05 PM (process laptop-0): Sun Microsystems Inc. Java
> HotSpot(TM) Client VM 1.5.0_11-b03 on Windows XP x86 5.1
> 8/21/09 12:24:05 PM (process laptop-0): time zone is IST (+0530)
> 8/21/09 12:24:07 PM (process laptop-0): worker process 0 of agent number 0
> 8/21/09 12:24:07 PM (process laptop-0): executing
> "src\ui\test_memmgmt.py" using Jython 2.2.1
> 8/21/09 12:24:10 PM (process laptop-0): registered plug-in
> net.grinder.plugin.http.HTTPPlugin
> 8/21/09 12:24:12 PM (process laptop-0): ERROR ("Error running
> worker..."), see error log for details
>
> Error file output:
> ===============================================
> 8/21/09 12:24:12 PM (process laptop-0): Error running worker process
> (NameError: TestRunner
> File "S:\loadtest\.\laptop-file-store\current\src\ui\yfcutils.py",
> line 32, in ?
> File "S:\loadtest\.\laptop-file-store\current\src\ui\test_memmgmt.py",
> line 10, in ?)
> NameError: TestRunner
> File "S:\loadtest\.\laptop-file-store\current\src\ui\yfcutils.py",
> line 32, in ?
> File "S:\loadtest\.\laptop-file-store\current\src\ui\test_memmgmt.py",
> line 10, in ?
>


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
grinder-use mailing list
grinder-use@...
https://lists.sourceforge.net/lists/listinfo/grinder-use

Re: Split grinder scripts into modules

by Satheesh Babu Vattekkat :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Full file is below - could it be that I am including instrumentMethod here that is causing this issue?:
#
#
# vim: ts=2:et:sw=2:ai:sm:nolist:foldmethod=indent:
#

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
import re


log = grinder.logger.output
error = grinder.logger.error

def valueFromTextInput(inputname, html):
  """Gets the first value that matches the input name. Assumes that HTML tag is made so that type, name, value attributes are specified in that order"""
  import re
  rx = re.compile(r'''<input\s+type=['"]text['"]\s+.*?\s+name=["']%s['"]\s+.*?\s+value=['"](.*?)['"].*?/>''' % inputname, re.MULTILINE|re.IGNORECASE)
  m = rx.findall(html)
  for a in m:
    return a
  return ''

def getYFCPageToken(html):
  """Gets the page token"""
  m = yfc_page_token.findall(html) #findall is a bit wasteful - TODO: change to match
  for a in m:
    return a
  return ''

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)


def debug_object(o, msg=""):
  log("{" + msg + "{{")
  log("|".join(dir(o)))
  log("}}" + msg + "}")

yfc_page_token = re.compile(r'''<input\s+name='YFC_PAGE_TOKEN'\s+type='hidden'\s+value='(.*?)' />''', re.MULTILINE)

connectionDefaults = HTTPPluginControl.getConnectionDefaults()
httpUtilities = HTTPPluginControl.getHTTPUtilities()
# To use a proxy server, uncomment the next line and set the host and port.
#connectionDefaults.setProxyServer("127.0.0.1", 80)

# Initialization for the worker process

connectionDefaults.defaultHeaders = (
  NVPair('User-Agent', 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648; InfoPath.1)'),
  NVPair('Accept-Encoding', 'gzip, deflate'),
  NVPair('Accept-Language', 'en-us'),
  )

headers_common= \
  ( NVPair('Accept', '*/*'),
  NVPair('Cache-Control', 'no-cache'), )

agentID = int(grinder.properties["grinder.agentID"])
processID = int(grinder.processName.split("-").pop())


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
grinder-use mailing list
grinder-use@...
https://lists.sourceforge.net/lists/listinfo/grinder-use

Re: Split grinder scripts into modules

by Philip Aston-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

The problem is the default parameter in the instrumentMethod function:

    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)


I suggest you remove it:

    def instrumentMethod(test, method_name, c):
      """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)


If you do this, you will need to supply the appropriate TestRunner class
in any call to instrumentMethod.

   instrumentMethod(myTest, "myTestMethod", TestRunner)

- Phil


Satheesh Babu Vattekkat wrote:

> Full file is below - could it be that I am including instrumentMethod
> here that is causing this issue?:
> #
> #
> # vim: ts=2:et:sw=2:ai:sm:nolist:foldmethod=indent:
> #
>
> 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
> import re
>
>
> log = grinder.logger.output
> error = grinder.logger.error
>
> def valueFromTextInput(inputname, html):
>   """Gets the first value that matches the input name. Assumes that
> HTML tag is made so that type, name, value attributes are specified in
> that order"""
>   import re
>   rx =
> re.compile(r'''<input\s+type=['"]text['"]\s+.*?\s+name=["']%s['"]\s+.*?\s+value=['"](.*?)['"].*?/>'''
> % inputname, re.MULTILINE|re.IGNORECASE)
>   m = rx.findall(html)
>   for a in m:
>     return a
>   return ''
>
> def getYFCPageToken(html):
>   """Gets the page token"""
>   m = yfc_page_token.findall(html) #findall is a bit wasteful - TODO:
> change to match
>   for a in m:
>     return a
>   return ''
>
> 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)
>
>
> def debug_object(o, msg=""):
>   log("{" + msg + "{{")
>   log("|".join(dir(o)))
>   log("}}" + msg + "}")
>
> yfc_page_token =
> re.compile(r'''<input\s+name='YFC_PAGE_TOKEN'\s+type='hidden'\s+value='(.*?)'
> />''', re.MULTILINE)
>
> connectionDefaults = HTTPPluginControl.getConnectionDefaults()
> httpUtilities = HTTPPluginControl.getHTTPUtilities()
> # To use a proxy server, uncomment the next line and set the host and
> port.
> #connectionDefaults.setProxyServer("127.0.0.1", 80)
>
> # Initialization for the worker process
>
> connectionDefaults.defaultHeaders = (
>   NVPair('User-Agent', 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT
> 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR
> 3.0.04506.30; .NET CLR 3.0.04506.648; InfoPath.1)'),
>   NVPair('Accept-Encoding', 'gzip, deflate'),
>   NVPair('Accept-Language', 'en-us'),
>   )
>
> headers_common= \
>   ( NVPair('Accept', '*/*'),
>   NVPair('Cache-Control', 'no-cache'), )
>
> agentID = int(grinder.properties["grinder.agentID"])
> processID = int(grinder.processName.split("-").pop())
>


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
grinder-use mailing list
grinder-use@...
https://lists.sourceforge.net/lists/listinfo/grinder-use

Re: Split grinder scripts into modules

by Satheesh Babu Vattekkat :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks! That seems to do it - some more errors are coming; but those are pure python issues in terms of modules/import etc - that I should be able to solve.

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
grinder-use mailing list
grinder-use@...
https://lists.sourceforge.net/lists/listinfo/grinder-use

Re: Split grinder scripts into modules

by kamalakarv :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi satheesh,

I am getting error regarding import .

Below is my project structure is like this..



Issue::
when i ran my "Hello.py" i am getting error saying "ImportError: no module named util"
 -- I have added _init_.py as u specified in the previous reply.. i am facing same problem
 
Hello.py:
=========


from java.lang import System
from net.grinder.script.Grinder import grinder
from util.UserBean import UserBean

class TestRunner:

    # This method is called for every run.
    def __call__(self):
        print "in call method"    
        print grinder.getThreadNumber()
        print grinder.threadNumber
        test=UserBean(grinder.threadNumber)
        print test.getUserName()

    def __init__(self):
        print "in init Method"



     

     




Satheesh Babu Vattekkat wrote:
Thanks! That seems to do it - some more errors are coming; but those are
pure python issues in terms of modules/import etc - that I should be able to
solve.

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
grinder-use mailing list
grinder-use@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/grinder-use