Skip to content

Commit 9c17371

Browse files
Add node to spin output, reformat with black
See https://github.com/ambv/black
1 parent f404c2e commit 9c17371

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

src/run.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
from spin import spin
66
from bottle import run, default_app
77

8-
run(host='localhost', port=8080, debug=True)
8+
run(host="localhost", port=8080, debug=True)

src/spin.py

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22
"""This module spins the CPU."""
33
import os
44
import time
5-
import newrelic.agent
65
import random
6+
import socket
7+
import urllib2
8+
import newrelic.agent
79
from bottle import route, default_app, response, HTTPError
810

9-
newrelic_ini = '../newrelic.ini'
10-
if os.path.isfile(newrelic_ini):
11-
newrelic.agent.initialize(newrelic_ini)
11+
NEWRELIC_INI = "../newrelic.ini"
12+
if os.path.isfile(NEWRELIC_INI):
13+
newrelic.agent.initialize(NEWRELIC_INI)
1214

1315

14-
@route('/spin')
15-
def spin(delay=0.1):
16+
@route("/spin")
17+
def spin(delay=0.1, max_duration=10.0, simulate_congestion=True):
1618
"""Spin the CPU, return the process id at the end"""
1719
spin.invocations += 1
1820
child_pid = os.getpid()
@@ -25,28 +27,43 @@ def spin(delay=0.1):
2527
pareto_factor = random.paretovariate(alpha)
2628
start_time = time.time()
2729

28-
2930
current_time = start_time
3031
scratch = 42 + int(current_time)
31-
congestion_slowdown = delay * 10 / (current_time - spin.last_time)
32+
congestion_slowdown = 0.0
33+
if simulate_congestion:
34+
congestion_slowdown = delay * 10 / (current_time - spin.last_time)
3235
end_time = start_time + (delay + congestion_slowdown) * pareto_factor
33-
time_limit = start_time + (delay * 100)
36+
time_limit = start_time + (max_duration)
3437
calcs = 0
3538
while current_time < end_time:
3639
calcs += 1
3740
scratch = (scratch * scratch) % upper_max
3841
current_time = time.time()
3942
interval = current_time - start_time
4043
if current_time > time_limit:
41-
raise HTTPError(500, "Allowed transaction time exceeded ({} ms elapsed)".format(interval))
44+
raise HTTPError(
45+
500,
46+
"Allowed transaction time exceeded ({} ms elapsed)".format(interval),
47+
)
4248
spin.last_time = current_time
4349
rate = calcs / interval
44-
response.set_header('Content-Type', 'text/plain')
45-
return ('pid {0} spun {1} times over {2}s (rate {3} invoked {4} times/s)\n'
46-
.format(child_pid, calcs, interval, rate, spin.invocations))
50+
response.set_header("Content-Type", "text/plain")
51+
return "node {0} pid {1} spun {2} times over {3}s (rate {4} invoked {5} times/s)\n".format(
52+
spin.node, child_pid, calcs, interval, rate, spin.invocations
53+
)
54+
4755

4856
spin.invocations = 0
4957
spin.last_time = time.time() - 10
5058
spin.slowdown = 0
59+
try:
60+
# Thanks stack overflow https://stackoverflow.com/a/43816449/424301
61+
spin.node = urllib2.urlopen(
62+
"http://169.254.169.254/latest/meta-data/instance-id", timeout=1
63+
).read()
64+
# Thanks stack overflow https://stackoverflow.com/questions/2712524/handling-urllib2s-timeout-python
65+
except urllib2.URLError:
66+
# Thanks stack overflow: https://stackoverflow.com/a/4271755/424301
67+
spin.node = socket.gethostname()
5168

5269
application = default_app()

0 commit comments

Comments
 (0)