Skip to content

Instantly share code, notes, and snippets.

@shilrobot
Created July 18, 2012 17:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shilrobot/3137723 to your computer and use it in GitHub Desktop.
Save shilrobot/3137723 to your computer and use it in GitHub Desktop.
Display graphs of results from UDP packet loss tester
import matplotlib.pyplot as plt
from scipy import stats
import numpy as np
# parse CSV file
# format is <size>, <recv_time>, <send_time>
# recv_time and send_time may be on different clocks so we can only compare vs. mean
lines =[]
for l in open('client_data/0.csv'):
parts = [float(x) for x in l.strip().split(',')]
lines.append(parts)
arr = np.array(lines)
sizes = arr[:,0]
diffs = (arr[:,2] - arr[:,1])
diffs = diffs - np.mean(diffs) # relative to mean
diffs *= 1000 # work in msec
# Perform linear least squares to get an equation that estimates relative transmission time time based on size
a = np.vstack([np.ones(len(lines)), sizes])
a = np.transpose(a)
b = diffs
x = np.linalg.lstsq(a,b)[0]
b,m = x
print 'msec = %f + %f x size' % (b,m)
print 'Derived transmission rate: %g kB/s' % ((1.0/m)/1024.0*1000)
# Get the times predicted based on the linear equation (any residues we will consider as jitter)
predicted_diffs = np.dot(a,x)
# Show some stats about jitter
import scipy
z = (diffs-predicted_diffs)
print 'Based on average transmission rate and packet size:'
print '95%% of packets arrive within %f ms of predicted time' % ((scipy.stats.scoreatpercentile(z, 97.5) - scipy.stats.scoreatpercentile(z, 2.5)) * 0.5)
print '99%% of packets arrive within %f ms of predicted time' % ((scipy.stats.scoreatpercentile(z, 99.5) - scipy.stats.scoreatpercentile(z, 0.5)) * 0.5)
# Do a plot of size vs. relative recv-send time, and a histogram of the residues of recv-send time vs. predicted recv-send time
plt.subplot(211)
plt.plot(sizes,diffs,'.')
s_sizes = np.sort(sizes)
plt.plot(s_sizes, (b+m*s_sizes),'r')
plt.subplot(212)
plt.hist((diffs-predicted_diffs), bins=50)
# Show plot
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment