Python based stress tester

I often find myself building something in anticipation of needing it at some point.  This is one of those cases 🙂  Below is a template (a real working one for my needs) that you can edit to build a simple load testing tool.  We were having a performance problem with one SQL statement, and I wanted a quick and dirty way to verify possible solutions.

import cx_Oracle
import thread
import time
import sys
import random
import datetime
import calendar

#-------------------------------------------------------------------------------------------

pool = cx_Oracle.SessionPool(user="rsprod_arch",password="rsprod_arch",dsn="perfrac",min=50,max=70,increment=1,threaded=True)
instList = []

#-------------------------------------------------------------------------------------------

def getRequest():
  con = pool.acquire();
  cursor = con.cursor()
  cursor.execute("alter session set events '10046 trace name context forever, level 12'")
  pool.release(con)
  loops = 0
  while loops < 5000:
    loops = loops + 1
    con = pool.acquire();
    cursor = con.cursor()
    cursor.arraysize = 50
    instNum=random.randrange(1, len(instList))
    today = datetime.date.today()
    daydiff = datetime.timedelta(days=random.randrange(1,365))
    cursor.execute("select * from ( select request0_.requestId as x0_0_ from Request request0_ where ((request0_.requesterSymbol=:b1 )or(request0_.SupplierSymbol=:b2 ))and(request0_.closedDate=:b3 ) order by  request0_.requestId ) where rownum <= :b4",
                    b1 = instList[instNum],
                    b2 = instList[instNum],
                    b3 = today - daydiff,
                    b4 = 1)
           
    for row in cursor.fetchall():
      print str(row[0]) + " " + str(today - daydiff) + " " + instList[instNum]
    
    #pooling mechanism doesn't close our cursors, so we must close before we return logical connection to physical connection pool.
    cursor.close()

    pool.release(con)

#-------------------------------------------------------------------------------------------

con = pool.acquire();
cursor = con.cursor()
cursor.execute("alter session set events '10046 trace name context forever, level 12'")
cursor.arraysize = 50
cursor.execute("select symbol from rs_prod.institution sample (1)"); 

for symbol in cursor.fetchall():
  instList.append(symbol[0])
  
#-------------------------------------------------------------------------------------------

for _ in range(50):
  thread.start_new_thread(getRequest, ())

raw_input('enter to close')

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.