-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgo_test.py
65 lines (55 loc) · 1.77 KB
/
algo_test.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
#!/usr/bin/env python
# -*- coding: utf8 -*-
videos = [50, 50, 80, 30, 110]
endpoints = []
caches = []
c_size = 100
class Endpoint(object):
"""docstring for Endpoint."""
def __init__(self, dc, cache):
super(Endpoint, self).__init__()
self.dc_latency = dc
self.cache_latencies = cache
self.video_requests = {}
def set_video(self, videos):
self.video_requests = videos
def pondera(self, cache):
# Pondera cada video usando las latencias de cada cache y las requests de los videos para el endpoint #
dictionary = {}
for video in self.video_requests:
dictionary[video] = float(self.video_requests[video])/float(self.cache_latencies[cache])
return dictionary
class Cache(object):
"""docstring for Cache."""
def __init__(self, v):
super(Cache, self).__init__()
self.videos_size = v
def main():
endpoints.append(Endpoint(1000, {0:1000, 1:200, 2:300}))
endpoints[0].set_video({3:1500, 4:500, 1:1000})
endpoints.append(Endpoint(500, {1:100}))
endpoints[1].set_video({0:1000, 4:500})
# Get least latency cache.
key = 0
cache = 999999999
for ep in endpoints:
tmp = ep.cache_latencies
if not tmp:
continue
tmp_k = min(tmp, key=tmp.get)
if tmp[tmp_k] < cache:
key = tmp_k
cache = tmp[tmp_k]
#print min(ep.cache_latencies, key=ep.cache_latencies.get)
rl_pond = {}
for ep in endpoints:
if key in ep.cache_latencies:
tmp = ep.pondera(key)
for k in tmp:
if k in rl_pond:
rl_pond[k] += tmp[k]
else:
rl_pond[k] = tmp[k]
print rl_pond
if __name__ == "__main__":
main()