mysql计算实时的QPS/TPS

版本:mysql 5.7

需要开启show_compatibility_56

复制代码
mysql> set global show_compatibility_56=on;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like '%show_compatibility_56%';
+-----------------------+-------+
| Variable_name         | Value |
+-----------------------+-------+
| show_compatibility_56 | ON    |
+-----------------------+-------+
1 row in set (0.01 sec)
复制代码

 

 

 

QPS:

select VARIABLE_VALUE,now() from information_schema.GLOBAL_STATUS where VARIABLE_NAME in ('Queries');

查询两次得到的差值除以时间间隔

 

TPS:

复制代码
select
  sum(`VARIABLE_VALUE`),
  now()
from
  information_schema.GLOBAL_STATUS
where
  VARIABLE_NAME in (
    'COM_COMMIT',
    'COM_ROLLBACK',
    'COM_INSERT',
    'COM_DELETE',
    'COM_UPDATE'
  )
复制代码

 

python 计算QPS

复制代码
# -*- coding: utf-8 -*-

import pymysql
import smtplib
import datetime
import time

##mysql 5.7
mysql_server="192.168.1.22"
user_name="root"
password= "testtest"
db_name="performance_schema"
gl_port=3306
offset = 100

def get_query_value():
    sqltext = "select VARIABLE_VALUE as a from performance_schema.global_status where VARIABLE_NAME in ('Queries')"
    ##print(sqltext)
    db = pymysql.connect(mysql_server,user_name, password, db_name,port=gl_port)
    cursor = db.cursor()
    try:
        cursor.execute(sqltext)
        results = cursor.fetchone()
    except Exception as e:
        print(e)
    db.close()
    q_value = results[0]
    return q_value

if __name__ == '__main__':
    prev_ops = 0
    while True:
        ops = int(get_query_value())
        qps = ops - prev_ops
        prev_ops = ops
        print("QPS: "+ str(qps))
        time.sleep(1)
复制代码

 

TPS

复制代码
# -*- coding: utf-8 -*-

import pymysql
import smtplib
import datetime
import time

##mysql 5.7
mysql_server="192.168.1.22"
user_name="root"
password= "testtest"
db_name="information_schema"
gl_port=3306
offset = 100

def get_tps_value():
    sqltext = "select sum(VARIABLE_VALUE) as a from information_schema.global_status where VARIABLE_NAME in ('COM_COMMIT','COM_ROLLBACK','COM_INSERT','COM_DELETE','COM_UPDATE')"
    ##print(sqltext)
    db = pymysql.connect(mysql_server,user_name, password, db_name,port=gl_port)
    cursor = db.cursor()
    try:
        cursor.execute(sqltext)
        results = cursor.fetchone()
    except Exception as e:
        print(e)
    db.close()
    q_value = results[0]
    return q_value



if __name__ == '__main__':
    prev_ops = 0
    while True:
        ops = int(get_tps_value())
        qps = ops - prev_ops
        prev_ops = ops
        print("TPS: "+ str(qps))
        time.sleep(1)
复制代码

 

posted @   slnngk  阅读(48)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2023-04-19 19c dml重定向使用
点击右上角即可分享
微信分享提示