+++ /dev/null
-#!/usr/bin/env python\r
-\r
-""" systimes() user and system timer implementations for use by\r
- pybench.\r
-\r
- This module implements various different strategies for measuring\r
- performance timings. It tries to choose the best available method\r
- based on the platforma and available tools.\r
-\r
- On Windows, it is recommended to have the Mark Hammond win32\r
- package installed. Alternatively, the Thomas Heller ctypes\r
- packages can also be used.\r
-\r
- On Unix systems, the standard resource module provides the highest\r
- resolution timings. Unfortunately, it is not available on all Unix\r
- platforms.\r
-\r
- If no supported timing methods based on process time can be found,\r
- the module reverts to the highest resolution wall-clock timer\r
- instead. The system time part will then always be 0.0.\r
-\r
- The module exports one public API:\r
-\r
- def systimes():\r
-\r
- Return the current timer values for measuring user and system\r
- time as tuple of seconds (user_time, system_time).\r
-\r
- Copyright (c) 2006, Marc-Andre Lemburg (mal@egenix.com). See the\r
- documentation for further information on copyrights, or contact\r
- the author. All Rights Reserved.\r
-\r
-"""\r
-import time, sys\r
-\r
-#\r
-# Note: Please keep this module compatible to Python 1.5.2.\r
-#\r
-# TODOs:\r
-#\r
-# * Add ctypes wrapper for new clock_gettime() real-time POSIX APIs;\r
-# these will then provide nano-second resolution where available.\r
-#\r
-# * Add a function that returns the resolution of systimes()\r
-# values, ie. systimesres().\r
-#\r
-\r
-### Choose an implementation\r
-\r
-SYSTIMES_IMPLEMENTATION = None\r
-USE_CTYPES_GETPROCESSTIMES = 'ctypes GetProcessTimes() wrapper'\r
-USE_WIN32PROCESS_GETPROCESSTIMES = 'win32process.GetProcessTimes()'\r
-USE_RESOURCE_GETRUSAGE = 'resource.getrusage()'\r
-USE_PROCESS_TIME_CLOCK = 'time.clock() (process time)'\r
-USE_WALL_TIME_CLOCK = 'time.clock() (wall-clock)'\r
-USE_WALL_TIME_TIME = 'time.time() (wall-clock)'\r
-\r
-if sys.platform[:3] == 'win':\r
- # Windows platform\r
- try:\r
- import win32process\r
- except ImportError:\r
- try:\r
- import ctypes\r
- except ImportError:\r
- # Use the wall-clock implementation time.clock(), since this\r
- # is the highest resolution clock available on Windows\r
- SYSTIMES_IMPLEMENTATION = USE_WALL_TIME_CLOCK\r
- else:\r
- SYSTIMES_IMPLEMENTATION = USE_CTYPES_GETPROCESSTIMES\r
- else:\r
- SYSTIMES_IMPLEMENTATION = USE_WIN32PROCESS_GETPROCESSTIMES\r
-else:\r
- # All other platforms\r
- try:\r
- import resource\r
- except ImportError:\r
- pass\r
- else:\r
- SYSTIMES_IMPLEMENTATION = USE_RESOURCE_GETRUSAGE\r
-\r
-# Fall-back solution\r
-if SYSTIMES_IMPLEMENTATION is None:\r
- # Check whether we can use time.clock() as approximation\r
- # for systimes()\r
- start = time.clock()\r
- time.sleep(0.1)\r
- stop = time.clock()\r
- if stop - start < 0.001:\r
- # Looks like time.clock() is usable (and measures process\r
- # time)\r
- SYSTIMES_IMPLEMENTATION = USE_PROCESS_TIME_CLOCK\r
- else:\r
- # Use wall-clock implementation time.time() since this provides\r
- # the highest resolution clock on most systems\r
- SYSTIMES_IMPLEMENTATION = USE_WALL_TIME_TIME\r
-\r
-### Implementations\r
-\r
-def getrusage_systimes():\r
- return resource.getrusage(resource.RUSAGE_SELF)[:2]\r
-\r
-def process_time_clock_systimes():\r
- return (time.clock(), 0.0)\r
-\r
-def wall_clock_clock_systimes():\r
- return (time.clock(), 0.0)\r
-\r
-def wall_clock_time_systimes():\r
- return (time.time(), 0.0)\r
-\r
-# Number of clock ticks per second for the values returned\r
-# by GetProcessTimes() on Windows.\r
-#\r
-# Note: Ticks returned by GetProcessTimes() are 100ns intervals on\r
-# Windows XP. However, the process times are only updated with every\r
-# clock tick and the frequency of these is somewhat lower: depending\r
-# on the OS version between 10ms and 15ms. Even worse, the process\r
-# time seems to be allocated to process currently running when the\r
-# clock interrupt arrives, ie. it is possible that the current time\r
-# slice gets accounted to a different process.\r
-\r
-WIN32_PROCESS_TIMES_TICKS_PER_SECOND = 1e7\r
-\r
-def win32process_getprocesstimes_systimes():\r
- d = win32process.GetProcessTimes(win32process.GetCurrentProcess())\r
- return (d['UserTime'] / WIN32_PROCESS_TIMES_TICKS_PER_SECOND,\r
- d['KernelTime'] / WIN32_PROCESS_TIMES_TICKS_PER_SECOND)\r
-\r
-def ctypes_getprocesstimes_systimes():\r
- creationtime = ctypes.c_ulonglong()\r
- exittime = ctypes.c_ulonglong()\r
- kerneltime = ctypes.c_ulonglong()\r
- usertime = ctypes.c_ulonglong()\r
- rc = ctypes.windll.kernel32.GetProcessTimes(\r
- ctypes.windll.kernel32.GetCurrentProcess(),\r
- ctypes.byref(creationtime),\r
- ctypes.byref(exittime),\r
- ctypes.byref(kerneltime),\r
- ctypes.byref(usertime))\r
- if not rc:\r
- raise TypeError('GetProcessTimes() returned an error')\r
- return (usertime.value / WIN32_PROCESS_TIMES_TICKS_PER_SECOND,\r
- kerneltime.value / WIN32_PROCESS_TIMES_TICKS_PER_SECOND)\r
-\r
-# Select the default for the systimes() function\r
-\r
-if SYSTIMES_IMPLEMENTATION is USE_RESOURCE_GETRUSAGE:\r
- systimes = getrusage_systimes\r
-\r
-elif SYSTIMES_IMPLEMENTATION is USE_PROCESS_TIME_CLOCK:\r
- systimes = process_time_clock_systimes\r
-\r
-elif SYSTIMES_IMPLEMENTATION is USE_WALL_TIME_CLOCK:\r
- systimes = wall_clock_clock_systimes\r
-\r
-elif SYSTIMES_IMPLEMENTATION is USE_WALL_TIME_TIME:\r
- systimes = wall_clock_time_systimes\r
-\r
-elif SYSTIMES_IMPLEMENTATION is USE_WIN32PROCESS_GETPROCESSTIMES:\r
- systimes = win32process_getprocesstimes_systimes\r
-\r
-elif SYSTIMES_IMPLEMENTATION is USE_CTYPES_GETPROCESSTIMES:\r
- systimes = ctypes_getprocesstimes_systimes\r
-\r
-else:\r
- raise TypeError('no suitable systimes() implementation found')\r
-\r
-def processtime():\r
-\r
- """ Return the total time spent on the process.\r
-\r
- This is the sum of user and system time as returned by\r
- systimes().\r
-\r
- """\r
- user, system = systimes()\r
- return user + system\r
-\r
-### Testing\r
-\r
-def some_workload():\r
- x = 0L\r
- for i in xrange(10000000L):\r
- x = x + 1L\r
-\r
-def test_workload():\r
- print 'Testing systimes() under load conditions'\r
- t0 = systimes()\r
- some_workload()\r
- t1 = systimes()\r
- print 'before:', t0\r
- print 'after:', t1\r
- print 'differences:', (t1[0] - t0[0], t1[1] - t0[1])\r
- print\r
-\r
-def test_idle():\r
- print 'Testing systimes() under idle conditions'\r
- t0 = systimes()\r
- time.sleep(1)\r
- t1 = systimes()\r
- print 'before:', t0\r
- print 'after:', t1\r
- print 'differences:', (t1[0] - t0[0], t1[1] - t0[1])\r
- print\r
-\r
-if __name__ == '__main__':\r
- print 'Using %s as timer' % SYSTIMES_IMPLEMENTATION\r
- print\r
- test_workload()\r
- test_idle()\r