PageRenderTime 24ms CodeModel.GetById 13ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 1ms

/src/lib/time/benchmark.e

http://github.com/tybor/Liberty
Specman e | 185 lines | 134 code | 22 blank | 29 comment | 5 complexity | b2f62f08fae7b00f74d8f39245aa238e MD5 | raw file
  1-- This file is part of a Liberty Eiffel library.
  2-- See the full copyright at the end.
  3--
  4class BENCHMARK
  5   --
  6   -- Benchmarking facilities
  7   --
  8
  9insert
 10   ANY
 11   PLATFORM
 12
 13create {ANY}
 14   make
 15
 16feature {ANY}
 17   make (n: like name)
 18      require
 19         n /= Void
 20      do
 21         name := n
 22         in_progress := False
 23         counter := 0
 24         total_time := 0
 25         min_time := Maximum_real
 26         max_time := 0
 27         last_print := 0
 28      ensure
 29         not in_progress
 30         counter = 0
 31      end
 32
 33   start
 34      require
 35         not in_progress
 36      do
 37         start_time.update
 38         in_progress := True
 39      ensure
 40         in_progress
 41      end
 42
 43   next
 44      require
 45         in_progress
 46      local
 47         end_time: MICROSECOND_TIME; exec_time: REAL
 48      do
 49         end_time.update
 50         exec_time := start_time.elapsed_seconds(end_time)
 51         start_time := end_time
 52         total_time := total_time + exec_time
 53         if exec_time < min_time then
 54            min_time := exec_time
 55         end
 56         if exec_time > max_time then
 57            max_time := exec_time
 58         end
 59         counter := counter + 1
 60         smart_print
 61      ensure
 62         counter = old counter + 1
 63         in_progress
 64      end
 65
 66   stop
 67      require
 68         in_progress
 69      do
 70         next
 71         in_progress := False
 72      ensure
 73         counter = old counter + 1
 74         not in_progress
 75      end
 76
 77   break
 78         -- useful for loop termination
 79      require
 80         in_progress
 81      do
 82         in_progress := False
 83      ensure
 84         counter = old counter
 85         not in_progress
 86      end
 87
 88   name: STRING
 89
 90   in_progress: BOOLEAN
 91
 92   counter: INTEGER --|*** PH (14/06/2005) INTEGER_64?
 93
 94   min_time: REAL -- in seconds
 95
 96   max_time: REAL -- in seconds
 97
 98   total_time: REAL -- in seconds
 99
100   mean_time: REAL
101         -- in seconds
102      require
103         counter > 0
104      do
105         Result := total_time / counter
106      end
107
108   set_custom_print (cp: like custom_print)
109      do
110         custom_print := cp
111      end
112
113   print_now
114      do
115         if custom_print /= Void then
116            custom_print.call([Current])
117         else
118            default_print
119         end
120         last_print := total_time
121      end
122
123   smart_print
124      do
125         if total_time - last_print > total_time / 10 + 1 then
126            print_now
127         end
128      end
129
130feature {}
131   start_time: MICROSECOND_TIME
132
133   last_print: REAL -- total_time value when last print occurred
134
135   custom_print: PROCEDURE[TUPLE[BENCHMARK]] -- Used if non Void. See also default_print
136
137   default_print
138         -- Used if custom_print is Void
139      require
140         counter > 0
141      local
142         speed: REAL
143      do
144         speed := mean_time
145         if speed < 0.1 then
146            std_output.put_real(1.0 / speed)
147            std_output.put_character(' ')
148            std_output.put_string(name)
149            std_output.put_string(once "/s")
150         else
151            std_output.put_real(speed)
152            std_output.put_character(' ')
153            std_output.put_string(once "s/")
154            std_output.put_string(name)
155         end
156         std_output.put_string(once ", min: ")
157         std_output.put_real(min_time)
158         std_output.put_string(once "s, max: ")
159         std_output.put_real(max_time)
160         std_output.put_string(once "s, counter: ")
161         std_output.put_integer(counter)
162         std_output.put_new_line
163      end
164
165end -- class BENCHMARK
166--
167-- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
168--
169-- Permission is hereby granted, free of charge, to any person obtaining a copy
170-- of this software and associated documentation files (the "Software"), to deal
171-- in the Software without restriction, including without limitation the rights
172-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
173-- copies of the Software, and to permit persons to whom the Software is
174-- furnished to do so, subject to the following conditions:
175--
176-- The above copyright notice and this permission notice shall be included in
177-- all copies or substantial portions of the Software.
178--
179-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
180-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
181-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
182-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
183-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
184-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
185-- THE SOFTWARE.