A Simple CDN Speed Test Script in Python

Written by productivityhacks | Published 2021/08/29
Tech Story Tags: engineering | python | testing | development

TLDR There is a simple python script I use to speed test CDNs. It’s open-sourced under the MIT license. The repository is: https://://github.com/speedtestdemon/speed-tests and the python script is “test.py” The script takes a single input “URL” and tests cold cache, hot cache, cold cache and warm cache. The response headers are important to see the header that reports ‘Hit’, ‘Miss’ or ‘RefreshHit”via the TL;DR App

Here is a simple python script I use to speed test CDNs. It’s open-sourced under the MIT license.

The repository is: https://github.com/speedtestdemon/speed-tests and the python script is “test.py”.

The script takes a single input “URL”. Example usage: `python3 test.py

https://d20zaq59cm4c4j.cloudfront.net/wp-content/uploads/2021/05/cf.png`.

Here are the script outputs:

  1. Cold cache, hot cache, and warm cache tests (in that order).
  2. The response headers for cold cache and warm cache tests. This is important to see the header that reports “Hit”, “Miss”, or “RefreshHit”. It ensures that you’re running the cold cache and warm cache tests correctly.
  3. The hot cache times are averages of 10 curls.

Here is an example output of the python script:

\-------------------------------------------------------------  
Testing "Cold cache speed"  
\-------------------------------------------------------------  
Got headers:  
HTTP/2 200  
content-type: image/png  
content-length: 719983  
date: Fri, 25 Jun 2021 15:01:17 GMT  
last-modified: Mon, 07 Jun 2021 00:16:21 GMT  
etag: "52ae2ff2354d4a68e680b77b4da58985"  
accept-ranges: bytes  
server: AmazonS3  
x-cache: Miss from cloudfront  
via: 1.1 9e8d1ff1eee770054f1561535ab320b2.cloudfront.net (CloudFront)  
x-amz-cf-pop: DFW55-C3  
x-amz-cf-id: lQPqV7PnAuwl\_52kDXS2rsC20kSrloThTOG8cN1fK-MHUce0NNIDIA==time\_namelookup: 0.12467000000000000304  
time\_connect: 0.02141099999999998560  
time\_appconnect: 0.06681200000000001027  
time\_pretransfer: 0.00020100000000000673  
time\_redirect: 0.00000000000000000000  
time\_starttransfer: 1.02790999999999987935  
time to download: 1.06810300000000002463  
time\_total: 2.30910700000000002063  
\-------------------------------------------------------------  
Testing "Hot cache speed"  
\-------------------------------------------------------------  
10 requests done. Average:  
time\_namelookup: 0.00173870000000000036  
time\_connect: 0.02068409999999999693  
time\_appconnect: 0.06124410000000000270  
time\_pretransfer: 0.00024639999999999802  
time\_redirect: 0.00000000000000000000  
time\_starttransfer: 0.03418949999999999767  
time to download: 0.21361620000000000608  
time\_total: 0.33171899999999998609  
\-------------------------------------------------------------  
Testing "Warm cache speed"  
\-------------------------------------------------------------  
Sleeping for 0.5 hr to move cache from hot to warm  
Got headers:  
HTTP/2 200  
content-type: image/png  
content-length: 719983  
date: Fri, 25 Jun 2021 15:01:17 GMT  
last-modified: Mon, 07 Jun 2021 00:16:21 GMT  
etag: "52ae2ff2354d4a68e680b77b4da58985"  
accept-ranges: bytes  
server: AmazonS3  
x-cache: Hit from cloudfront  
via: 1.1 7c556a1545ad08a01c9e86ad41ac73a1.cloudfront.net (CloudFront)  
x-amz-cf-pop: DFW55-C3  
x-amz-cf-id: ACpfQ14wbO\_vjjAisApBAJkRphZQ7IbD4Lawc77eo\_K5UM\_3-yWPZw==  
age: 1804time\_namelookup: 0.05657399999999999929  
time\_connect: 0.02411100000000000743  
time\_appconnect: 0.07374400000000000399  
time\_pretransfer: 0.00145299999999998208  
time\_redirect: 0.00000000000000000000  
time\_starttransfer: 0.02921799999999999398  
time to download: 0.22752000000000000002  
time\_total: 0.41261999999999998678

Also published on https://speedtestdemon.com/python-cdn-speed-test-script/.


Written by productivityhacks | Software engineer blogging about productivity hacks, and analyzing software performance
Published by HackerNoon on 2021/08/29