一、目的
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 i 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數據保存到數據庫中,作爲後續的分析。