09
2013-04

python并发


作者:容易 2013-04-09  17:34:32


使用Python多进程对oracle进行简单的压力测试,random_sql_type可根据实际需求定义,随机生成。

 


import time,cx_Oracle,multiprocessing,random,string 
import sys
class SqlToOracle:
    def m_sql(self,x,y,z):
        db_pool = cx_Oracle.SessionPool(user="syslog",password="oracle",dsn="192.168.0.205:1521/pnrdb",min=2,max=5,increment=1)
        conn = db_pool.acquire()
        for i in xrange(x):
            sql_type=self.random_sql_type()
            sql=self.random_sql(sql_type,y,z)
           # conn = db_pool.acquire()
            cursor = conn.cursor()
            cursor.prepare(sql[0]) 
            cursor.execute(None,sql[1])
            conn.commit()
            cursor.close()
        db_pool.release(conn)
    def random_sql_type(self):
        sql_insert=("insert into blog (category_id,sub_category_id,author_name,name,price,content,repertory) values (:category_id,:sub_category_id,:author_name,:name,:price,:content,:repertory)","insert")
        return sql_insert
    def random_sql(self,sql_type,y,z):
        if sql_type[1] == 'id':
                values={}
                values['id']=random.randint(100,100000)
                return (sql_type[0],values)
        elif sql_type[1] == 'name':
                values={}
                values['name']=str(random.choice(y)[0])
                return (sql_type[0],values)
        else:
                values={}
                cate_id=random.choice(y)
                values['category_id']=cate_id[0]
                values['sub_category_id']=cate_id[1]
                values['author_name']=random.choice(z)[0]
                values['name']=string.join(random.sample(['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','ora','py','dj','lin','net','nginx','http','ww','ftp','cdev','tig','bin','dns','ad','exchange','sql','dev','qq','sina','haha'], 2)).replace(" ","")
                values['price']=random.randint(1,100)
                values['content']=string.join(random.sample(['lin','net','nginx','http','ww','ftp','cdev','tig','bin','dns','ad','exchange','sql','dev','qq','sina','haha','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','ora','py','dj','lin','net','nginx','http','ww','ftp','cdev','tig','bin','dns','ad','exchange','sql','dev','qq','sina','haha'], 5)).replace(" ","")
                values['repertory']=random.randint(10000,100000)
                return (sql_type[0],values)
def gen_load(x,y,z):
    task=SqlToOracle()
    print "task start!"
    task.m_sql(x,y,z)
if __name__ == "__main__": 
        db_pool = cx_Oracle.SessionPool(user="syslog",password="oracle",dsn="192.168.0.205/pnrdb",min=2,max=5,increment=1)  
        pro_pool = multiprocessing.Pool(processes=51)
        manager = multiprocessing.Manager()
        manager1 = multiprocessing.Manager()
        conn = db_pool.acquire()
        cursor = conn.cursor()
        cate_sql='select category_id,id from sub_category'
        qq='hahah'
        cursor.execute(cate_sql)
        cate_set=cursor.fetchall()
        #cate_set=[('a','c'),('b','l')]
        cursor.close()
        db_pool.release(conn)
        conn = db_pool.acquire()
        cursor = conn.cursor()
        sql_author='select name from author'
        cursor.execute(sql_author)
        author_set=cursor.fetchall()
        #author_set=[('c',),('d',)]
        cursor.close()
        db_pool.release(conn)
        m_cate_set=manager.list(cate_set)
        m_author_set=manager.list(author_set)
#       gen_load(10,cate_set,author_set)
        for i in xrange(50):  
                pro_pool.apply_async(gen_load,args=(10000,m_cate_set,m_author_set)) 
        pro_pool.close()
        pro_pool.join()
        print "insert into 0K!,sleep 60 seconds!"
        time.sleep(5)
        print "Sub-process(es) done."

 

One Response


    还没有评论!
1  

Leave your comment

请留下您的姓名(*)

请输入正确的邮箱地址(*)

请输入你的评论(*)


感谢开源 © 2016. All rights reserved.&3Q Open Source&^_^赣ICP备15012863号-1^_^
乐于分享共同进步 KreativeThemes