Skip to content

Instantly share code, notes, and snippets.

@cobookman
Last active October 2, 2018 05:51
Show Gist options
  • Save cobookman/424670df4c1dca6e5c75a8df7b62e760 to your computer and use it in GitHub Desktop.
Save cobookman/424670df4c1dca6e5c75a8df7b62e760 to your computer and use it in GitHub Desktop.
while true
do
./test.py >> log.txt
done
#!/usr/bin/python3
import requests
import datetime
import time
import sys
from google.cloud import bigquery
ZONE = "us-central1-a"
ENDPOINTS = [
"http://snap-tests-217018.appspot.com/hello",
"http://snap-tests-217018.appspot.com/file",
]
BQ = bigquery.Client()
TABLE_REF = BQ.dataset("latency").table("measures")
TABLE = BQ.get_table(TABLE_REF) # API request
def load_test(endpoint):
"""Times latency to hit endpoint & logs into BQ."""
response = requests.get(endpoint)
rows = [(
ZONE,
datetime.datetime.utcnow().isoformat(),
response.elapsed.total_seconds(),
endpoint)]
errors = BQ.insert_rows(TABLE, rows)
if len(errors) != 0:
print(errors, file=sys.stderr)
else:
print("probe: %s - %s - %f - %s" % (rows[0][0], rows[0][1], rows[0][2], rows[0][3]))
def main():
while True:
for endpoint in ENDPOINTS:
load_test(endpoint)
sys.stdout.flush()
sys.stderr.flush()
time.sleep(1)
if __name__ == "__main__":
main()
sudo apt-get update \
&& sudo apt-get upgrade -y \
&& sudo apt-get dist-upgrade \
&& sudo apt-get install python3-pip -y \
&& pip3 install google-cloud-bigquery \
&& wget https://gist.githubusercontent.com/cobookman/424670df4c1dca6e5c75a8df7b62e760/raw/eba0bfec3871089446ce6f31efbf066b83ed150f/forever.sh \
&& wget https://gist.githubusercontent.com/cobookman/424670df4c1dca6e5c75a8df7b62e760/raw/eba0bfec3871089446ce6f31efbf066b83ed150f/test.py \
&& chmod +x * \
&& sudo reboot -n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment