~thomas-philipp/openwns-systemtest-library/queueing-experimental

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
# import the necessary modules

# openwns contains the Simulator class, which is needed for every
# simulation
import openwns

# openws.queuingsystem contains the simulation model called
# "SimpleMM1" which is used in this example
import openwns.queuingsystem

import openwns.probebus

### Simulation setup
#
# Q: queue with unlimited size
#
# W: worker, the job processing time is negative-exponentially
#    distributed
#
# The jobs arrive at the system with an inter arrival time that is
# negative-exponentially distributed.
#
#             ----
# new jobs --> Q |-->(W)-->
#             ----
#

class StatisticsProbeBus(openwns.probebus.PythonProbeBus):

    def __init__(self, outputFilename):
        openwns.probebus.PythonProbeBus.__init__(self, self.accepts, self.onMeasurement, self.output)
        self.outputFilename = outputFilename
        self.sum = 0.0
        self.trials = 0

    def accepts(self, time, context):
        return True

    def onMeasurement(self, time, measurement, context):
        self.sum += measurement
        self.trials += 1

    def output(self):
        f = open(self.outputFilename, "w")
        f.write("Number of trials: %s\n" % str(self.trials))
        f.write("Mean value : %s\n" % str(self.sum/self.trials))
        f.close()

# create the M/M/1 (step4) simulation model configuration (time in seconds)
# we reuse step3 and only change the configuration!
mm1 = openwns.queuingsystem.SimpleMM1Step5(meanJobInterArrivalTime = 0.100,
                                           meanJobProcessingTime   = 0.099)

# Replace the default LoggingProbeBus configured in SimpleMM1Step3 by
# our StatisticsProbeBus
statisticsProbeBus = StatisticsProbeBus("SimpleMM1Step5.output")
loggingProbeBus = openwns.probebus.LoggingProbeBus()

# create simulator configuration
sim = openwns.Simulator(simulationModel = mm1,
                        maxSimTime      = 100.0)

sim.eventSchedulerMonitor = None

# If an output directory is already present it will be deleted 
# if you change this to MOVE a present output directory will
# be ranamed 
sim.outputStrategy = openwns.simulator.OutputStrategy.DELETE

pbr = sim.environment.probeBusRegistry

pbr.getMeasurementSource("openwns.queuingsystem.MM1.sojournTime").addObserver(statisticsProbeBus)

pbr.getMeasurementSource("openwns.queuingsystem.MM1.sojournTime").addObserver(loggingProbeBus)

# set the configuration for this simulation
openwns.setSimulator(sim)