-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathqos.py
145 lines (101 loc) · 4.01 KB
/
qos.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import random as rnd
import venv
import numpy as np
import json
class ProbabilityDistribution():
def __init__(self, probabilities = [1], values = [0]):
self.probabilities = probabilities
self.values = values
self.value = -1.0
def sample_value(self):
self.value = float(np.random.choice(self.values, p = self.probabilities))
def __repr__(self):
return json.dumps({"probabilities": self.probabilities, "values": self.values, "value": self.value})
def __dict__(self):
return {"probabilities": self.probabilities, "values": self.values, "value": self.value}
# #TESTS
# p = ProbabilityDistribution([0.5, 0.5], [1.0, 0.0])
# for i in range(0,10):
# p.sample_value()
# print(p.value)
# print(p)
class HardwareResources():
def __init__(self, ram = ProbabilityDistribution(), hdd = ProbabilityDistribution(), cpu = ProbabilityDistribution() ):
self.ram = ram
self.hdd = hdd
self.cpu = cpu
def sample_resources(self):
self.ram.sample_value()
self.cpu.sample_value()
self.hdd.sample_value()
def get_ram(self):
return self.ram.value
def get_hdd(self):
return self.hdd.value
def get_cpu(self):
return self.cpu.value
class Node():
def __init__(self, node_id = '', resources = HardwareResources(), software = []):
self.node_id = node_id
self.resources = resources
self.software = software
self.used_ram = 0
self.used_hdd = 0
self.used_cpu = 0
def sample_resources(self):
self.resources.sample_resources()
def get_available_ram(self):
return self.resources.get_ram() - self.used_ram
def get_available_hdd(self):
return self.resources.get_hdd() - self.used_hdd
def get_available_cpu(self):
return self.resources.get_cpu() - self.used_cpu
class QoSProfile:
def __init__(self, bandwidth_ab = ProbabilityDistribution(), bandwidth_ba = ProbabilityDistribution(), latency = ProbabilityDistribution()):
self.bandwidth_ab = bandwidth_ab
self.bandwidth_ba = bandwidth_ba
self.latency = latency
def sample_qos(self):
self.bandwidth_ab.sample_value()
self.bandwidth_ba.sample_value()
self.latency.sample_value()
def get_bandwidth_ab(self):
return self.bandwidth_ab.value
def get_bandwidth_ba(self):
return self.bandwidth_ba.value
def get_latency(self):
return self.latency.value
def __repr__(self):
return str({"bandwidth_ab": self.bandwidth_ab, "bandwidth_ba": self.bandwidth_ba, "latency": self.latency})
# # #TEST
# b_ab = ProbabilityDistribution([0.5, 0.25, 0.25], [12.0, 6.5, 0.0])
# b_ba = ProbabilityDistribution([0.5, 0.25, 0.25], [12.0, 6.0, 0.0])
# l = ProbabilityDistribution([0.5, 0.25, 0.25], [50.0, 60.0, 100.0])
# q = QoSProfile(b_ab, b_ba, l)
# q.sample_qos()
# for i in range(0,100):
# print("***")
# q.sample_qos()
# print(q.get_bandwidth_ba())
# print(q.get_bandwidth_ab())
# print(q.get_latency())
class Link():
def __init__(self, endpoint_a = '', endpoint_b = '', qos_profile = QoSProfile()):
self.endpoint_a = endpoint_a
self.endpoint_b = endpoint_b
self.qos_profile = qos_profile
def sample_qos(self):
self.qos_profile.sample_qos()
def __repr__(self):
b_ab = self.qos_profile.get_bandwidth_ab()
b_ba = self.qos_profile.get_bandwidth_ba()
l = self.qos_profile.get_latency()
return json.dumps({"endpoint_a": self.endpoint_a, "endpoint_b": self.endpoint_b, "qos_profile": {'bandwidth_ab': b_ab, 'bandwidth_ba': b_ba, 'latency': l }})
# link = Link("A", "B", q)
# for i in range(0,10):
# link.sample_qos()
# print(link)
# # print(ProbabilityDistribution().toJSON())
# # def jdefault(o):
# # return o.__dict__
# # print(json.dumps(QoSProfile(), default=jdefault))