一、目的
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数据保存到数据库中,作为后续的分析。