测试sql 语句执行时间

#-*-coding:utf-8-*-

import sys
import os
import time
import traceback
import MySQLdb as sqldb
from log import tracelog


query_list = [
"select count(*) from T_FieldInfo,T_FamTableDescInfo,T_DataEleDesc;",            
]

def getconn():
    host = '10.1.81.164'
    user = 'db_user'
    passwd = 'db_passwd'
    dbname = 'db_name'
    return sqldb.connect(host,user,passwd,dbname)

def query_time(cursor,query):
    cursor.execute("flush hosts;")  #这个加入的是清空mysq缓存的命令,但是我还没有找到这个命令
    time.sleep(10)
   
    begintime = time.time()
    cursor.execute(query)
    endtime = time.time()
    spantime = endtime - begintime
    note = "sql:%s  spantime:%s" %(query,str(spantime))
    return note

def openmysqport():
    strcmd=r'plink root@%s -pw %s "iptables -D INPUT -i bond1 -p tcp --dport 3306 -j DROP " ' %('10.1.81.164','root_passwd')
    os.system(strcmd) 
       

def main():
    tracelog.setLogFileName(r"./query_time_st.txt")
    tracelog.log("======sql test begin==========")
   
    try:
        conn = getconn()
        cursor = conn.cursor()
        for query in query_list:
            qnote = query_time(cursor,query)
            tracelog.log(qnote)
    except:
        expmsg = ''.join(traceback.format_exception(*sys.exc_info()))
        tracelog.log(expmsg)
    finally:
        cursor.close()
        conn.close()
       
    tracelog.log("======sql test end==========")
   

if __name__ == '__main__':
    main()

posted on 2014-09-05 13:55  leafs  阅读(245)  评论(0编辑  收藏  举报

导航