Operations 6 min read

Visualizing Long-Term API Latency with Java, Python, and Plotly

This guide shows how to extract average API response times from a MySQL database using Java, process the data with a Python script, and generate an interactive time-series chart with Plotly, providing a practical method for long-term performance monitoring.

FunTester
FunTester
FunTester
Visualizing Long-Term API Latency with Java, Python, and Plotly

The article demonstrates a complete workflow for collecting, processing, and visualizing API response time metrics over an extended period.

1. Retrieve data from MySQL using Java

A Java snippet executes a SQL query that groups average elapsed time by date for two specific API endpoints, then stores the results in a JSONObject and writes the JSON to a file named apitime.

JSONObject data = new JSONObject();
ResultSet resultSet = MySqlTest.excuteQuerySql(
    "SELECT DATE(create_time),AVG(elapsed_time) *1000 FROM request_record " +
    "WHERE api_name in(\"/service/user/v3/login/mobile/v5\",\"/service/user/v3/login/mobile/v4\") " +
    "GROUP BY DATE(create_time) ORDER BY DATE(create_time);");
while (resultSet.next()) {
    String name = resultSet.getString(1);
    double time = resultSet.getDouble(2);
    data.put(name, time);
}
resultSet.close();
Save.saveJsonList(data, "apitime");

2. Read the JSON file and prepare data in Python

A Python class Fission reads the generated log files, filters out values greater than 2 seconds, and builds three lists: x (date labels), y (response times), and z (raw timestamps). The method getDataMarkLine parses lines split by "|" to obtain day and time values.

#!/usr/bin/python
# coding=utf-8

class Fission:
    x = []
    y = []
    z = []

    def __init__(self):
        print "Welcome to use Fission class!"

    def getData(self, name):
        size = 0
        with open("/Users/Vicky/Documents/workspace/api_test/long/" + name + ".log") as apidata:
            for i in apidata:
                data = i.split("
")[0]
                time = float(data)
                if time > 2:
                    continue
                self.z.append(data)
                size += 1
        length = size
        for i in range(length):
            self.x.append(name + " thread")

    def getDataMarkLine(self, name):
        with open("/Users/Vicky/Documents/workspace/api_test_najm/long/" + name + ".log") as apidata:
            for i in apidata:
                data = i.split("
")[0].split("|")
                day = data[0]
                time = float(data[1])
                self.x.append(day)
                self.y.append(time)
        return [self.x, self.y]

if __name__ == "__main__":
    fission = Fission()
    a = fission.getDataMarkLine("apitime")
    DatePlot.MakePlot(a[0], a[1], "time")

3. Plot the time‑series chart with Plotly

The DatePlot class encapsulates Plotly chart creation. It defines a static method MakePlot that builds a scatter trace, optionally adds a second static trace, assembles a layout with a title, and writes an offline HTML file.

#!/usr/bin/python
# coding=utf-8
import plotly.graph_objs as drive
import plotly.plotly

class DatePlot:
    def __init__(self):
        print "Time table!"

    @staticmethod
    def MakePlot(x, y, title):
        a = drive.Scatter(x=x, y=y, name="SSSSS", line=dict(color='#17BECF'), opacity=1)
        b = drive.Scatter(x=["2016-02-20", "2016-02-21", "2016-02-23"],
                         y=[28.04, 20, 33], name="AAAAA", line=dict(color='#7F7F7F'), opacity=0.8)
        data = [a]
        layout = dict(title=title)
        fig = dict(data=data, layout=layout)
        plotly.offline.plot(fig, filename=title + ".html")

4. Result

The generated HTML file displays an interactive line chart where the X‑axis represents dates and the Y‑axis shows average response times in milliseconds, enabling engineers to observe performance trends and identify potential regressions.

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.

JavaPythonperformance analysisTime SeriesplotlyAPI monitoring
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.