测量服务器带宽


0

我想测量我在Python重邮器服务器中实现的带宽,这意味着我想测量我的服务器每秒处理的字节数。所以,我是如何刨做到这一点是:对于一段固定时间(例如300sec),我测量的数量received bytessent bytes。在这段时间后,我计算出比率:bytes_received / bytes_sent。但是,我不确定这是我想要的,因为它给了我一个比率(通常在1-1.5左右),所以这意味着我处理了我在一段时间内收到的所有或几乎所有的消息,但是我想测量我处理的字节数。如果有人能告诉我如何衡量带宽,我将非常感激。

Answers:


0

我想你需要做的是:

bytes_received = bytes_received300s - bytes_received0s

bytes_sent = bytes_sent300s - bytes_sent0s

total_bytes_processed = bytes_received - bytes_sent

这将为您提供300秒期间处理的总字节数。


你是什​​么意思bytes_received0s?
Ziva 2016年

0

您可以使用psutil.net_io_counters()来计算一段时间内的带宽。您将在0秒拍摄快照,在300秒拍摄快照。

def get_bandwidth():
    # Get net in/out
    net1_out = psutil.net_io_counters().bytes_sent
    net1_in = psutil.net_io_counters().bytes_recv

    time.sleep(300) # Not best way to handle getting a value 300 seconds later

    # Get new net in/out
    net2_out = psutil.net_io_counters().bytes_sent
    net2_in = psutil.net_io_counters().bytes_recv

    # Compare and get current speed
    if net1_in > net2_in:
        current_in = 0
    else:
        current_in = net2_in - net1_in

    if net1_out > net2_out:
        current_out = 0
    else:
        current_out = net2_out - net1_out

    network = {"traffic_in": current_in, "traffic_out": current_out}

    # Return data in bytes
    return network
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.