projected_link_metrics.py
Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 
00003 import sys
00004 import math
00005 
00006 def get_projected_link_metrics(bandwidth_limit, latency, loss, packet_size, tx_bandwidth):
00007     bandwidth_limit = float(bandwidth_limit)
00008     tx_bandwidth = float(tx_bandwidth)
00009     if bandwidth_limit == 0.0:
00010         bandwidth_limit = 10000 * 1e6
00011 
00012     tx_bandwidth_loss_adjusted = tx_bandwidth * (1 - loss/100.0)
00013 
00014     link_saturated = tx_bandwidth_loss_adjusted > bandwidth_limit
00015 
00016     proj_bandwidth = min(bandwidth_limit, tx_bandwidth_loss_adjusted)
00017     
00018     loss_due_to_saturation = (1 - bandwidth_limit/tx_bandwidth) * 100.0
00019 
00020     proj_loss = max(loss_due_to_saturation, loss) 
00021 
00022     if not link_saturated:
00023         proj_latency = latency
00024     else:
00025         packet_send_time = (8.0 * packet_size) / bandwidth_limit
00026         netem_rule_in_place = (loss != 0.0 or latency != 0.0)
00027 
00028         if netem_rule_in_place:   
00029             queue_size_in_packets = math.ceil(latency/packet_send_time + 1)
00030             proj_latency = queue_size_in_packets * packet_send_time 
00031             
00032             # if extra loss is being added by netem, in addition to that caused by saturation
00033             # latency must be scaled accordingly
00034             if loss > loss_due_to_saturation:
00035                 proj_latency = proj_latency * loss/(100.0 - loss_due_to_saturation)
00036         else: # i.e. just tbf, no netem
00037             proj_latency = packet_send_time
00038      
00039     return (proj_bandwidth, proj_latency, proj_loss)
00040 
00041 if __name__ == '__main__':
00042     if len(sys.argv) != 6:
00043         print "Usage: projected_link_metrics.py <bandwidth_limit> <latency> <loss> <packet_size> <tx_bandwidth>\n"
00044         print "bandwidth_limit: simulated link capacity (in bps)"
00045         print "latency: simulated link latency (in seconds)"
00046         print "loss: packet loss (in %, but don't include the % sign)"
00047         print "packet_size: in bytes (NOT bits)"
00048         print "tx_bandwidth: send rate in bps"
00049         print 
00050         print "Example: projected_link_metrics.py 1000000 0.01 20.0 1500 1500000"
00051         print "Simulates a link capacity of 1Mbit/s with a 10ms latency and 20% packet loss, packet size of 1500bytes" \
00052             " and data sent at 1.5Mbit/s"
00053         exit(1)
00054 
00055     (bandwidth, latency, loss) = get_projected_link_metrics(float(sys.argv[1]), float(sys.argv[2]), float(sys.argv[3]),
00056                                                             int(sys.argv[4]), float(sys.argv[5]))
00057 
00058     print "Projected metrics: bandwidth %.2fKbit/s latency %.2fms loss %.2f%%"% \
00059         (bandwidth/1e3, latency*1e3, loss)
00060 
00061     


network_traffic_control
Author(s): Catalin Drula
autogenerated on Thu Apr 24 2014 15:35:03