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.

FunTester
FunTester
FunTester
Visualizing API Response Times with Python Plotly Violin Charts

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.

Violin chart of API request times
Violin chart of API request times

This approach demonstrates a practical workflow for turning raw performance metrics into insightful visualizations without requiring heavyweight BI tools.

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.

mysqlAPI testingdata-visualizationviolin-plot
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

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.