mysql计算QPS

首先连接上mysql:

$ mysql -h 192.168.0.x -P3306 -uusername -p123456

进入Mysql之后,查询general_log:

mysql>  SHOW VARIABLES LIKE "general_log%";
+------------------+----------------------------+
| Variable_name    | Value                      |
+------------------+----------------------------+
| general_log      | OFF                        |
| general_log_file | /var/run/mysqld/mysqld.log |
+------------------+----------------------------+
2 rows in set (0.00 sec)

如果这个时候general_log是关闭的,可用命令设置为ON:

mysql> SET GLOBAL general_log = 'ON';
Query OK, 0 rows affected (0.00 sec)

同理也可以设置文件路径:

mysql> SET GLOBAL general_log_file = '/var/run/mysqld/mysqld.log';
Query OK, 0 rows affected (0.06 sec)

打开/var/run/mysqld/mysqld.log文件看到的是如下展示:

Time            Id  Command     Argument
190529 22:26:07 4   Query       REPLACE INTO *
                1   Query       REPLACE INTO *
                4   Query       REPLACE INTO *
                1   Query       REPLACE INTO *

从该文件可以看出来,秒数发生改变的时候,才会打印带日期的日志。那么可用通过该文件两个带日期的行之间的行数差距,即可算出其QPS:

  • 筛选带有日期的行,带上行号转存到文件
$ grep "^1905" -n mysqld.log > Line.txt 
  • 编写一个Python脚本,计算QPS
 1 #coding=utf-8
 2 import os,sys
 3 
 4 if __name__ == '__main__':
 5         file = open('Line.txt', 'r')
 6         if file == None:
 7                 print "open file failed"
 8                 sys.exit(1)
 9         outFile = open("QPS.csv", 'w')
10         outFile.write("Time,QPS");
11         lastLine = 0
12         for line in file:
13                 params = line.split(' ')
14                 if len(params) < 2:
15                         continue
16 
17                 time = params[1]
18                 lineAndDate = params[0]
19                 subParams = lineAndDate.split(':')
20                 if len(subParams) < 2:
21                         continue
22                 line = int(subParams[0])
23                 date = subParams[1]
24                 fullDate = "%s %s" %(date, time)
25                 QPS = line - lastLine
26                 lastLine = line
27 
28                 outFile.write("\r\n%s,%d"%(fullDate,QPS))
29         outFile.close()
30         file.close()

然后执行脚本,计算结果导出到QPS.csv:

 

posted @ 2019-05-30 16:36  溟漓  阅读(3902)  评论(0编辑  收藏  举报