-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.py
67 lines (54 loc) · 2.22 KB
/
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
66
67
import simpy
import random, time
from pulpy.system import *
from pulpy.machines import RouterLeastCongested, Constrained_Machine
from pulpy.offline import Controller
from pulpy.alloc import Allocator
def example_1():
# Example for system of num_machines parallel request processing machines,
# with a least congested load balancer (Router) in front of them providing
# service to requests generated by a single source. Which machine can
# process which type of request is controlled by an allocator object.
# The main controller coordinates the allocator and the load balancer.
# Simulation parameters
num_machines = 100
catalog_size = 500
verbose = False
simulated_time = 1000
# Create a common context
env = simpy.Environment()
print("Initialize catalog...")
catalog = build_catalog(catalog_size)
monitor = Monitor(env) # keeps metrics
ctx = Context( env, monitor, catalog)
DefaultContextUser.set_default_context(ctx)
# Create request processing machines
machines = []
print("Initialize machines...")
for i in range(num_machines):
s = Constrained_Machine(name=f"MACH_{i}", context = ctx, bandwidth = 10, space_capacity=10)
machines.append(s)
# Instantiate Load balancer.
allocator= Allocator(machines, catalog=ctx.catalog, verbose = verbose)
load_balancer = RouterLeastCongested(context = ctx, machines=machines, name= "MAIN_ROUTER", \
alloc_map = allocator.allocation_map)
# Requests source`
src = Source(context = ctx, intensity = 10)
# instruct the source to send its requests to the load balancer
env.process(src.send_requests(load_balancer))
# Controller
Controller(ctx, allocator, load_balancer, verbose = verbose)
# Let's go!
print("Run sim...")
start = time.time()
env.run(until=simulated_time)
print("Simulation finished!\n")
# Print stats
print("data: ", monitor.data)
if verbose:
print("data by name: ", monitor.data_by_name)
elapsed_time = time.time() - start
print("elapsed real time:", elapsed_time, " simulated ", src.n, " requests. ( ", src.n/elapsed_time,"reqs/s)")
print()
if __name__ == "__main__":
example_1()