{"id":324,"date":"2009-07-22T15:35:05","date_gmt":"2009-07-22T20:35:05","guid":{"rendered":"http:\/\/appcrawler.com\/wordpress\/?p=324"},"modified":"2011-07-06T09:40:29","modified_gmt":"2011-07-06T14:40:29","slug":"pthon-based-stress-tester","status":"publish","type":"post","link":"http:\/\/appcrawler.com\/wordpress\/2009\/07\/22\/pthon-based-stress-tester\/","title":{"rendered":"Python based stress tester"},"content":{"rendered":"<p>I often find myself building something in anticipation of needing it at some point.\u00a0 This is one of those cases \ud83d\ude42\u00a0 Below is a template (a real working one for my needs) that you can edit to build a simple load testing tool.\u00a0 We were having a performance problem with one SQL statement, and I wanted a quick and dirty way to verify possible solutions.<\/p>\n<pre lang=\"python\" line=\"1\">\r\nimport cx_Oracle\r\nimport thread\r\nimport time\r\nimport sys\r\nimport random\r\nimport datetime\r\nimport calendar\r\n\r\n#-------------------------------------------------------------------------------------------\r\n\r\npool = cx_Oracle.SessionPool(user=\"rsprod_arch\",password=\"rsprod_arch\",dsn=\"perfrac\",min=50,max=70,increment=1,threaded=True)\r\ninstList = []\r\n\r\n#-------------------------------------------------------------------------------------------\r\n\r\ndef getRequest():\r\n  con = pool.acquire();\r\n  cursor = con.cursor()\r\n  cursor.execute(\"alter session set events '10046 trace name context forever, level 12'\")\r\n  pool.release(con)\r\n  loops = 0\r\n  while loops < 5000:\r\n    loops = loops + 1\r\n    con = pool.acquire();\r\n    cursor = con.cursor()\r\n    cursor.arraysize = 50\r\n    instNum=random.randrange(1, len(instList))\r\n    today = datetime.date.today()\r\n    daydiff = datetime.timedelta(days=random.randrange(1,365))\r\n    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\",\r\n                    b1 = instList[instNum],\r\n                    b2 = instList[instNum],\r\n                    b3 = today - daydiff,\r\n                    b4 = 1)\r\n           \r\n    for row in cursor.fetchall():\r\n      print str(row[0]) + \" \" + str(today - daydiff) + \" \" + instList[instNum]\r\n    \r\n    #pooling mechanism doesn't close our cursors, so we must close before we return logical connection to physical connection pool.\r\n    cursor.close()\r\n\r\n    pool.release(con)\r\n\r\n#-------------------------------------------------------------------------------------------\r\n\r\ncon = pool.acquire();\r\ncursor = con.cursor()\r\ncursor.execute(\"alter session set events '10046 trace name context forever, level 12'\")\r\ncursor.arraysize = 50\r\ncursor.execute(\"select symbol from rs_prod.institution sample (1)\"); \r\n\r\nfor symbol in cursor.fetchall():\r\n  instList.append(symbol[0])\r\n  \r\n#-------------------------------------------------------------------------------------------\r\n\r\nfor _ in range(50):\r\n  thread.start_new_thread(getRequest, ())\r\n\r\nraw_input('enter to close')<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I often find myself building something in anticipation of needing it at some point.\u00a0 This is one of those cases \ud83d\ude42\u00a0 Below is a template (a real working one for my needs) that you can edit to build a simple&hellip;<\/p>\n<p class=\"more-link-p\"><a class=\"more-link\" href=\"http:\/\/appcrawler.com\/wordpress\/2009\/07\/22\/pthon-based-stress-tester\/\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"footnotes":""},"categories":[19,24,22,26],"tags":[],"_links":{"self":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/324"}],"collection":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/comments?post=324"}],"version-history":[{"count":8,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/324\/revisions"}],"predecessor-version":[{"id":944,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/324\/revisions\/944"}],"wp:attachment":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/media?parent=324"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/categories?post=324"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/tags?post=324"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}