Skip to content

Instantly share code, notes, and snippets.

@RAttab
Last active August 29, 2015 14:09
Show Gist options
  • Save RAttab/8ed9d4a8743bade41a55 to your computer and use it in GitHub Desktop.
Save RAttab/8ed9d4a8743bade41a55 to your computer and use it in GitHub Desktop.
Latency report through bid request capture.
#! /bin/bash
#------------------------------------------------------------------------------#
# capture-bids
# Rémi Attab, 01 Oct 2013
# Copyright (c) 2013 Datacratic. All rights reserved.
#
# Captures bid requests off of port 8950 (haproxy usually) and dumps them in a
# file specified in the command line argument. Works in tandem with
# report-latency to produce latency reports on a per-host basis.
#
#------------------------------------------------------------------------------#
sudo ngrep -W single -qt 'POST /bids' port 8950 > $1
#! /usr/bin/python
#-------------------------------------------------------------- -*- python -*- #
# report-latency
# Remi Attab, 01 Oct 2013
# Copyright (c) 2013 Datacratic. All rights reserved.
#
# Uses a bid capture log taken by capture-bids and accumulates latency
# distributions based on hosts. The capture log is specified as the first
# command line argument.
#
# The latency is calculated based on the timestamp at which the packet was
# received minus the timestamp supplied in the bid request. The bid request
# timestamp may not always be available or it may be specified under a field in
# which case the script will have to be modified to work properly (I hope you
# like regexes).
# ------------------------------------------------------------------------------#
import sys
import re
from datetime import datetime
capture_file = sys.argv[1]
host_latency = {}
latencies = []
def get_latency(line):
ip_regex = "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+"
host_regex = "(" + ip_regex + "):[0-9]+ ->"
timestamp_regex = "\"timestamp\":([0-9]*\.[0-9]*),"
m = re.match("^T (.*) " + host_regex + ".*" + timestamp_regex + ".*", line)
if not m: return (False, "", 0, 0)
host = m.group(2)
start_ts = datetime.fromtimestamp(float(m.group(3)))
end_ts = datetime.strptime(m.group(1), "%Y/%m/%d %H:%M:%S.%f")
return (True, host, (end_ts - start_ts).total_seconds())
with open(capture_file, 'r') as f:
for line in f:
line = line.strip()
if len(line) == 0: continue
result = get_latency(line)
if not result[0]: continue
latencies.append(result[2])
if not result[1] in host_latency:
host_latency[result[1]] = [result[2]]
else:
host_latency[result[1]].append(result[2])
def print_dist(name, dist):
dist.sort()
n = len(dist)
print "%-15s %9d %9.6f %9.6f" % (name, n, dist[n/2], dist[n / 100 * 99])
print "%-15s %9s %9s %9s" % ("name", "count", "median", "99th")
print_dist("all", latencies)
for host, latency_dist in host_latency.items():
print_dist(host, latency_dist)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment