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