Skip to content

Instantly share code, notes, and snippets.

@msoedov
Last active November 26, 2020 12:46
Show Gist options
  • Save msoedov/199aad3873d92e0eca768529ea834e46 to your computer and use it in GitHub Desktop.
Save msoedov/199aad3873d92e0eca768529ea834e46 to your computer and use it in GitHub Desktop.
import random
import itertools
COMPUTE_UNITS_PER_CYCLE = 24
CYCLES = 1000
task_weights = [0.1, 1, 6]
task_prob = [1, .4, .1]
IO_WAIT = -1
def cycle(iterable_factory):
while True:
for element in iterable_factory():
yield element
def consume_until_exhausted(list_iter, n):
total = 0
if not list_iter:
return total, n
for t in list_iter:
val = next(t)
if val == IO_WAIT:
list_iter.remove(t)
continue
elif (val + total) > n:
return total, val
total += val
sub_total, rest = consume_until_exhausted(list_iter, n - total)
return sub_total + total, rest
def scheduler(n, task_type):
utilizations = []
tasks = [cycle(task_type) for _ in range(n)]
left_over = 0
for _ in range(CYCLES):
total, left_over = consume_until_exhausted(
tasks[:], COMPUTE_UNITS_PER_CYCLE - left_over
)
utilizations.append(total)
return utilizations
def monolith():
yield IO_WAIT
yield IO_WAIT
for x, p in zip(task_weights, task_prob):
if p >= random.random():
yield x
else:
break
def microservice():
yield IO_WAIT
yield IO_WAIT
yield task_weights[1]
loss = lambda vec: 1 - sum(vec) / (COMPUTE_UNITS_PER_CYCLE * CYCLES)
for task_type in (microservice, monolith):
utilization = None
n = None
for workers in range(5, 150, 1):
u = scheduler(workers, task_type=task_type)
utilization = utilization or u
if loss(utilization) > loss(u):
utilization = u
n = workers
print("Workers", task_type, n)
print("Min utilization", min(utilization))
print("Max utilization", max(utilization))
print("Avg utilization", sum(utilization) / len(utilization))
print("Utilization %", sum(utilization) / (COMPUTE_UNITS_PER_CYCLE * CYCLES))
print("Idle time %", 1 - sum(utilization) / (COMPUTE_UNITS_PER_CYCLE * CYCLES))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment