s/BenchmarkInstance.st
changeset 6 25b264cec44e
parent 5 8669edf62d9b
child 11 88ec277d733a
equal deleted inserted replaced
5:8669edf62d9b 6:25b264cec44e
    10 !
    10 !
    11 
    11 
    12 
    12 
    13 !BenchmarkInstance class methodsFor:'instance creation'!
    13 !BenchmarkInstance class methodsFor:'instance creation'!
    14 
    14 
    15 class: class benchmark: benchmark
    15 class:class selector:benchmark 
    16     ^self new class: class benchmark: benchmark
    16     ^ self new class:class selector:benchmark
    17 
    17 
    18     "Created: / 27-05-2013 / 19:04:02 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    18     "Created: / 27-05-2013 / 19:04:02 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    19 ! !
    19 ! !
    20 
    20 
    21 !BenchmarkInstance class methodsFor:'class initialization'!
    21 !BenchmarkInstance class methodsFor:'class initialization'!
    42     "Created: / 31-05-2013 / 12:02:10 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    42     "Created: / 31-05-2013 / 12:02:10 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    43 ! !
    43 ! !
    44 
    44 
    45 !BenchmarkInstance methodsFor:'accessing'!
    45 !BenchmarkInstance methodsFor:'accessing'!
    46 
    46 
    47 benchmark
       
    48     ^ benchmarkSelector
       
    49 !
       
    50 
       
    51 instance
    47 instance
    52     ^ instance
    48     ^ instance
       
    49 !
       
    50 
       
    51 selector
       
    52     ^ benchmarkSelector
    53 ! !
    53 ! !
    54 
    54 
    55 !BenchmarkInstance methodsFor:'initialization'!
    55 !BenchmarkInstance methodsFor:'initialization'!
    56 
    56 
    57 class: class benchmark: benchmark
    57 class:class selector:benchmark 
    58     self instance: class new benchmark: benchmark
    58     self instance:class new selector:benchmark
    59 
    59 
    60     "Created: / 27-05-2013 / 19:04:31 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    60     "Created: / 27-05-2013 / 19:04:31 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    61 !
    61 !
    62 
    62 
    63 instance: anObject benchmark: aSelector
    63 instance:anObject selector:aSelector 
    64     | benchmarkMethod annotation |
    64     | benchmarkMethod  annotation |
    65     (anObject respondsTo: aSelector) ifFalse:[
    65 
    66         self error:'Object does not respond to benchmark (no ', anObject class printString , '>>', aSelector storeString.
    66     (anObject respondsTo:aSelector) ifFalse:[
    67     ].
    67         self 
    68 
    68             error:'Object does not respond to benchmark (no ' 
       
    69                     , anObject class printString , '>>' 
       
    70                     , aSelector storeString.
       
    71     ].
    69     instance := anObject.
    72     instance := anObject.
    70     benchmarkSelector := aSelector.
    73     benchmarkSelector := aSelector.
    71 
    74     instance class methodDictionary 
    72     instance class methodDictionary keysAndValuesDo:[:selector :method |
    75         keysAndValuesDo:[:selector :method | 
    73         method numArgs == 0 ifTrue:[
    76             method numArgs == 0 ifTrue:[
    74             (method pragmaAt: #setup) notNil ifTrue:[
    77                 (method pragmaAt:#setup) notNil ifTrue:[
    75                 setUpSelector1 notNil ifTrue:[
    78                     setUpSelector1 notNil ifTrue:[
    76                     self error: 'More than one <setup> method'.
    79                         self error:'More than one <setup> method'.
       
    80                     ].
       
    81                     setUpSelector1 := selector.
    77                 ].
    82                 ].
    78                 setUpSelector1 := selector.
    83                 (method pragmaAt:#teardown) notNil ifTrue:[
       
    84                     tearDownSelector1 notNil ifTrue:[
       
    85                         self error:'More than one <teardown> method'.
       
    86                     ].
       
    87                     tearDownSelector1 := selector.
       
    88                 ].
       
    89                 (method pragmaAt:#warmup) notNil ifTrue:[
       
    90                     warmUpSelector1 notNil ifTrue:[
       
    91                         self error:'More than one <warmup> method'.
       
    92                     ].
       
    93                     warmUpSelector1 := selector.
       
    94                 ].
    79             ].
    95             ].
    80             (method pragmaAt: #teardown) notNil ifTrue:[
    96         ].
    81                 tearDownSelector1 notNil ifTrue:[
    97     benchmarkMethod := instance class compiledMethodAt:benchmarkSelector.
    82                     self error: 'More than one <teardown> method'.
    98     annotation := benchmarkMethod pragmaAt:#setup:.
    83                 ].
       
    84                 tearDownSelector1 := selector.
       
    85             ].
       
    86             (method pragmaAt: #warmup) notNil ifTrue:[
       
    87                 warmUpSelector1 notNil ifTrue:[
       
    88                     self error: 'More than one <warmup> method'.
       
    89                 ].
       
    90                 warmUpSelector1 := selector.
       
    91             ].
       
    92         ].
       
    93     ].
       
    94 
       
    95     benchmarkMethod := instance class compiledMethodAt: benchmarkSelector.
       
    96 
       
    97     annotation := benchmarkMethod pragmaAt: #setup:.
       
    98     annotation notNil ifTrue:[
    99     annotation notNil ifTrue:[
    99         | method selector |
   100         | method  selector |
   100         selector := annotation argumentAt: 1.
   101 
       
   102         selector := annotation argumentAt:1.
   101         selector isSymbol ifFalse:[
   103         selector isSymbol ifFalse:[
   102             self error: '<setup:> annotation argument not a symbol'.
   104             self error:'<setup:> annotation argument not a symbol'.
   103         ].
   105         ].
   104         method := instance compiledMethodAt: selector ifAbsent: [nil].
   106         method := instance compiledMethodAt:selector ifAbsent:[ nil ].
   105         method isNil ifTrue:[
   107         method isNil ifTrue:[
   106             self error: '<setup:> method does not exist (', selector,')'.
   108             self error:'<setup:> method does not exist (' , selector , ')'.
   107         ].
   109         ].
   108         method numArgs ~~ 0 ifTrue:[
   110         method numArgs ~~ 0 ifTrue:[
   109             self error: '<setup:> method has arguments (', selector,')'.
   111             self error:'<setup:> method has arguments (' , selector , ')'.
   110         ].
   112         ].
   111         setUpSelector2 := selector.
   113         setUpSelector2 := selector.
   112     ].
   114     ].
   113 
   115     annotation := benchmarkMethod pragmaAt:#teardown:.
   114     annotation := benchmarkMethod pragmaAt: #teardown:.
       
   115     annotation notNil ifTrue:[
   116     annotation notNil ifTrue:[
   116         | method selector |
   117         | method  selector |
   117         selector := annotation argumentAt: 1.
   118 
       
   119         selector := annotation argumentAt:1.
   118         selector isSymbol ifFalse:[
   120         selector isSymbol ifFalse:[
   119             self error: '<teardown:> annotation argument not a symbol'.
   121             self error:'<teardown:> annotation argument not a symbol'.
   120         ].
   122         ].
   121         method := instance compiledMethodAt: selector ifAbsent: [nil].
   123         method := instance compiledMethodAt:selector ifAbsent:[ nil ].
   122         method isNil ifTrue:[
   124         method isNil ifTrue:[
   123             self error: '<teardown:> method does not exist (', selector,')'.
   125             self error:'<teardown:> method does not exist (' , selector , ')'.
   124         ].
   126         ].
   125         method numArgs ~~ 0 ifTrue:[
   127         method numArgs ~~ 0 ifTrue:[
   126             self error: '<teardown:> method has arguments (', selector,')'.
   128             self error:'<teardown:> method has arguments (' , selector , ')'.
   127         ].
   129         ].
   128         tearDownSelector2 := selector.
   130         tearDownSelector2 := selector.
   129     ].
   131     ].
   130 
   132     annotation := benchmarkMethod pragmaAt:#warmup:.
   131     annotation := benchmarkMethod pragmaAt: #warmup:.
       
   132     annotation notNil ifTrue:[
   133     annotation notNil ifTrue:[
   133         | method selector |
   134         | method  selector |
   134         selector := annotation argumentAt: 1.
   135 
       
   136         selector := annotation argumentAt:1.
   135         selector isSymbol ifFalse:[
   137         selector isSymbol ifFalse:[
   136             self error: '<warmup:> annotation argument not a symbol'.
   138             self error:'<warmup:> annotation argument not a symbol'.
   137         ].
   139         ].
   138         method := instance compiledMethodAt: selector ifAbsent: [nil].
   140         method := instance compiledMethodAt:selector ifAbsent:[ nil ].
   139         method isNil ifTrue:[
   141         method isNil ifTrue:[
   140             self error: '<warmup:> method does not exist (', selector,')'.
   142             self error:'<warmup:> method does not exist (' , selector , ')'.
   141         ].
   143         ].
   142         method numArgs ~~ 0 ifTrue:[
   144         method numArgs ~~ 0 ifTrue:[
   143             self error: '<warmup:> method has arguments (', selector,')'.
   145             self error:'<warmup:> method has arguments (' , selector , ')'.
   144         ].
   146         ].
   145         warmUpSelector2 := selector.
   147         warmUpSelector2 := selector.
   146     ].
   148     ].
   147 
   149 
   148     "Created: / 27-05-2013 / 19:06:29 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   150     "Created: / 27-05-2013 / 19:06:29 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   177     t := -1.
   179     t := -1.
   178     [
   180     [
   179         self setUp.
   181         self setUp.
   180         self setUpParameters:aBenchmarkParameterSet.
   182         self setUpParameters:aBenchmarkParameterSet.
   181         self warmUp.
   183         self warmUp.
   182         t := self performBenchmark.
   184         t := self benchmark.
   183     ] ensure:[ self tearDown. ].
   185     ] ensure:[ self tearDown. ].
   184     ^ t
   186     ^ t
   185 
   187 
   186     "Created: / 27-05-2013 / 22:25:33 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   188     "Created: / 27-05-2013 / 22:25:33 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   187     "Modified: / 28-05-2013 / 08:49:21 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   189     "Modified: / 28-05-2013 / 08:49:21 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   193     "Created: / 27-05-2013 / 22:18:58 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   195     "Created: / 27-05-2013 / 22:18:58 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   194 ! !
   196 ! !
   195 
   197 
   196 !BenchmarkInstance methodsFor:'running-private'!
   198 !BenchmarkInstance methodsFor:'running-private'!
   197 
   199 
   198 performBenchmark
   200 benchmark
   199     | t0 t1 |
   201     | t0  t1 |
   200 
   202 
   201     t0 := MillisecondsTime value.
   203     t0 := MillisecondsTime value.
   202     instance perform: benchmarkSelector.
   204     instance perform:benchmarkSelector.
   203     t1 := MillisecondsTime value.
   205     t1 := MillisecondsTime value.
   204     ^t1 - t0
   206     ^ t1 - t0
   205 
   207 
   206     "Created: / 28-05-2013 / 08:49:26 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   208     "Created: / 28-05-2013 / 08:49:26 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   207     "Modified: / 31-05-2013 / 12:02:35 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   209     "Modified: / 31-05-2013 / 12:02:35 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   208 !
       
   209 
       
   210 performSetUpAnnotatedBy: keyword
       
   211 
       
   212     "Created: / 28-05-2013 / 08:51:05 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   213 !
   210 !
   214 
   211 
   215 setUp
   212 setUp
   216     setUpSelector1 notNil ifTrue:[
   213     setUpSelector1 notNil ifTrue:[
   217         instance perform: setUpSelector1 
   214         instance perform: setUpSelector1 
   297     ].
   294     ].
   298     "Default warmup"
   295     "Default warmup"
   299     warmed ifFalse:[
   296     warmed ifFalse:[
   300         instance perform: benchmarkSelector 
   297         instance perform: benchmarkSelector 
   301     ].
   298     ].
       
   299     ObjectMemory garbageCollect; tenure.
   302 
   300 
   303     "Created: / 27-05-2013 / 19:02:36 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   301     "Created: / 27-05-2013 / 19:02:36 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   304     "Modified: / 28-05-2013 / 11:00:57 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   302     "Modified: / 04-06-2013 / 22:19:52 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   305     "Modified (comment): / 31-05-2013 / 00:30:49 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   306 ! !
   303 ! !
   307 
   304 
   308 !BenchmarkInstance class methodsFor:'documentation'!
   305 !BenchmarkInstance class methodsFor:'documentation'!
   309 
   306 
   310 version_HG
   307 version_HG