Over a million developers have joined DZone.

Analyzing API Performance by Day of the Week

DZone 's Guide to

Analyzing API Performance by Day of the Week

User patterns, the Internet, APIs — these components are constantly changing, which is why analyzing performance by day is essential.

· Performance Zone ·
Free Resource

My last post showed how the API Science API can be utilized to create a graphical analysis of API performance binned by the hour of the day. In this post, we use data extracted from the API Science API to analyze the performance of an API by day of the week. This type of analysis would be useful for a company whose product experiences surges of usage on different days of the week (for example, weekends versus work days).

First, we create an csh command-line script to pull the data from the API Science API:

curl 'https://api.apiscience.com/v1/monitors/1572022/performance.json?start=2019-06-14&end=2019-06-21&resolution=day' -H 'Authorization: Bearer MY_API_SCIENCE_KEY'

This script invokes cURL (a command-line tool for transferring data with URLs) to download performance data from the API Science API. The number 1572022 identifies the API Science monitor. This particular monitor calls the World Bank Countries API (which is located in Washington, DC, US) from Ireland. The start and end parameters identify that performance data should be returned for the week that begins on 14 June 2019. resolution=day indicates that the data should be binned into the average performance for each day. The Authorization field contains the authorization key provided by API Science, which provides the user with permission to access the API Science API.

Executing this script returns JSON-formatted data, including a heading and records for each day of the week. Here’s a snippet of the returned data (formatted for reading convenience):

"data":[{"averageResolve":112.19,"averageConnect":74.36,"averageProcessing":82.71,"averageTransfer":0.19,"averageTotal":269.45,"startPeriod":"2019-06-20T00:00:00.000Z","endPeriod":"2019-06-21T00:00:00.000Z"}, ...

The data is directed to a text file. A Python program reads the JSON data stored in the file, analyzes it by day of the week, then creates a bar plot of the results binned by day of the week.

Python’s JSON.load() loads the data from the file:

with open('IR_daily.json') as f:
    perf = json.load(f)

Each performance data record contains a startPeriod and endPeriod timestamp. We use the startPeriod timestamp to compute the day of the week. In Python, Monday is day-of-week 0, Tuesday is day-of-week 1, etc., with Sunday being day-of-week 6. The following code extracts the year, month, and day of the month from the timestamp, then uses the Python  datetime() and  weekday() functions to identify which day of the week the timestamp represents:

    year = int(perf['data'][i]['startPeriod'][0:4])
    month = int(perf['data'][i]['startPeriod'][5:7])
    dom = int(perf['data'][i]['startPeriod'][8:10])
    this_day = datetime.date(year, month, dom)
    this_dow = this_day.weekday()
    print year, month, dom, this_dow

The average performance for each day of the week is placed into an array of size 7 named daily_avg. This is the Y data for the plot. The X data (xAxis) is the values 0 through 6 (Python’s designation of Monday through Sunday). Three-letter text labels for the days of week 0-6 are plotted along the X axis.

Here’s the Python code for creating a bar plot of the day-of-week performance data:

plt.bar(xAxis, daily_avg) #, color='teal')
plt.xticks(xAxis, days_of_week)
plt.ylabel('Average Total Milliseconds')
plt.xlabel('Day of Week')
title = 'Monitor 1572022 Past Week Performance by Day of Week'

Here’s the plot the program produces for the example set of downloaded performance data:

In this case, the average performance for each day of the week was fairly uniform. If this is the typical performance per day of the week over the long term, then there would be no reason for a company whose product relies on the World Bank Countries API to be concerned about, for example, performance on weekends versus work days.

Since user patterns, the Internet, and APIs are constantly changing, analyzing API performance by day of the week on an ongoing basis is valuable. This article shows how to use the API Science API, curl, and Python to create a custom monitor that accomplishes this.

performance ,api ,data ,analysis ,monitor ,day ,week ,python ,json ,curl

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}