Databases 5 min read

How to Monitor MySQL QPS and TPS with a Simple Bash/Awk Script

This guide explains what MySQL QPS and TPS metrics are, how to calculate them using show status or mysqladmin extended‑status, and provides a ready‑to‑run Bash/Awk script that records QPS, TPS, Threads_connected and Threads_running every second.

Java High-Performance Architecture
Java High-Performance Architecture
Java High-Performance Architecture
How to Monitor MySQL QPS and TPS with a Simple Bash/Awk Script

QPS (Queries Per Second) and TPS (Transactions Per Second) are two key MySQL performance indicators that should be monitored regularly and compared with benchmark results.

Calculation Method

QPS is calculated as the difference in the Queries counter over a time interval, e.g., sampling for 10 seconds: QPS = (Q2 - Q1) / 10. TPS is derived from the sum of Com_commit and Com_rollback counters: TPS = (Com_commit + Com_rollback) / Seconds. Both counters are obtained via SHOW STATUS or mysqladmin extended-status.

Monitoring Script

The script runs mysqladmin -uroot -p'password' extended-status each second, extracts the values Queries , Com_commit , Com_rollback , Threads_connected and Threads_running , and computes QPS, TPS, and thread counts.

Key goals:

Collect QPS, TPS, Threads_connected, Threads_running every second.

Below is the core awk script used for parsing:

BEGIN{flag=0;
print "";
print "QPS   TPS    Threads_con Threads_run";
print "-------------------------------------"}
$2 ~ /Queries$/    {q=$4-lq; lq=$4;}
$2 ~ /Com_commit$/ {c=$4-lc; lc=$4;}
$2 ~ /Com_rollback$/ {r=$4-lr; lr=$4;}
$2 ~ /Threads_connected$/ {tc=$4;}
$2 ~ /Threads_running$/   {tr=$4;}
if(flag==0){ flag=1; }
else { printf "%-6d %-8d %-10d %d 
", q, c+r, tc, tr; }

The script prints a header on the first run, then for each subsequent second it outputs the calculated QPS, TPS, and thread counts.

You can download the full script from http://devdd.oss-cn-beijing.aliyuncs.com/mysql_QPS_TPS.txt and try it in your environment.

Images illustrating the metrics and script output:

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Performance MonitoringmysqlTPSawkdatabase metrics
Java High-Performance Architecture
Written by

Java High-Performance Architecture

Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.