Skip to content

Instantly share code, notes, and snippets.

@krischer
Created February 10, 2016 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krischer/9d9a4bd078a8e64675f2 to your computer and use it in GitHub Desktop.
Save krischer/9d9a4bd078a8e64675f2 to your computer and use it in GitHub Desktop.
Performace Tester for Syngine
# Requires Python 3 and numpy and requests.
# `ping` and `curl` must be available and usable
#
# Measured times are the time to the first received
# byte of the response.
from __future__ import print_function
import math
import os
import random
import re
import subprocess
import numpy as np
import requests
IRIS_URL = "service.iris.edu"
REPEAT = 10
def ttfb(url):
"""
Estimate the time to first received byte.
"""
return float(subprocess.check_output([
'curl', '-s', '-o', '/dev/null', '-w', '%{time_starttransfer}',
url]).decode())
if __name__ == "__main__":
print("Testing Syngine Performance.")
print("")
print(" Printed timings are TTFBs, e.g. the time until the first byte of "
"the response is received.")
print("")
results = {}
print("-> Estimating ping to '%s'..." % IRIS_URL, end=" ")
_t = subprocess.check_output(
['ping', '-c10', '-i0.2', IRIS_URL]).decode().splitlines()[-1].strip()
ping = float(re.search(
r"\d+\.\d+/(\d+\.\d+)/\d+\.\d+/\d+\.\d+", _t).group(1))
print("%.3f ms" % ping)
models = requests.get(
"http://service.iris.edu/irisws/syngine/1/models").json()
print("-> Found %i earth models." % len(models))
for name in sorted(models.keys()):
info = models[name]
print("\n-> Benchmarking model %s:" % name)
dt = math.floor(float(info["max_sampling_period"]) * 1E4) / 1E4
max_event_depth = info["max_event_depth"]
components = "ZNE" if "horizontal" in info["components"] else "Z"
model_results = []
results[name] = model_results
for _i in range(REPEAT):
# random lat/lng accounting for latitudinal variations.
src_lat = math.degrees(math.asin(2 * random.random() - 1))
src_lng = random.uniform(-180.0, 180.0)
rec_lat = math.degrees(math.asin(2 * random.random() - 1))
rec_lng = random.uniform(-180.0, 180.0)
src_dep = random.uniform(0, max_event_depth)
print(" Testing SRC:%.1f/%.1f/%.1f REC:%.1f/%.1f ..." % (
src_lat, src_lng, src_dep, rec_lat, rec_lng), end=" ")
url = ("http://service.iris.edu/irisws/syngine/1/query?"
"model={name}&"
"sourcelatitude={src_lat}&"
"sourcelongitude={src_lng}&"
"sourcedepthinmeters={src_dep}&"
"receiverlatitude={rec_lat}&"
"receiverlongitude={rec_lng}&"
"components={components}&"
"format=miniseed&"
"dt={dt}&kernelwidth=4").format(
name=name,
src_lat=src_lat,
src_lng=src_lng,
src_dep=src_dep,
rec_lat=rec_lat,
rec_lng=rec_lng,
components=components,
dt=dt)
ttfb_time = (ttfb(url))
model_results.append(ttfb_time)
print("%.3f seconds; %.3f seconds accounting for ping" % (
ttfb_time, ttfb_time - ping / 1000.0))
print("\n============================================")
print("============================================")
print("Results")
print("============================================")
for name in sorted(results.keys()):
values = np.array(results[name])
print("Model %10s : min/avg/max/stddev = "
"%5.2f/%5.2f/%5.2f/%5.2f sec" % (
name, values.min(), values.mean(), values.max(), values.std()))
values -= ping / 1000.0
print(" --> accounting for ping: min/avg/max/stddev = "
"%5.2f/%5.2f/%5.2f/%5.2f sec" % (
values.min(), values.mean(), values.max(), values.std()))
print("")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment