运维开发网

探测web接口的质量,将连接接口的时间在折线图中显示

运维开发网 https://www.qedev.com 2020-09-20 13:26 出处:51CTO 作者:happyting1214
安装pycurl模块注:由于操作系统是libcurl模块的版本和pycurl的最新版本不匹配,先要重新升级一下libcurl模块。步骤如下:下载并安装curl和pycurl模块wgethttps://curl.haxx.se/download/curl-7.72.0.tar.gzwgethttps://files.pythonhosted.org/packages/50/1a/35b1d8b8e4
  1. 安装pycurl模块

    注:由于操作系统是libcurl模块的版本和pycurl的最新版本不匹配,先要重新升级一下libcurl模块。

    步骤如下:

    下载并安装curl和pycurl模块

wget https://curl.haxx.se/download/curl-7.72.0.tar.gz
wget https://files.pythonhosted.org/packages/50/1a/35b1d8b8e4e23a234f1b17a8a40299fd550940b16866c9a1f2d47a04b969/pycurl-7.43.0.6.tar.gz

# tar -zvxf curl-7.72.0.tar.gz
# cd curl-7.72.0
# ./configure --prefix=/usr/local/curl
# make && make install

# tar -zvxf pycurl-7.43.0.6.tar.gz
# cd pycurl-7.43.0.6
# python setup.py install --curl-config=/usr/local/curl/bin/curl-config

# vim /etc/profile
export LD_LIBRARY_PATH=/usr/local/curl/lib:$LD_LIBRARY_PATH

# source /etc/profile

2、安装pyecharts模块

注:如果使用默认安装,在执行过程中会报一个异常:所有图表类型将在 v1.9.0 版本开始强制使用 ChartItem 进行数据项配置!因此使用降级安装

# pip install pyecharts==1.7.1

3、脚本内容

注:为了方便测试,时间和执行频率都使用固定值

import os
import sys
import time
import datetime
import pycurl
import pyecharts.options as opts
from pyecharts.charts import Line
from pyecharts.faker import Faker

eTime = "2020-09-17 18:30:00"
DateTypeTime = datetime.datetime.strptime(eTime,"%Y-%m-%d %H:%M:%S")
eTimeStamp = DateTypeTime.timestamp()

num = int(eTimeStamp) - int(time.time())
step = 5

data1 = []
data2 = []
data3 = []
data4 = []
data5 = []
x_data = []
startNum = 0
def webQuality(url):
    c = pycurl.Curl()
    c.setopt(pycurl.URL,url)
    c.setopt(pycurl.CONNECTTIMEOUT,10)
    c.setopt(pycurl.TIMEOUT,10)
    c.setopt(pycurl.NOPROGRESS,1)
    c.setopt(pycurl.FORBID_REUSE,1)
    c.setopt(pycurl.MAXREDIRS,1)
    c.setopt(pycurl.DNS_CACHE_TIMEOUT,30)
    indexfile = open(os.path.dirname(os.path.realpath(__file__))+"/ content.txt","wb")
    c.setopt(pycurl.WRITEHEADER,indexfile)
    c.setopt(pycurl.WRITEDATA,indexfile)
    try:
        c.perform()
    except Exception as e:
        print("connection error:"+str(e))
        indexfile.close()
        c.close()
        sys.exit()
    NAMELOOKUP_TIME = c.getinfo(c.NAMELOOKUP_TIME)
    CONNECT_TIME = c.getinfo(c.CONNECT_TIME)
    PRETRANSFER_TIME = c.getinfo(c.PRETRANSFER_TIME)
    STARTTRANSFER_TIME = c.getinfo(c.STARTTRANSFER_TIME)
    TOTAL_TIME = c.getinfo(c.TOTAL_TIME)
    HTTP_CODE = c.getinfo(c.HTTP_CODE)
    HEADER_SIZE = c.getinfo(c.HEADER_SIZE)
    result =  [NAMELOOKUP_TIME,CONNECT_TIME,PRETRANSFER_TIME,STARTTRANSFER_TIME,TOTAL_TIME]
    return result

while num>=0:
    time.sleep(5)
    num = num - 5
    startNum = startNum + 5
    xdata.append(startNum)
    webTimeInfo = webQuality("http://www.XXXXX.com")
    data1.append(webTimeInfo[0])
    data2.append(webTimeInfo[1])
    data3.append(webTimeInfo[2])
    data4.append(webTimeInfo[3])
    data5.append(webTimeInfo[4])

print("x_data = "+str(x_data))
print("data1 = "+str(data1))
print("data2 = "+str(data2))
print("data3 = "+str(data3))
print("data4 = "+str(data4))
print("data5 = "+str(data5))

c = (
    Line()
    .add_xaxis(x_data)
    .add_yaxis("DNS解析时间", data1, is_smooth=True)
    .add_yaxis("建立连接时间", data2, is_smooth=True)
    .add_yaxis("准备传输时间", data3, is_smooth=True)
    .add_yaxis("开始传输时间", data4, is_smooth=True)
    .add_yaxis("传输结束总时间", data5, is_smooth=True)
    .set_global_opts(title_opts=opts.TitleOpts(title="Line-smooth"))
    .render("line_smooth.html")
)

扫码领视频副本.gif

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号