-
Notifications
You must be signed in to change notification settings - Fork 2
/
model.py
68 lines (58 loc) · 2.02 KB
/
model.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
import random
from datetime import datetime, timedelta
from SimPy.Simulation import initialize, simulate, hold, Process, now
from guppy import hpy
class DummyProcess(Process):
"""A process that does nothing for one second"""
def pem(self):
yield hold, self, 1
class Model(object):
class Generator(Process):
def generate(self, model):
while 1:
yield hold, self, model.inter_arrival_time()
model.new_process()
class StatCollector(Process):
def collect(self, model, n=300.0):
interval = model.runtime / n
tlast = 0
while 1:
yield hold, self, interval
model.collect_stats(tlast)
tlast = now()
class Progress(Process):
def report(self, until):
h = hpy()
start = datetime.now()
last = 0
t = until / 20
x = 5
while 1:
yield hold, self, t
now = datetime.now()
elapsed = now - start
guess = elapsed.seconds / float(x) * 100
eta = start + timedelta(seconds=guess)
print "[%s] %02d%% [ETA: %s]" % (now.strftime('%H:%M:%S'),
x,
eta.strftime('%H:%M:%S'))
x += 5
def setup():
pass
def start(self, *a, **kw):
g = self.Generator('generator')
g.start(g.generate(self))
p = self.Progress('progress')
p.start(p.report(self.runtime))
c = self.StatCollector('stats')
c.start(c.collect(self))
def new_process(self):
"""Generates new process entering the system"""
p = DummyProcess("dummy process")
p.start(p.pem())
def run(self, *a, **kw):
kw['until'] = kw.get('until', getattr(self, 'runtime', 100))
self.runtime = kw['until']
initialize()
self.start(*a, **kw)
simulate(*a, **kw)