queueing.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. Application to Queueing Systems
  2. ===============================
  3. To present a more realistic model and highlight the potential for performance analysis, we present a simple queueing system.
  4. While a lot has been done in queueing theory, we present simulation as an alternative to the mathematical solutions.
  5. Even though the mathematical solutions have their advantages, simulation offers more flexibility and doesn't get that complex.
  6. It is, however, necessarily limited to sampling: simulations will only take samples and will therefore generally not find rare and exceptional cases.
  7. Not taking them into account is fine in many situations, as it is now in our example model.
  8. In this section, we present a simple queueing problem.
  9. Variations on this model --- in either its behaviour, structure, or parameters --- are easy to do.
  10. Problem Description
  11. -------------------
  12. In this example we model the behaviour of a simple queue that gets served by multiple processors.
  13. Implementations of this queueing systems are widespread, such as for example at airport security.
  14. Our model is parameterizable in several ways: we can define the random distribution used for event generation times and event size, the number of processors, performance of each individual processor, and the scheduling policy of the queue when selecting a processor.
  15. Clearly, it is easier to implement this, and all its variants, in DEVS than it is to model mathematically.
  16. For our performance analysis, we show the influence of the number of processors (e.g., metal detectors) on the average and maximal queueing time of jobs (e.g., travellers).
  17. A model of this system is shown next.
  18. .. image:: queue.svg
  19. :alt: Queue system with a single generator, single queue, and *n* processors.
  20. :width: 50%
  21. Events (people) are generated by a generator using some distribution function.
  22. They enter the queue, which decides the processor that they will be sent to.
  23. If multiple processors are available, it picks the processor that has been idle for the longest; if no processors are available, the event is queued until a processor becomes available.
  24. The queue works First-In-First-Out (FIFO) in case multiple events are queueing.
  25. For a processor to signal that it is available, it needs to signal the queue.
  26. The queue keeps track of available processors.
  27. When an event arrives at a processor, it is processed for some time, depending on the size of the event and the performance characteristics of the processor.
  28. After processing, the processor signals the queue and sends out the event that was being processed.
  29. Description in DEVS
  30. -------------------
  31. While examples could be given purely in their formal description, they would not be executable and would introduce a significant amount of accidental complexity.
  32. To specify this model, we first define the event exchanged between different examples: the Job.
  33. A job is coded as a class ``Job``.
  34. It has the attributes ``size`` (i.e., indicative of processing time) and ``creation time`` (i.e., time the event was created, for statistic gathering).
  35. The ``Job`` class definition is shown next and can de downloaded: :download:`job.py <../examples/queueing/job.py>`.
  36. .. literalinclude:: ../examples/queueing/job.py
  37. We now focus on each atomic model seperately, starting at the event generator.
  38. The *generator* is defined as an atomic model using the class ``Generator``.
  39. Classes that represent an atomic model inherit from the ``AtomicDEVS`` class.
  40. They should implement methods that implement each of the DEVS components.
  41. Default implementations are provided for a passivated model, such that unused functions don't need to be defined.
  42. In the constructor, input and output ports are defined, as well as model parameters and the initial state.
  43. We see that the definition of the generator is very simple: we compute the time remaining until the next event (``remaining``), and decrement the number of events to send.
  44. The generator also keeps track of the current simulation time, in order to set the creation time of events.
  45. The time advance function returns the time remaining until the next internal transition.
  46. Finally, the output function returns a new customer event with a randomly defined size.
  47. The job has an attribute containing the time at which it was generated.
  48. Recall, however, that the output function was invoked before the internal transition, so the current time has not yet been updated by the internal transition.
  49. Therefore, the output function also has to do this addition, without storing the result in the state (as it cannot write to the state).
  50. The ``Generator`` class definition is shown next and can de downloaded: :download:`generator.py <../examples/queueing/generator.py>`.
  51. .. literalinclude:: ../examples/queueing/generator.py
  52. Next up is the queue, which is the most interesting component of the simulation, as it is the part we wish to analyze.
  53. The ``Queue`` implementation is similar in structure to the ``Generator``.
  54. Of course, the \DEVS parts get a different specification, as shown in Listing~\ref{lst:queue}.
  55. The queue takes a structural parameter, specifying the number of processors.
  56. This is needed since the queue has an output port for each processor.
  57. When an internal transition happens, the queue knows that it has just output an event to the first idle processor.
  58. It thus marks the first idle processor as busy, and removes the event it was currently processing.
  59. If there are events remaining in the queue, and a processor is available to process it, we process the first element from the queue and set the ``remaining\_time`` counter.
  60. In the external transition, we check the port we received the event on.
  61. Either it is a signal of the processor to indicate that it has finished, or else it is a new event to queue.
  62. In the former case, we mark the processor that sent the event as idle, and potentially process a queued message.
  63. For this to work, the processor should include its ID in the event, as otherwise the queue has no idea who sent this message.
  64. In the latter case, we either process the event immediately if there are idle processors, or we store it in the queue.
  65. The time advance merely has to return the ``remaining\_time`` counter that is managed in both transition functions.
  66. Finally in the output function, the model outputs the first queued event to the first available processor.
  67. Note that we can only read the events and processors, and cannot modify these lists: state modification is reserved for the transition functions.
  68. An important consideration in this model is the ``remaining\_time`` counter, which indicates how much time remains before the event is processed.
  69. We can't simply put the processing time of events in the time advance, as interrupts could happen during this time.
  70. When an interrupt happens (e.g., another event arrives), the time advance is invoked again, and would return the total processing time, instead of the remaining time to process the event.
  71. To solve this problem, we maintain a counter that explicitly gets decremented when an external interrupt happens.
  72. The ``Queue`` class definition is shown next and can de downloaded: :download:`queue.py <../examples/queueing/queue.py>`.
  73. .. literalinclude:: ../examples/queueing/queue.py
  74. The next atomic model is the ``Processor`` class.
  75. It merely receives an incoming event and starts processing it.
  76. Processing time, computed upon receiving an event in the external transition, is dependent on the size of the task, but takes into account the processing speed and a minimum amount of processing that needs to be done.
  77. After the task is processed, we trigger our output function and internal transition function.
  78. We need to send out two events: one containing the job that was processed, and one to signal the queue that we have become available.
  79. For this, two different ports are used.
  80. Note that the definition of the processor would not be this simple in case there was no queue before it.
  81. We can now make the assumption that when we get an event, we are already idle and therefore don't need to queue new incoming events first.
  82. The ``Processor`` class definition is shown next and can de downloaded: :download:`processor.py <../examples/queueing/processor.py>`.
  83. .. literalinclude:: ../examples/queueing/processor.py
  84. The processor finally sends the task to the ``Collector`` class.
  85. The collector is an artificial component that is not present in the system being modeled; it is only used for statistics gathering.
  86. For each job, it stores the time in the queue.
  87. The ``Collector`` class definition is shown next and can de downloaded: :download:`collector.py <../examples/queueing/collector.py>`.
  88. .. literalinclude:: ../examples/queueing/collector.py
  89. With all atomic examples defined, we only have to couple them together in a coupled model: the ``System``.
  90. In this system, we instantiate a generator, queue, and collector, as well as a variable number of processors.
  91. The number of processors is variable, but is still static during simulation.
  92. The couplings also depend on the number of processors, as each processor is connected to the queue and the collector.
  93. The ``System`` class definition is shown next and can de downloaded: :download:`system.py <../examples/queueing/system.py>`.
  94. .. literalinclude:: ../examples/queueing/system.py
  95. Now that our DEVS model is completely specified, we can start running simulations on it.
  96. Simulation requires an *experiment* file though, which initializes the model with parameters and defines the simulation configuration.
  97. The experiment writes out the raw queueing times to a Comma Seperated Value (CSV) file.
  98. An experiment file often contains some configuration of the simulation tool, which differs for each tool.
  99. The experiment file is shown next and can de downloaded: :download:`experiment.py <../examples/queueing/experiment.py>`.
  100. .. literalinclude:: ../examples/queueing/experiment.py
  101. Performance Analysis
  102. --------------------
  103. After the definition of our DEVS model and experiment, we of course still need to run the simulation.
  104. Simply by executing the experiment file, the CSV file is generated, and can be analyzed in a spreadsheet tool or plotting library.
  105. Depending on the data stored during simulation, analysis can show the average queueing times, maximal queueing times, number of events, processor utilization, and so on.
  106. Corresponding to our initial goal, we perform the simulation in order to find out the influence of opening multiple processors on the average and maximum queueing time.
  107. First, we show the evolution of the waiting time for subsequent clients.
  108. .. image:: queueing_evolution.svg
  109. :alt: Evolution of queueing times for subsequent customers.
  110. :width: 50%
  111. The same results can be visualized with boxplots.
  112. .. image:: queueing_boxplot.svg
  113. :alt: Boxplot of queueing times for varying number of active processors.
  114. :width: 50%
  115. These results indicate that while two processors are able to handle the load, maximum waiting time is rather high: a median of 200 seconds and a maximum of around 470 seconds.
  116. When a single additional processor is added, average waiting time decreases significantly, and the maximum waiting time also becomes tolerable: the mean job is served immediately, with 75% of jobs being handled within 25 seconds.
  117. Further adding processors still has a positive effect on queueing times, but the effect might not warrant the increased cost in opening processors: apart from some exceptions, all customers are processed immediately starting from four processors.
  118. Ideally, a cost function would be defined to quantize the value (or dissatisfaction) of waiting jobs, and compare this to the cost of adding additional processors.
  119. We can then optimize that cost function to find out the ideal balance between paying more for additional processors and losing money due to long job processing times.
  120. Of course, this ideal balance depends on several factors, including our model configuration and the cost function used.