mysql-qps统计python脚本(每秒查询处理量)

时间:2015-05-07 17:07:11   收藏:0   阅读:959
#!/usr/bin/env python
# coding=utf-8
import time
import sys
import os
import MySQLdb
conn = MySQLdb.connect(host=‘127.0.0.1‘,port=3306,user=‘root‘,passwd=‘abc.123‘, charset=‘utf8‘)
conn.autocommit(True)
cursor=conn.cursor()
while True:
    diff = 1
    sql = "show global status where Variable_name in(‘com_select‘,‘com_insert‘,‘com_delete‘,‘com_update‘)"
    cursor.execute(sql)
    results = cursor.fetchall()
    first = []
    for rec in results:
        first.append(rec[1])
    time.sleep(diff)
    sql = "show global status where Variable_name in(‘com_select‘,‘com_insert‘,‘com_delete‘,‘com_update‘)"
    cursor.execute(sql)
    results = cursor.fetchall()
    second = []
    for rec in results:
        second.append(rec[1])
    qps = 0
    tps = 0
    for i in range(0, 4):
        if i != 0:
            b = int(second[i]) - int(first[i])
            tps += b
        a = int(second[i]) - int(first[i])
        qps += a
    print ‘qps = %s‘%(qps/diff)
    print ‘tps = %s‘%(tps/diff)
conn.close()


本文出自 “我的运维博客” 博客,请务必保留此出处http://linuxpython.blog.51cto.com/10015972/1643779

评论(0)
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!