Skip to content

Instantly share code, notes, and snippets.

@danriti
Last active August 29, 2015 14:01
Show Gist options
  • Save danriti/f93c88522fb17eb1d4a1 to your computer and use it in GitHub Desktop.
Save danriti/f93c88522fb17eb1d4a1 to your computer and use it in GitHub Desktop.
PHP Average Latency By Week
from datetime import datetime
import matplotlib.pyplot as plt
import traceview
API_KEY = 'API KEY HERE'
def main():
tv = traceview.TraceView(API_KEY)
results = tv.server.latency_by_layer('Default', time_window='week')
# get the php layer
php = next((x for x in results if x.get('layer') == 'PHP'), None)
# convert timestamps to datetime objects
dates = [datetime.utcfromtimestamp(i[0]) for i in php['timeseries']['items']]
# calculate average latency (total_latency / volume) and convert to
# milliseconds
average_latency = [((i[2] / i[1]) / 1000) for i in php['timeseries']['items']]
# create a matplotlib plot
plt.figure(figsize=(12, 5), dpi=80)
plt.stackplot(dates, average_latency, colors=['#1dacd6'])
plt.title('PHP Average Latency (ms) - by Week')
plt.savefig('latency.png')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment