Advertisement
Guest User

Untitled

a guest
Nov 21st, 2014
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.80 KB | None | 0 0
  1. import sys, subprocess, numpy, Gnuplot
  2.  
  3. latency_file = "latencies.txt"
  4.  
  5. def create_latency_file(logfile):
  6.         # extract latency column from log
  7.         out_file = open(latency_file, "w")
  8.         var = subprocess.call(["cut", "-d", " ", "-f", "4", logfile], stdout=out_file)
  9.         out_file.close()
  10.  
  11. def get_ordered_latencies():
  12.  
  13.         latencies = []
  14.         valid_latencies = []
  15.  
  16.         with open(latency_file) as f:
  17.                 data = f.read()
  18.                 latencies = data.split("\n")
  19.                 latencies.pop(-1) #ninjahack
  20.  
  21.         for latency in latencies:
  22.                 # Only use valid latencies. -1 indicates failure
  23.                 if latency != '-1':
  24.                         valid_latencies.append(float(latency))
  25.  
  26.         return sorted(valid_latencies)
  27.  
  28. def plot_cdf():
  29.     x_values = get_ordered_latencies()
  30.     y_values = numpy.arange(len(x_values))/float(len(x_values))
  31.  
  32.     gp = Gnuplot.Gnuplot(persist = 1)
  33.     gp('set title "CDF of latencies"')
  34.     gp('set grid')
  35.     gp('set xlabel "Latency [s]"')
  36.     gp('set ylabel "Cumulative probability"')
  37.  
  38.     cdf = Gnuplot.Data(x_values, y_values, with_="line", title="Latency")
  39.     gp.plot(cdf)
  40.  
  41.  
  42. if __name__ == "__main__":
  43.     if len(sys.argv) == 2:
  44.         if 'plot' in sys.argv[1]:
  45.             print "... Plotting CDF ..."
  46.             plot_cdf()
  47.             print "... Plotting completed ..."
  48.     elif len(sys.argv) == 3:
  49.         if "extract" in sys.argv[1]:
  50.             print "... Extracting latencies from logfile ..."
  51.             create_latency_file(sys.argv[2])
  52.             print "... Done ..."
  53.     else:
  54.         print "Commands:"
  55.         print "* 'extract' 'logfile': creates a new file with all the latencies extracted from the log file"
  56.         print "* 'plot': plots the latencies from the newly created latency file using Gnuplot"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement