Advertisement
Guest User

LatencyList

a guest
Aug 21st, 2014
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.54 KB | None | 0 0
  1. #!/usr/bin/env python
  2. """
  3. File name: omnitools.py
  4. Author: Enrico lomiz Zimol
  5. Date created: 17/12/2013
  6. Date last modified: 21/08/2014
  7. Python Version: 2.7.X
  8. """
  9. import math
  10. __license__ = "LGPL"
  11. __version__ = "0.5"
  12. __email__ = "enricoONEDOTzimolROUNDEDATgmailANOTHERDOTcom"
  13. __status__ = "Prototype"
  14. # TODO LatencyList
  15. # Using decorators or @propriety force used_latencies to be less or equal of max_width
  16. # http://stackoverflow.com/questions/17330160/python-how-does-the-property-decorator-work
  17. class LatencyList:
  18. """
  19. Manages and elaborates a list of latencies
  20. A list that work with FIFO(First In First Out) logic.
  21. You can obtain usefull latency information like jitter, average, min, max calculated
  22. with the lasts N latencies where N is "used_latencies".
  23. Class Attributes:
  24. latencies -- a list of lantencies in ms
  25. max_width -- over this list width old latencies will be discharged to allow new ones (FIFO system)
  26. used_latencies -- how many recent latencies will be used for calculations
  27. DocTest
  28. >>> l = LatencyList([], 8)
  29. >>> l.max_width = 15
  30. >>> l.add(42.3)
  31. >>> l.add(47.432)
  32. >>> l.add(81)
  33. >>> l.add(50.19)
  34. >>> l.add(150.91)
  35. >>> l.add(40.7)
  36. >>> l.add(45)
  37. >>> l.add(69.4)
  38. >>> l.add(13.43)
  39. >>> l.add(64.6)
  40. >>> l.add(None)
  41. >>> l.add(None)
  42. >>> l.add(None)
  43. >>> l.add(44.9123)
  44. >>> l.add(23.1)
  45. >>> l.length()
  46. 15
  47. >>> l.add(44.9123)
  48. >>> l.add(23.1)
  49. >>> l.length()
  50. 15
  51. >>> len(l.get_used_latencies()) >= len(l.get_used_latencies(True))
  52. True
  53. >>> l.get_packetloss()
  54. 0.375
  55. >>> round(l.samp_std_dev(),3)
  56. 17.497
  57. >>> round(l.pop_std_dev(),3)
  58. 15.65
  59. """
  60. def __init__(self, latencies=[], used_latencies=15):
  61. self.latencies = latencies
  62. self.max_width = 50
  63. self.used_latencies = used_latencies
  64. def __str__(self):
  65. #return str(self.latencies)
  66. # To obtain only 2 decimal precision
  67. #l = ["%0.2f" % i for i in self.latencies]
  68. r = "latencies: " + str(self.get_string_latencies()) + \
  69. "\nmax_width: " + str(self.max_width) + \
  70. "\nlength: " + str(self.length()) + \
  71. "\nused_latencies: " + str(self.used_latencies) + "\n"
  72. return r
  73. def get_string_latencies(self, rounding_decimals=2):
  74. """
  75. Convert self.latencies elements in strings and trucates off numerical values to N decimals
  76. where N is rounding_decimals. None value (packet lost) will be translated in "lost".
  77. This new list isn't suitable for calculation
  78. """
  79. # String conversion and float truncation to the new list
  80. rounded_list = []
  81. for latency in self.latencies:
  82. # if number -> truncate decimal and append to list
  83. if isinstance(latency, int) or isinstance(latency, float):
  84. rounded_list.append(str(round(latency, rounding_decimals)))
  85. # if other -> append only
  86. else:
  87. rounded_list.append("lost")
  88. return rounded_list
  89. def get_used_latencies(self, crop=False):
  90. """
  91. Return a list of last N latencies where N is "used_latencies"
  92. If you need the list for calculation probably you want to enable the cropping
  93. crop - if True remove every "None" in the list and return list cleaned. Usefull for calculation.
  94. """
  95. if crop:
  96. return self.crop_latencies(self.latencies[-self.used_latencies:])
  97. else:
  98. return self.latencies[-self.used_latencies:]
  99. def crop_latencies(self, lat_list):
  100. """
  101. Return "list" with only latencies (exclude all None(s) that mean PacketLoss)
  102. """
  103. cropped_list = []
  104. for latency in lat_list:
  105. if latency is not None:
  106. cropped_list.append(latency)
  107. return cropped_list
  108. def get_packetloss(self):
  109. """
  110. Return packetloss percentage in used_latencies
  111. """
  112. return self.count_packetlost(self.get_used_latencies()) / float(self.used_latencies)
  113. def count_packetlost(self, lat_list):
  114. """
  115. Return None(packetloss) counts in "list"
  116. """
  117. counter = 0
  118. for latency in lat_list:
  119. if latency is None:
  120. counter += 1
  121. return counter
  122. def add(self, latency):
  123. if self.length() >= self.max_width:
  124. self.remove()
  125. self.latencies.append(latency)
  126. def remove(self):
  127. self.latencies.pop(0)
  128. def average(self):
  129. return round(reduce(lambda x, y: x + y / float(len(self.get_used_latencies(True))), self.get_used_latencies(True), 0),5)
  130. def length(self):
  131. return len(self.latencies)
  132. def max(self):
  133. return max(self.get_used_latencies(True))
  134. def min(self):
  135. return min(self.get_used_latencies(True))
  136. def variations_sum(self):
  137. sm = 0 # somma scarti
  138. for variation in self.get_used_latencies(True):
  139. sm = sm + ( (variation - self.average())**2)
  140. return sm
  141. def samp_std_dev(self):
  142. """
  143. Standard Deviation of a Sample
  144. """
  145. return math.sqrt( self.variations_sum() / (len(self.get_used_latencies(True))-1) )
  146. def pop_std_dev(self):
  147. """
  148. Standard Deviation of a Population
  149. """
  150. return math.sqrt(self.variations_sum()/len(self.get_used_latencies(True)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement