2017-08-08 DBA日记,使用python模拟高并发访问数据库

一、目的

1.1 简单快速完成定制高并发模拟

1.2 监控高并发情况数据库的资源表现

1.3 观察不同的高并发数对数据库的影响

二、设计思路

2.1  利用python的多线程包,模拟高并发数据库操作,并监控相关资源使用。

三、代码

#-*-coding=utf8 -*-
import  threading
import  cx_Oracle
import  time
import  paramiko
#全局变量,用于控制监控进程的退出
T_status = True  
def  CC_ins():
     try :
         conn = cx_Oracle.connect( 'dbuser' , 'password' , 'Tnsnames' )
         cur = conn.cursor()
         #cur.execute('call dm.qqtproc1()')
         cur.callproc( 'dm.qqtproc1' )
     except  Exception as e:
         print  e
         print  'ora'
     else :
         print  'insert successful!'
#采集数据库事件函数
def  Mon_event():
     global  T_status
     try :
         conn = cx_Oracle.connect( 'dbuser' , 'password' , 'Tnsnames' )
         cur = conn.cursor()
     except  Exception as e:
         print  e
     while  T_status = = True :
         try :
             print  T_status
             cur.execute( 'select event from gv$session where username=\'HQQ\'' )
             rows = cur.fetchall()
             for  row  in  rows:
                 print  row
             #cur.callproc('dm.qqtproc1')
         except  Exception as e:
             print  e
             print  'ora'
         else :
             print  'successful!'        
         time.sleep( 1 )
#采集os资源的函数
def  Mon_os():
     global  T_status
     try :
         ssh = paramiko.SSHClient()
         ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
         ssh.connect(username = 'osuser' ,password = 'ospassword' ,hostname = 'os_ip' ,port = 22 )
     except  Exception as e:
         print  e
     while  T_status = = True :      
         stdin,stdout,stderr = ssh.exec_command( 'sar 1 1' )
         lines = stdout.readlines()
         print  lines[ - 1 ]
         time.sleep( 1 )
 
if  __name__  = =  '__main__' :
     CC_qty = raw_input ( "please Concurrency QTY:" )
     #CC_qty='20'
     T_list = []
     if  CC_qty.isdigit() :
         for  in  range ( int (CC_qty)):
             t = threading.Thread(target = CC_ins,args = ())
             T_list.append(t)
     T_mon = threading.Thread(target = Mon_os,args = ())
     T_mon.start()
     for  t1  in  T_list:
         t1.start()
     for  t2  in  T_list:
         t2.join()
         T_status = False

 

四、加密

代码编写完后,我们可以用python -m py_compile xxx.py

五、效果展示

5.1 模拟30个用户同时向一张表循环插入记录,每插入一条记录,commit;一次

5.2 以下是采集sar数据记录。

六、扩展

我们可以同时采集不同的CPU,IO数据保存到数据库中,作为后续的分析。


  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值