FunTester python plotly 制作接口响应耗时的时间序列表 (Time Series)

FunTester · 2020年04月12日 · 802 次阅读

本人在做工作中,要对某一个接口的响应耗时进行一个长期的统计,由于之前的数据全都写在了数据库中,统计了半年多的数据。在学习了 plotly 的 Time Series 时间序列图标之后,绘制了一张接口响应耗时的图标,分享代码,供大家参考。

下面是从数据库读取数据的 java 代码:

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");

下面是读取文件的方法和生成表格的调用类的代码:

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

from first.date import DatePlot


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

    def __init__(self):
        print "欢迎使用fission类!"

    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("\r\n")[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 + "线程")

    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("\r\n")[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")

下面是生成时间序列表的封装类(我保留了多条折现的方法以及注释了显示规定日期间隔数据的方法):

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

import plotly.graph_objs as drive
import plotly.plotly


class DatePlot:
    def __init__(self):
        print "时间表格!"

    @staticmethod
    def MakePlot(x, y, titile):
        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=titile,
            # xaxis=dict(
            #     range=['2018-07-01', '2018-07-20'])
        )
        fig = dict(data=data, layout=layout)
        plotly.offline.plot(fig, filename=titile + ".html")

下面是效果图:

点击查看所有文章

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册