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