PageRenderTime 44ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/benchmarks/pybench/systimes.py

https://bitbucket.org/pombredanne/nuitka
Python | 230 lines | 107 code | 30 blank | 93 comment | 24 complexity | 793c4cb46e1c3712cd07c06a68854a55 MD5 | raw file
  1. #!/usr/bin/env python
  2. # Copyright 2013, Kay Hayen, mailto:kay.hayen@gmail.com
  3. #
  4. # Python test originally created or extracted from other peoples work. The
  5. # parts from me are licensed as below. It is at least Free Softwar where
  6. # it's copied from other people. In these cases, that will normally be
  7. # indicated.
  8. #
  9. # Licensed under the Apache License, Version 2.0 (the "License");
  10. # you may not use this file except in compliance with the License.
  11. # You may obtain a copy of the License at
  12. #
  13. # http://www.apache.org/licenses/LICENSE-2.0
  14. #
  15. # Unless required by applicable law or agreed to in writing, software
  16. # distributed under the License is distributed on an "AS IS" BASIS,
  17. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. # See the License for the specific language governing permissions and
  19. # limitations under the License.
  20. #
  21. """ systimes() user and system timer implementations for use by
  22. pybench.
  23. This module implements various different strategies for measuring
  24. performance timings. It tries to choose the best available method
  25. based on the platforma and available tools.
  26. On Windows, it is recommended to have the Mark Hammond win32
  27. package installed. Alternatively, the Thomas Heller ctypes
  28. packages can also be used.
  29. On Unix systems, the standard resource module provides the highest
  30. resolution timings. Unfortunately, it is not available on all Unix
  31. platforms.
  32. If no supported timing methods based on process time can be found,
  33. the module reverts to the highest resolution wall-clock timer
  34. instead. The system time part will then always be 0.0.
  35. The module exports one public API:
  36. def systimes():
  37. Return the current timer values for measuring user and system
  38. time as tuple of seconds (user_time, system_time).
  39. Copyright (c) 2006, Marc-Andre Lemburg (mal@egenix.com). See the
  40. documentation for further information on copyrights, or contact
  41. the author. All Rights Reserved.
  42. """
  43. import time, sys
  44. #
  45. # Note: Please keep this module compatible to Python 1.5.2.
  46. #
  47. # TODOs:
  48. #
  49. # * Add ctypes wrapper for new clock_gettime() real-time POSIX APIs;
  50. # these will then provide nano-second resolution where available.
  51. #
  52. # * Add a function that returns the resolution of systimes()
  53. # values, ie. systimesres().
  54. #
  55. ### Choose an implementation
  56. SYSTIMES_IMPLEMENTATION = None
  57. USE_CTYPES_GETPROCESSTIMES = 'ctypes GetProcessTimes() wrapper'
  58. USE_WIN32PROCESS_GETPROCESSTIMES = 'win32process.GetProcessTimes()'
  59. USE_RESOURCE_GETRUSAGE = 'resource.getrusage()'
  60. USE_PROCESS_TIME_CLOCK = 'time.clock() (process time)'
  61. USE_WALL_TIME_CLOCK = 'time.clock() (wall-clock)'
  62. USE_WALL_TIME_TIME = 'time.time() (wall-clock)'
  63. if sys.platform[:3] == 'win':
  64. # Windows platform
  65. try:
  66. import win32process
  67. except ImportError:
  68. try:
  69. import ctypes
  70. except ImportError:
  71. # Use the wall-clock implementation time.clock(), since this
  72. # is the highest resolution clock available on Windows
  73. SYSTIMES_IMPLEMENTATION = USE_WALL_TIME_CLOCK
  74. else:
  75. SYSTIMES_IMPLEMENTATION = USE_CTYPES_GETPROCESSTIMES
  76. else:
  77. SYSTIMES_IMPLEMENTATION = USE_WIN32PROCESS_GETPROCESSTIMES
  78. else:
  79. # All other platforms
  80. try:
  81. import resource
  82. except ImportError:
  83. pass
  84. else:
  85. SYSTIMES_IMPLEMENTATION = USE_RESOURCE_GETRUSAGE
  86. # Fall-back solution
  87. if SYSTIMES_IMPLEMENTATION is None:
  88. # Check whether we can use time.clock() as approximation
  89. # for systimes()
  90. start = time.clock()
  91. time.sleep(0.1)
  92. stop = time.clock()
  93. if stop - start < 0.001:
  94. # Looks like time.clock() is usable (and measures process
  95. # time)
  96. SYSTIMES_IMPLEMENTATION = USE_PROCESS_TIME_CLOCK
  97. else:
  98. # Use wall-clock implementation time.time() since this provides
  99. # the highest resolution clock on most systems
  100. SYSTIMES_IMPLEMENTATION = USE_WALL_TIME_TIME
  101. ### Implementations
  102. def getrusage_systimes():
  103. return resource.getrusage(resource.RUSAGE_SELF)[:2]
  104. def process_time_clock_systimes():
  105. return (time.clock(), 0.0)
  106. def wall_clock_clock_systimes():
  107. return (time.clock(), 0.0)
  108. def wall_clock_time_systimes():
  109. return (time.time(), 0.0)
  110. # Number of clock ticks per second for the values returned
  111. # by GetProcessTimes() on Windows.
  112. #
  113. # Note: Ticks returned by GetProcessTimes() are 100ns intervals on
  114. # Windows XP. However, the process times are only updated with every
  115. # clock tick and the frequency of these is somewhat lower: depending
  116. # on the OS version between 10ms and 15ms. Even worse, the process
  117. # time seems to be allocated to process currently running when the
  118. # clock interrupt arrives, ie. it is possible that the current time
  119. # slice gets accounted to a different process.
  120. WIN32_PROCESS_TIMES_TICKS_PER_SECOND = 1e7
  121. def win32process_getprocesstimes_systimes():
  122. d = win32process.GetProcessTimes(win32process.GetCurrentProcess())
  123. return (d['UserTime'] / WIN32_PROCESS_TIMES_TICKS_PER_SECOND,
  124. d['KernelTime'] / WIN32_PROCESS_TIMES_TICKS_PER_SECOND)
  125. def ctypes_getprocesstimes_systimes():
  126. creationtime = ctypes.c_ulonglong()
  127. exittime = ctypes.c_ulonglong()
  128. kerneltime = ctypes.c_ulonglong()
  129. usertime = ctypes.c_ulonglong()
  130. rc = ctypes.windll.kernel32.GetProcessTimes(
  131. ctypes.windll.kernel32.GetCurrentProcess(),
  132. ctypes.byref(creationtime),
  133. ctypes.byref(exittime),
  134. ctypes.byref(kerneltime),
  135. ctypes.byref(usertime))
  136. if not rc:
  137. raise TypeError('GetProcessTimes() returned an error')
  138. return (usertime.value / WIN32_PROCESS_TIMES_TICKS_PER_SECOND,
  139. kerneltime.value / WIN32_PROCESS_TIMES_TICKS_PER_SECOND)
  140. # Select the default for the systimes() function
  141. if SYSTIMES_IMPLEMENTATION is USE_RESOURCE_GETRUSAGE:
  142. systimes = getrusage_systimes
  143. elif SYSTIMES_IMPLEMENTATION is USE_PROCESS_TIME_CLOCK:
  144. systimes = process_time_clock_systimes
  145. elif SYSTIMES_IMPLEMENTATION is USE_WALL_TIME_CLOCK:
  146. systimes = wall_clock_clock_systimes
  147. elif SYSTIMES_IMPLEMENTATION is USE_WALL_TIME_TIME:
  148. systimes = wall_clock_time_systimes
  149. elif SYSTIMES_IMPLEMENTATION is USE_WIN32PROCESS_GETPROCESSTIMES:
  150. systimes = win32process_getprocesstimes_systimes
  151. elif SYSTIMES_IMPLEMENTATION is USE_CTYPES_GETPROCESSTIMES:
  152. systimes = ctypes_getprocesstimes_systimes
  153. else:
  154. raise TypeError('no suitable systimes() implementation found')
  155. def processtime():
  156. """ Return the total time spent on the process.
  157. This is the sum of user and system time as returned by
  158. systimes().
  159. """
  160. user, system = systimes()
  161. return user + system
  162. ### Testing
  163. def some_workload():
  164. x = 0L
  165. for i in xrange(10000000L):
  166. x = x + 1L
  167. def test_workload():
  168. print 'Testing systimes() under load conditions'
  169. t0 = systimes()
  170. some_workload()
  171. t1 = systimes()
  172. print 'before:', t0
  173. print 'after:', t1
  174. print 'differences:', (t1[0] - t0[0], t1[1] - t0[1])
  175. print
  176. def test_idle():
  177. print 'Testing systimes() under idle conditions'
  178. t0 = systimes()
  179. time.sleep(1)
  180. t1 = systimes()
  181. print 'before:', t0
  182. print 'after:', t1
  183. print 'differences:', (t1[0] - t0[0], t1[1] - t0[1])
  184. print
  185. if __name__ == '__main__':
  186. print 'Using %s as timer' % SYSTIMES_IMPLEMENTATION
  187. print
  188. test_workload()
  189. test_idle()