quoracle/examples/plot_node_loads.py

56 lines
2 KiB
Python
Raw Permalink Normal View History

2021-02-07 20:19:08 +00:00
"""
This script shows how to use plot_node_load_on, plot_node_utilization, and
plot_node_throughput to plot the load, utilization, and throughput of nodes in
a read-write quorum system.
"""
2021-02-05 04:59:10 +00:00
from quoracle import *
2021-02-07 20:19:08 +00:00
import argparse
2021-02-01 06:59:02 +00:00
import datetime
2021-01-28 21:37:38 +00:00
import matplotlib
import matplotlib.pyplot as plt
2021-01-29 07:47:41 +00:00
2021-02-07 20:19:08 +00:00
def main(output_filename: str) -> None:
2021-01-28 21:37:38 +00:00
a = Node('a', write_capacity=1000, read_capacity=10000)
b = Node('b', write_capacity=500, read_capacity=5000)
c = Node('c', write_capacity=1000, read_capacity=10000)
d = Node('d', write_capacity=500, read_capacity=5000)
e = Node('e', write_capacity=1000, read_capacity=10000)
2021-02-01 06:59:02 +00:00
fr = 0.9
2021-01-29 07:47:41 +00:00
nodes = [a, b, c, d, e]
2021-01-28 21:37:38 +00:00
simple_majority = QuorumSystem(reads=majority([a, b, c, d, e]))
crumbling_walls = QuorumSystem(reads=a*b + c*d*e)
paths = QuorumSystem(reads=a*b + a*c*e + d*e + d*c*b)
opt, _ = search(nodes, read_fraction=fr, timeout=datetime.timedelta(seconds=9))
2021-01-28 21:37:38 +00:00
2021-02-01 06:59:02 +00:00
fig, ax = plt.subplots(3, 4, figsize = (6.3 * 2, 4.8 * 2), sharey='row')
for i, qs in enumerate([simple_majority, crumbling_walls, paths, opt]):
2021-01-28 21:37:38 +00:00
sigma = qs.strategy(read_fraction=fr)
2021-02-01 06:59:02 +00:00
print(qs, sigma.capacity(read_fraction=fr))
2021-01-29 07:47:41 +00:00
plot_node_load_on(ax[0][i], sigma, nodes=nodes, read_fraction=fr)
plot_node_utilization_on(ax[1][i], sigma, nodes=nodes, read_fraction=fr)
plot_node_throughput_on(ax[2][i], sigma, nodes=nodes, read_fraction=fr)
2021-01-28 21:37:38 +00:00
ax[0][0].set_title('Simple Majority')
ax[0][1].set_title('Crumbling Walls')
ax[0][2].set_title('Paths')
2021-02-01 06:59:02 +00:00
ax[0][3].set_title(f'Opt {opt.reads}')
2021-01-28 21:37:38 +00:00
ax[0][0].set_ylabel('Load')
2021-02-07 20:19:08 +00:00
ax[1][0].set_ylabel('Utilization')
ax[2][0].set_ylabel('Throughput')
2021-01-28 21:37:38 +00:00
fig.tight_layout()
2021-02-07 20:19:08 +00:00
fig.savefig(output_filename)
print(f'Wrote figure to "{output_filename}".')
2021-01-28 21:37:38 +00:00
2021-01-29 07:47:41 +00:00
2021-01-28 21:37:38 +00:00
if __name__ == '__main__':
2021-02-07 20:19:08 +00:00
parser = argparse.ArgumentParser()
parser.add_argument('--output',
type=str,
default='node_loads.pdf',
help='Output filename')
args = parser.parse_args()
main(args.output)