Skip to content

Instantly share code, notes, and snippets.



Created Nov 11, 2014
What would you like to do?
Ping 99% Latencies
#!/usr/bin/env python
import argparse
import datetime
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy
import os
import re
import sys
import time
def total_seconds(td):
return td.days*24*60*60 + td.seconds
def parse(file):
Parse through the ping file and get the ping values.
:param file: The input file for ping data.
:return (x, y): The tuple containing a list of x values which are seconds since start and list y of ping values.
x = []
y = []
start = None
for line in file:
# Verify format has a time=#### ms
m ="(\d+-\d+-\d+ \d+:\d+:\d+).*time=([0-9\.]+) ms", line)
if not m: continue
ts =
ts = datetime.datetime.strptime(ts, '%Y-%m-%d %H:%M:%S')
y_i = float(
if not start:
start = ts
x_i = 0
x_i = total_seconds(ts - start)
return (x, y)
def process_99(x, y):
Group the values in minute buckets and get 99% for each bucket.
:param x: The seconds values.
:param y: The actual ping values.
:return (nx, ny): The tuple containing a list of nx values which are seconds since start and list ny of 99% ping values.
# Group into minute intervals and calculate 99%
nx = []
ny = []
group_x = []
group_y = []
for (x_i, y_i) in zip(x, y):
if len(group_x) == 0:
if (x_i - group_x[0]) <= 60:
# Within minute interval so group
# Process the past minute and start new
x_99 = group_x[0] # Use first data point in minute section as data point
y_99 = numpy.percentile(group_y, 99)
# Start new group
group_x = [x_i]
group_y = [y_i]
return (nx, ny)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Plot 99% ping latencies.')
parser.add_argument('-i', '--input', type=str, required=True, help='the ping output file')
args = parser.parse_args()
input_filename = args.input
if not os.path.isfile(input_filename):
sys.exit("Error: File does not exist, please provide a valid file.")
# Process the file
input_file = open(input_filename, 'r')
(x, y) = parse(input_file)
(nx, ny) = process_99(x, y)
# Plot the data
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_title("99 Percentile Ping Latencies")
ax.set_xlabel("Seconds Since Collection (sec)")
ax.set_ylabel("Ping (ms)")
ax.plot(nx, ny)
fig.savefig(input_filename + ".png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment