【MySQL】计算 TPS,QPS 的python 脚本

本文详细介绍了如何通过Python脚本利用MySQL连接获取系统性能指标,包括每秒成功提交事务数(Com_commit)、删除数(Com_delete)、插入数(Com_insert)、回滚数(Com_rollback)、查询数(Com_select)和更新数(Com_update),以及每秒问题数(Questions)。通过计算这些指标的变化率,可以得到每秒提交事务数(TPS_Com_rol)、每秒插入/删除/更新数(TPS_iud)、每秒查询数(QPS_s)和每秒问题数(QPS_ques)。
#!/usr/bin/env python
#coding=utf-8
""" 以不同方式获取mysql中的tps,qps,并做对比 """
import time
import sys
import os
import MySQLdb
import dbconn
def main() :
    try: 
      conn = MySQLdb.connect(host=dbconn.DB_HOST,port=int(dbconn.DB_PORT),user=dbconn.DB_USER,passwd=dbconn.DB_PASS, charset='utf8')
    except  MySQLdb.ERROR,e:
      print "Error %d:%s"%(e.args[0],e.args[1])
      exit(1)

    conn.autocommit(True)
    cursor=conn.cursor()
    diff = 1
    mystat1={}
    mystat2={}
    sql = "show global status where Variable_name in ('Com_commit','Com_delete','Com_insert','Com_rollback','Com_select','Com_update','Questions');"
    while True:
       try :
          cursor.execute(sql)
          results1 = cursor.fetchall()
          mystat1=dict(results1)
  
          time.sleep(diff)
          cursor.execute(sql)
          results2 = cursor.fetchall()
          mystat2=dict(results2)

          Com_diff = (int(mystat2['Com_commit'])   - int(mystat1['Com_commit']) ) / diff 
          del_diff = (int(mystat2['Com_delete'])   - int(mystat1['Com_delete']) ) / diff
          ins_diff = (int(mystat2['Com_insert'])   - int(mystat1['Com_insert']) ) / diff
          rol_diff = (int(mystat2['Com_rollback']) - int(mystat1['Com_rollback']))/ diff
          sel_diff = (int(mystat2['Com_select'])   - int(mystat1['Com_select']) ) / diff
          upd_diff = (int(mystat2['Com_update'])   - int(mystat1['Com_update']) ) / diff
          que_diff = (int(mystat2['Questions'])    - int(mystat1['Questions']) )  / diff
          qps_s = sel_diff
          tps_iud = del_diff+ins_diff+upd_diff
          qps_ques=que_diff
          tps_Com_rol= Com_diff + rol_diff 
 
          print 'qps_s = %s , qps_ques = %s , tps_iud = %s ,tps_Com_rol = %s'  %(qps_s ,qps_ques, tps_iud,tps_Com_rol)
       except KeyboardInterrupt :
          print "exit .."
          sys.exit()
  
    conn.close()
if __name__ == '__main__':
   main()

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/22664653/viewspace-767266/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/22664653/viewspace-767266/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值