Visualizing API Response Times with Python Plotly Violin Charts
The author records API request durations in a MySQL table, then uses Python, pandas, and Plotly to fetch the data, filter out outliers, and generate a violin chart that clearly visualizes the distribution of response times for multiple endpoints.
During routine API testing the author stored each request's latency in a MySQL database, accumulating a sizable dataset. To make the data easier to interpret, they learned Python with Plotly and created a violin plot that visualizes the response‑time distribution for several API endpoints.
The main script performs the following steps:
#!/usr/bin/python
# coding=utf-8
import plotly.plotly
import pandas as pd
import plotly.figure_factory as ff
import second.mysql
if __name__ == "__main__":
a = second.mysql.Mysql()
b = a.getApiTimes('/article/list/userfeed', '/article/detail', '/article/info', '/advertise/api/list', '/common/menu')
df = pd.DataFrame(dict(Score=b[0], Group=b[1])) # combine data
fig = ff.create_violin(df, data_header='Score', group_header='Group',
height=700, width=1200, title='接口请求时间')
plotly.offline.plot(fig)The getApiTimes method defined in the MySQL helper class queries the api_result table for each supplied endpoint, filters out abnormal records (response time < 1 second), and returns two parallel lists: the recorded times and the corresponding API names.
def getApiTimes(self, *params):
num = str(params.__len__())
print "接口数:" + num
conn = self.conn
cur = conn.cursor()
data = []
size = []
for api in params:
cur.execute("SELECT * FROM api_result WHERE api_name = \"" + api + "\"")
dfs = cur.fetchall()
for row in dfs:
if row[7] < 1:
data.append(row[7])
size.append(api)
cur.close()
conn.commit()
conn.close()
return [data, size]The resulting violin chart (shown below) makes the distribution of API latencies immediately apparent, providing a clear visual aid for presentations or performance reports.
This approach demonstrates a practical workflow for turning raw performance metrics into insightful visualizations without requiring heavyweight BI tools.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
