Package SimPy :: Module MonitorTest |
[hide private]
[frames] | no frames]

# Source Code for Module SimPy.MonitorTest

      1  #!/usr/bin/env python
      2  from __future__ import generators
      3  from random import *
      4  from SimPy.Simulation import *
      5  from SimPy.SimPlot import SimPlot
      6  import unittest
      7  # $Revision: 1.1.1.18 $ $Date: 2007/01/08 14:45:56 $
      8
      9  """MonitorTest.py
     10  Testing Monitor, Tally.
     11  This may be included in SimPyTest eventually.
     12
     13  Change history:
     14  # 2004 05 03 corrected test for Monitored queues (gav)
     15  # 2005 09 06 added tests for Tally (kgm)
     16  """
     17  __version__="1.8 $Revision: 1.1.1.18 $ $Date: 2007/01/08 14:45:56 $"
     18
     19  ## ------------------------------------------------------------
     20 -class Thing(Process):
     21     """ Thing process for testing Monitors in simulation"""
     22 -   def __init__(self,M=None,name="Thing"):
     23          Process.__init__(self)
     24          self.name=name
     25          self.y = 0.0
     26          self.M = M
     27
     28 -   def execute(self):
     29          DEBUG = 0
     30          self.y = 0.0
     31          if DEBUG: print self.name,now(),self.y
     32          self.M.observe(self.y)
     33
     34          yield hold,self,10.0
     35          self.y = 10
     36          if DEBUG: print self.name,now(),self.y
     37          self.M.observe(self.y)
     38
     39          yield hold,self,10.0
     40          self.y = 5
     41          if DEBUG: print self.name,now(),self.y
     42          self.M.observe(self.y)
     43
     44  ## ------------------------------------------------------------
     45
     46 -class makeMtestCase(unittest.TestCase):
     47      """ Test Monitor
     48      """
     49
     50 -    def setUp(self):
     51          self.M = Monitor(name='First')
     52          for i in range(10):
     53              self.M.observe(2*i,i)
     54          self.T = Monitor(name='tallier')
     55
     56 -    def testObserve(self):
     57          """Test Monitor - observe"""
     58          m = self.M
     59          #for i in m.series():
     60          #   print i
     61          assert m == [[i, 2*i] for i in range(10)],'series wrong'
     62          assert m.name == 'First','name wrong'
     63          assert m.tseries() == list(range(10)),'tseries wrong:%s'%(m.tseries(),)
     64          assert m.yseries() == [2*i for i in range(10)],'yseries wrong:%s'%(m.yseries(),)
     65          assert m.total() == 90, 'total wrong:%s'%m.total()
     66          assert m.mean() == 9.0,'mean wrong:%s'%m.mean()
     67          assert m.var() == (4*285.-(90*90/10.0))/10.0,'sample var wrong:%s'%(m.var(),)
     68
     69 -    def testObserveNoTime(self):
     70          """Test Monitor - observe with time being picked up from now()"""
     71          m = Monitor(name='No time')
     72          initialize()
     73          t = Thing(m)
     74          activate(t,t.execute(),0.0)
     75          simulate(until=20.0)
     76          assert m.yseries() == [0,10,5],'yseries wrong:%s'%(m.yseries(),)
     77          assert m.tseries() == [0,10,20],'tseries wrong:%s'%(m.tseries(),)
     78          assert m.total() == 15, 'total wrong:%s'%m.total()
     79          assert m.timeAverage(10.0) == 5.0 ,'time average is wrong:%s'%m.timeAverage(10.0)
     80
     81 -    def testObserveTally(self):
     82          """Test Monitor - observe without time values"""
     83          m = self.T
     84          for i in range(10):
     85             m.tally(2*i)
     86          assert m == [[0, 2*i] for i in range(10)],'series wrong'
     87          assert m.total() == 90, 'total wrong:%s'%m.total()
     88          assert m.mean() == 9.0,'mean wrong:%s'%m.mean()
     89          assert m.var() == (4*285.-(90*90/10.0))/10.0,'sample var wrong:%s'%(m.var(),)
     90
     91 -    def testtimeAverage(self):
     92         """ test time averages """
     93         m = self.M
     94         assert m == [[i,2*i] for i in range(10)],'series wrong'
     95         assert m.timeAverage(10.0) == 9.0 ,'time average is wrong:%s'%m.timeAverage(10.0)
     96
     97
     98 -    def testreset(self):
     99         """ test time averages """
    100         m=self.M
    101         m.reset(t = 10.0)
    102         assert m.startTime == 10.0,'reset time  wrong'
    103         assert m == [],'reset series wrong:%s'%(m,)
    104
    105
    106 -    def testTally(self):
    107          """Test Monitor - tally"""
    108          m = Monitor(name='First')
    109          S = []
    110          for i in range(10):
    111              m.tally(i)
    112              S.append([0,i])
    113          assert m == S,'Stored series is wrong: %s'%(m,)
    114          assert m.name == 'First','Tally name wrong'
    115          assert m.total() == 45,'Tally total wrong'
    116          assert m.mean() == 4.5,'Tally mean wrong'
    117          assert m.var()  == (285-(45*45/10.0))/10.0,'Tally sample var wrong%s'%(m.var(),)
    118
    119
    120 -    def testAccumulate(self):
    121          """Test Monitor - accumulate"""
    122          #print 'Monitor version '+__version__
    123          m2 = Monitor(name='Second')
    124          assert m2.startTime == 0,'accum startTime wrong'
    125          for i in range(5):
    126              m2.accum(10,i)  # this is (y,t)
    127          # print 'debug', m2.data
    128          assert m2.total() == 50,'accum total wrong:%s'%(m2.total(),)
    129          assert m2.startTime == 0,'accum startTime wrong'
    130          assert m2.timeAverage(5.0) == 10.0,'accum timeAverage wrong:%s'%(m2.timeAverage(10.0),)
    131          ## test reset
    132          m2.reset(10)
    133          assert m2 == [],'accum reset list wrong:%s'%(m2,)
    134          assert m2.total() == 0.0,'accum reset total wrong'
    135          assert m2.startTime == 10,'accum startTime wrong'
    136
    137 -    def testAccumulateInTime(self):
    138          """Test Monitor - accumulate over simulation time"""
    139          #print 'Monitor version '+__version__
    140          initialize()
    141          m3 = Monitor(name='third')
    142          T3 = Thing(name="Job",M=m3)
    143          assert m3.startTime == 0,'Accumulate startTime wrong'
    144          activate(T3,T3.execute(),0.0)
    145          simulate(until=30.0)
    146          assert m3.startTime == 0,'Accumulate startTime wrong'
    147
    148 -    def testListStuff(self):
    149         """Test some Monitor list operations"""
    150         shouldBe=[[i,2*i] for i in range(10)]
    151         assert shouldBe == self.M, 'M list is wrong'
    152         assert [2,4] == self.M[2], 'indexing wrong:%s'%(self.M[2],)
    153         self.M[0] = [10,10]
    154         assert [10,10] == self.M[0], 'item replacement wrong:%s'%(self.M[0],)
    155         self.M.reverse()
    156         assert [10,10] == self.M[-1], 'list reverse wrong:%s'%(self.M[-1],)
    157         self.M.sort()
    158         assert [1,2] == self.M[0], 'list sort wrong:%s'%(self.M[0],)
    159         assert 10 == len(self.M), 'list length wrong'
    160         assert [2,4] in self.M, 'item in list wrong'
    161
    162
    163 -    def testhistogram(self):
    164         """Test Monitor histogram"""
    165         m = Monitor(name='First')
    166         for y in [-5, 0, 5, 15,99,105,120]:m.observe(y)
    167         h = m.histogram(low=0.0,high=100.0,nbins=10)
    168         shouldBe = list(zip(*h)[1])
    169         assert shouldBe == [1,2,1,0,0,0,0,0,0,0,1,2], 'm histogram is wrong: %s'%(shouldBe,)
    170
    171 -def makeMSuite():
    172      suite = unittest.TestSuite()
    173      testObserve = makeMtestCase("testObserve")
    174      testObserveNoTime = makeMtestCase("testObserveNoTime")
    175      testObserveTally = makeMtestCase("testObserveTally")
    176      testtimeAverage = makeMtestCase("testtimeAverage")
    177      testreset = makeMtestCase("testreset")
    178      testTally = makeMtestCase("testTally")
    179      testAccumulate = makeMtestCase("testAccumulate")
    180      testAccumulateInTime = makeMtestCase("testAccumulateInTime")
    181      testListStuff = makeMtestCase("testListStuff")
    182      testhistogram = makeMtestCase("testhistogram")
    183      suite.addTests([testObserve,testObserveNoTime,
    184                      testObserveTally,
    185                      testtimeAverage,testreset,
    186                      testTally,testAccumulate,
    187                      testAccumulateInTime,
    188                      testListStuff,
    189                      testhistogram,
    190                      ])
    191      return suite
    192
    193  ## -----------------------------------------------------------------------
    194  ## Tally test cases
    195  ## -----------------------------------------------------------------------
    196 -class makeTtestCase(unittest.TestCase):
    197      """ Test Tally
    198      """
    199
    200 -    def setUp(self):
    201          self.T = Tally(name='First')
    202          for i in range(10):
    203              self.T.observe(2*i,i)
    204          self.TT = Tally(name='tallier')
    205
    206 -    def testObserve(self):
    207          """Test Tally - observe"""
    208          t = self.T
    209          #for i in m.series():
    210          #   print i
    211          assert t.name == 'First','name wrong'
    212          assert t.total() == 90, 'total wrong:%s'%m.total()
    213          assert t.mean() == 9.0,'mean wrong:%s'%m.mean()
    214          assert t.var() == (4*285.-(90*90/10.0))/10.0,'sample var wrong:%s'%(t.var(),)
    215
    216 -    def testObserveNoTime(self):
    217          """Test Tally - observe with time being picked up from now()"""
    218          ta= Tally(name='No time')
    219          initialize()
    220          t = Thing(ta)
    221          activate(t,t.execute(),0.0)
    222          simulate(until=20.0)
    223          assert ta.total() == 15, 'total wrong:%s'%ta.total()
    224          assert ta.timeAverage(10.0) == 5.0 ,'time average is wrong:%s'%ta.timeAverage(10.0)
    225
    226 -    def testtimeAverage(self):
    227         """ test time averages """
    228         ta= self.T
    229         assert ta.timeAverage(10.0) == 9.0 ,'time average is wrong:%s'%ta.timeAverage(10.0)
    230
    231 -    def testreset(self):
    232         """ test time averages """
    233         ta=self.T
    234         ta.reset(t = 10.0)
    235         assert ta.startTime == 10.0,'reset time  wrong'
    236
    237 -    def testhistogram(self):
    238         """Test some Monitor list operations"""
    239         ta = Monitor(name='First')
    240         for y in [-5, 0, 5, 15,99,105,120]:ta.observe(y)
    241         ta.setHistogram(low=0.0,high=100.0,nbins=10)
    242         h = ta.histogram()
    243         shouldBe = list(zip(*h)[1])
    244         assert shouldBe == [1,2,1,0,0,0,0,0,0,0,1,2], 'm histogram is wrong: %s'%(shouldBe,)
    245
    246 -def makeTSuite():
    247      suite = unittest.TestSuite()
    248      testObserve = makeTtestCase("testObserve")
    249      testObserveNoTime = makeTtestCase("testObserveNoTime")
    250      testtimeAverage = makeTtestCase("testtimeAverage")
    251      testreset = makeTtestCase("testreset")
    252      testhistogram = makeTtestCase("testhistogram")
    253      suite.addTests([testObserve,testObserveNoTime,
    254                      testtimeAverage,
    255                      testreset,
    256                      testhistogram,
    257                      ])
    258      return suite
    259
    260  ## -----------------------------------------------------------------------
    261  ## Test cases to test equivalence of Monitor and Tally
    262  ## for monitored Resource instances
    263  ## -----------------------------------------------------------------------
    264
    265 -class Actor(Process):
    266      """Process used in MakeEquivTestCase"""
    267 -    def act(self,res):
    268          while True:
    269              yield request,self,res
    270              yield hold,self,1
    271              yield release,self,res
    272
    273 -class makeEquivTestCase(unittest.TestCase):
    274      """To test that the histograms produced in monitoring
    275      a Resource instance's queues are equivalent
    276      """
    277 -    def testResHistogram(self):
    278          initialize()
    279          r=Resource(monitored=True,monitorType=Monitor,name="TheResource/Monitor")
    280          r.waitMon.setHistogram(high=3,nbins=3)
    281          r.actMon.setHistogram(high=3,nbins=3)
    282
    283          for i in range (5):
    284            a=Actor()
    285            activate(a,a.act(r))
    286          simulate(until=20)
    287          mHistoAct= r.actMon.getHistogram()
    288          mHistoWait=r.waitMon.getHistogram()
    289
    290          initialize()
    291          r=Resource(monitored=True,monitorType=Tally,name="TheResource/Tally")
    292          r.waitMon.setHistogram(high=3,nbins=3)
    293          r.actMon.setHistogram(high=3,nbins=3)
    294          for i in range (5):
    295            a=Actor()
    296            activate(a,a.act(r))
    297          simulate(until=20)
    298          tHistoAct= r.actMon.getHistogram()
    299          tHistoWait=r.waitMon.getHistogram()
    300
    301          assert mHistoAct==tHistoAct,"actMon histograms are different"
    302          assert mHistoWait==tHistoWait,"waitMon histograms are different"
    303
    304 -def makeEquivSuite():
    305      suite = unittest.TestSuite()
    306      testResHistogram = makeEquivTestCase("testResHistogram")
    307      suite.addTests([testResHistogram
    308                      ])
    309      return suite
    310
    311  ## -----------------------------------------------------------------------
    312
    313  if __name__ == '__main__':
    314      print "MonitorTest.py %s"%__version__
    315      alltests = unittest.TestSuite((makeMSuite(),
    316  ##                                   makeHSuite(),
    317                                     makeTSuite(),
    318                                     makeEquivSuite()
    319                                     ))
    320      runner = unittest.TextTestRunner()
    321      runner.run(alltests)
    322